Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/bin/bash
# Add Vagrant's NFS setup commands to sudoers, for `vagrant up` without a password
# Updated to work with Vagrant 1.3.x
# Stage updated sudoers in a temporary file for syntax checking
TMP=$(mktemp -t vagrant_sudoers)
cat /etc/sudoers > $TMP
cat >> $TMP <<EOF
# Allow passwordless startup of Vagrant when using NFS.
# https://gist.github.com/joemaller/6764700
Cmnd_Alias VAGRANT_EXPORTS_ADD = /bin/bash -c echo '*' >> /etc/exports
Cmnd_Alias VAGRANT_NFSD = /sbin/nfsd restart
Cmnd_Alias VAGRANT_EXPORTS_REMOVE = /usr/bin/sed -E -e /*/ d -ibak /etc/exports
%staff ALL=(root) NOPASSWD: VAGRANT_EXPORTS_ADD, VAGRANT_NFSD, VAGRANT_EXPORTS_REMOVE
EOF
# Check syntax and overwrite sudoers if clean
visudo -c -f $TMP
if [ $? -eq 0 ]; then
echo "Adding vagrant commands to sudoers"
cat $TMP > /etc/sudoers
else
echo "sudoers syntax wasn't valid. Aborting!"
fi
rm -f $TMP
@robbanl

This comment has been minimized.

Show comment
Hide comment
@robbanl

robbanl Nov 15, 2013

Didn't work :-(

robbanl commented Nov 15, 2013

Didn't work :-(

@joemaller

This comment has been minimized.

Show comment
Hide comment
@joemaller

joemaller Nov 17, 2013

@roberlord, was there an error or anything?

Owner

joemaller commented Nov 17, 2013

@roberlord, was there an error or anything?

@Starefossen

This comment has been minimized.

Show comment
Hide comment
@Starefossen

Starefossen Feb 10, 2014

Works like a charm for Vagrant 1.4.3!

Starefossen commented Feb 10, 2014

Works like a charm for Vagrant 1.4.3!

@karlingen

This comment has been minimized.

Show comment
Hide comment
@karlingen

karlingen Feb 21, 2014

Yep. Working on Vagrant 1.4.3 !! Thanks!!

karlingen commented Feb 21, 2014

Yep. Working on Vagrant 1.4.3 !! Thanks!!

@moorooboorai-jeroen

This comment has been minimized.

Show comment
Hide comment
@moorooboorai-jeroen

moorooboorai-jeroen Mar 9, 2014

Like a charm! Thank you very, very much...

moorooboorai-jeroen commented Mar 9, 2014

Like a charm! Thank you very, very much...

@ngocphamm

This comment has been minimized.

Show comment
Hide comment
@ngocphamm

ngocphamm Mar 17, 2014

I don't really know why but this worked the first time after I apply the hack. After that I hasn't worked anymore. I still have to enter password.

ngocphamm commented Mar 17, 2014

I don't really know why but this worked the first time after I apply the hack. After that I hasn't worked anymore. I still have to enter password.

@karlingen

This comment has been minimized.

Show comment
Hide comment
@karlingen

karlingen Mar 24, 2014

@ngocphamm are you using something else than bash, such as zsh?

karlingen commented Mar 24, 2014

@ngocphamm are you using something else than bash, such as zsh?

@justinatcamfil

This comment has been minimized.

Show comment
Hide comment
@justinatcamfil

justinatcamfil Apr 10, 2014

Doesn't work with 1.5 because they changed the system command to something like:

sudo -s -- echo "#{line}" >> /etc/exports

@ngocphamm, the reason it worked the first time is likely that your sudo timestamp_timeout was not up. You can verify that by setting, in sudoers:

Defaults  timestamp_timeout = 0

Then sudo won't cache the result of a good authentication for a time. This means you have to enter the password for every sudo attempt... successive attempts are normally "cached".

The solution for this to work with vagrant 1.5 isn't clear to me since changing VAGRANT_EXPORTS_ADD to something like the below doesn't seem to work.

/bin/bash echo '*' >> /etc/exports

justinatcamfil commented Apr 10, 2014

Doesn't work with 1.5 because they changed the system command to something like:

sudo -s -- echo "#{line}" >> /etc/exports

@ngocphamm, the reason it worked the first time is likely that your sudo timestamp_timeout was not up. You can verify that by setting, in sudoers:

Defaults  timestamp_timeout = 0

Then sudo won't cache the result of a good authentication for a time. This means you have to enter the password for every sudo attempt... successive attempts are normally "cached".

The solution for this to work with vagrant 1.5 isn't clear to me since changing VAGRANT_EXPORTS_ADD to something like the below doesn't seem to work.

/bin/bash echo '*' >> /etc/exports
@ngocphamm

This comment has been minimized.

Show comment
Hide comment
@ngocphamm

ngocphamm Apr 24, 2014

Too bad GitHib doesn't have notification for mentions in Gist comments. I didn't know there're answers here till today.

@karlingen yes I'm using zsh

@justinatcamfil That's probably the reason. I ended up leave it alone as I rarely destroy the box. When I don't need it I just need to vagrant suspend and it's all good. Memory released.

ngocphamm commented Apr 24, 2014

Too bad GitHib doesn't have notification for mentions in Gist comments. I didn't know there're answers here till today.

@karlingen yes I'm using zsh

@justinatcamfil That's probably the reason. I ended up leave it alone as I rarely destroy the box. When I don't need it I just need to vagrant suspend and it's all good. Memory released.

@ngocphamm

This comment has been minimized.

Show comment
Hide comment
@ngocphamm

ngocphamm May 7, 2014

Finally Vagrant 1.6 has this fixed with a "stable" hack to visudo:

Cmnd_Alias VAGRANT_EXPORTS_ADD = /usr/bin/tee -a /etc/exports
Cmnd_Alias VAGRANT_NFSD = /sbin/nfsd restart
Cmnd_Alias VAGRANT_EXPORTS_REMOVE = /usr/bin/sed -E -e /*/ d -ibak /etc/exports
%admin ALL=(root) NOPASSWD: VAGRANT_EXPORTS_ADD, VAGRANT_NFSD, VAGRANT_EXPORTS_REMOVE

Ref: hashicorp/vagrant#3638

ngocphamm commented May 7, 2014

Finally Vagrant 1.6 has this fixed with a "stable" hack to visudo:

Cmnd_Alias VAGRANT_EXPORTS_ADD = /usr/bin/tee -a /etc/exports
Cmnd_Alias VAGRANT_NFSD = /sbin/nfsd restart
Cmnd_Alias VAGRANT_EXPORTS_REMOVE = /usr/bin/sed -E -e /*/ d -ibak /etc/exports
%admin ALL=(root) NOPASSWD: VAGRANT_EXPORTS_ADD, VAGRANT_NFSD, VAGRANT_EXPORTS_REMOVE

Ref: hashicorp/vagrant#3638

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment