Quantcast
Viewing all articles
Browse latest Browse all 13

Vagrant: sudo access and the hostsupdater plugin

Bringing up a vagrant machine is as easy as vagrant up.

If you’re a web developer, it would be nice if it were to add the private network IP address to /etc/hosts of the host machine. Thus giving you instant access to http://my-awesome-site.dev/

This doesn’t happen by default, but it is possible with the use of a plugin. The one I like to use is vagrant-hostsupdater.

Install thus:

vagrant plugin install vagrant-hostsupdater

When you bring up the vagrant machine, it will now automatically add the VM’s name to /etc/hosts.

As /etc/hosts is owned by root (and I hope you aren’t running everything as root), you have to provide sudo access to edit /etc/hosts.

Either you manually enter your sudo password every time you run vagrant up, or you can add some rules to sudoers.

This will work on Ubuntu type systems. Paths to sh and sed may be different on your own system.

Copy/paste the following into /etc/sudoers.d/vagrant and chmod the file to 0440

# vagrant-hostsupdater plugin
Cmnd_Alias VAGRANT_HOSTS_ADD = /bin/sh -c echo "*" >> /etc/hosts
Cmnd_Alias VAGRANT_HOSTS_REMOVE = /bin/sed -i -e /*/ d /etc/hosts
%sudo ALL=(root) NOPASSWD: VAGRANT_HOSTS_ADD, VAGRANT_HOSTS_REMOVE

A similar system can be used if you want to make use of nfs for the file sharing with the VM.

Again, this works for Ubuntu systems, you mileage may vary.

Copy/paste the following into /etc/sudoers.d/vagrant and chmod the file to 0440

# nfs mounting in vagrant
Cmnd_Alias VAGRANT_EXPORTS_ADD = /usr/bin/tee -a /etc/exports
Cmnd_Alias VAGRANT_EXPORTS_COPY = /bin/cp /tmp/exports /etc/exports
Cmnd_Alias VAGRANT_NFSD_CHECK = /etc/init.d/nfs-kernel-server status
Cmnd_Alias VAGRANT_NFSD_START = /etc/init.d/nfs-kernel-server start
Cmnd_Alias VAGRANT_NFSD_APPLY = /usr/sbin/exportfs -ar
Cmnd_Alias VAGRANT_EXPORTS_REMOVE = /bin/sed -r -e * d -ibak /tmp/exports
%sudo ALL=(root) NOPASSWD: VAGRANT_EXPORTS_ADD, VAGRANT_NFSD_CHECK, VAGRANT_NFSD_START, VAGRANT_NFSD_APPLY, VAGRANT_EXPORTS_REMOVE, VAGRANT_EXPORTS_COPY

You will now be able to use nfs without having to enter your sudo password on each vagrant up and vagrant halt.
 


Viewing all articles
Browse latest Browse all 13

Trending Articles