Starting Vagrant VM on host boot

Shrikant Patnaik picture Shrikant Patnaik · Aug 7, 2013 · Viewed 12.4k times · Source

I am new to vagrant and have set up a couple of vagrant vm's which I use for development and testing purposes. My issue is that I cant get these boxes to start up automatically on my machine (Ubuntu). It is really annoying to go to the folder and vagrant up each machine every time my host machine starts up.

I have tried adding a cronjob that looks like cd path/to/vm/folder && vagrant up but this does not seem to be working.

I also tried a cronjob for VBoxManage but vagrant changes the name of the VM (rather the number/version 'vmname_version') everytime the VM boots up.

Answer

Terry Wang picture Terry Wang · Aug 7, 2013

Cron job doesn't fit into this use case, it is for scheduled jobs.

As you are running Ubuntu as host, I'd recommend using /etc/rc.local, place the commands in the rc.local script which is executed at the end of the init process.

It'll look like this

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.    
cd /path/to/vagrant_vm1 && vagrant up
cd /path/to/vagrant_vm2 && vagrant up
exit 0

NOTE: There will be port conflicts on the host if you start more than 1 Vagrant boxes with the same networking mode - NAT (default), which by default use the same port forwarding rule => guest 22 to host 2222.

If you need to start more than 1 boxes (NAT), consider using public network (bridged) or use VBoxManage controlvm to start the VMs, refer to the answer in Two separate Vagrant machines, windows host, PuTTY - how?