Skip to content

Instantly share code, notes, and snippets.

@CedricGatay
Forked from GUI/install_vagrant_sudoers.sh
Last active August 29, 2015 13:55
Show Gist options
  • Save CedricGatay/8729196 to your computer and use it in GitHub Desktop.
Save CedricGatay/8729196 to your computer and use it in GitHub Desktop.
Script for placing sudoers.d files with syntax-checking, from https://gist.github.com/GUI/2864683, adapted to work with Vagrant 1.4.x under OS X / Linux
#!/bin/bash
# Script for placing sudoers.d files with syntax-checking
# From https://gist.github.com/GUI/2864683, adapted to work
# with Vagrant 1.4.x under OS X / Linux
# Making a temporary file to contain the sudoers-changes to be pre-checked
TMP=$(mktemp -t vagrant_sudoers.XXX)
cat /etc/sudoers > $TMP
# Does not reedit file
if [ "$(grep "VAGRANT_EXPORTS_ADD" $TMP | wc -l)" == "0" ]; then
if [ "$(uname)" == "Darwin" ]; then
# BSD commands can be seen at https://github.com/mitchellh/vagrant/blob/master/plugins/hosts/bsd/cap/nfs.rb
cat >> $TMP <<EOF
Cmnd_Alias VAGRANT_EXPORTS_ADD = $SHELL -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
else
# Linux commands can be seen at https://github.com/mitchellh/vagrant/blob/master/plugins/hosts/linux/cap/nfs.rb
cat >> $TMP <<EOF
# Allow passwordless startup of Vagrant when using NFS.
Cmnd_Alias VAGRANT_EXPORTS_ADD = /usr/bin/su root -c echo '*' >> /etc/exports
Cmnd_Alias VAGRANT_NFSD = /sbin/nfsd restart
Cmnd_Alias VAGRANT_EXPORTS_REMOVE = /usr/bin/sed -r -e /*/ d -ibak /etc/exports
%staff ALL=(root) NOPASSWD: VAGRANT_EXPORTS_ADD, VAGRANT_NFSD, VAGRANT_EXPORTS_REMOVE
EOF
fi
visudo -c -f $TMP
if [ $? -eq 0 ]; then
cat $TMP > /etc/sudoers
fi
rm -f $TMP
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment