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:

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

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

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