How to Automatically Add Hosts to Vagrant Without a Password on OS X

UPDATE 2016-07-27: I do not believe this is working with the current version of vagrant-hostmanager 1.2.0. Will update if I find a resolution.

There’s an excellent plugin for vagrant called vagrant-hostmanager which will automatically add entries to your hosts file for domain aliases used in your vagrant instance. However, you’ll get a password prompt every time it runs as editing /etc/hosts requires elevated privileges. The instructions below allow you to run the hostupdater without having to enter your password every time.

The vagrant-hostmanager repo provides these instructions, but I’ve added additional information if you haven’t dealt with visudo before.

Be super careful when editing the sudoers file because editing it incorrectly can lock you out of your computer and prevent you from editing files!

  • Open Terminal
  • Check your $EDITOR env variable: echo $EDITOR
  • If it’s subl -w (for Sublime users) or anything that’s not nano, vi, or vim, you will need to use the longer version of the command below.
  • Short Version: sudo visudo
  • Long Version: sudo EDITOR=nano visudo
  • This opens the sudoers file for editing, which should look like this: http://opensource.apple.com/source/sudo/sudo-60/src/sudoers
  • If the file opens in Sublime Text or is empty, stop what you’re doing, otherwise proceed.
  • Near the bottom of the file, add these two lines, replacing <YOUR_USERNAME> with your OS X username:
Cmnd_Alias VAGRANT_HOSTMANAGER_UPDATE = /bin/cp /home/<YOUR_USERNAME>/.vagrant.d/tmp/hosts.local /etc/hosts
%sudo ALL=(root) NOPASSWD: VAGRANT_HOSTMANAGER_UPDATE

The next time you vagrant up or vagrant halt you shouldn’t be asked to provide your password. It will work for both command line vagrant use and a tool like Vagrant Manager.

How to Easily Increase Memory on your Vagrant Virtual Machine

This assumes you are using VirtualBox as your provider, which you are if you’re using a pre-built vagrant configuration like I am with VVV.

I was running out of RAM and crashing my VM while trying to import a 9MB WXR file into a WordPress install.

First, I tried adding a swapfile so I could get some virtual memory. I was able to do that with these instructions from Digital Ocean, but I wasn’t sure how to make it persistent after a vagrant destroy and didn’t want to have to set this up every time I booted up the VM.

Finally, I found this thread in the issues for VVV with the answer.

Create a Customfile in the same directory as your Vagrantfile, and add the contents below, changing 2048 to be whatever value you want to increase your VM’s RAM to.

config.vm.provider :virtualbox do |v|
  v.customize ["modifyvm", :id, "--memory", 2048]
end

Save the file, and then do a vagrant reload and you should be able to verify with top or free -m that you now have some additional memory in the VM.

I went with 2GB. There has been some discussion of increasing the default in VVV from 512MB, however I think that would be an issue for people on laptops, especially running MacBook Air’s with only 4GB of RAM.