Vagrant private IP not found after network change

mac-osxprivate-ipvagrantvirtualbox

I've got a simple Vagrant Ubuntu virtual machine setup using Virtual Box, running on a Mac.

I'm using private networking, configured with a static private IP like so:

Vagrant.configure("2") do |config|
    config.vm.network :private_network, ip: "10.11.12.13"
end

This works fine, except for when I switch back and forth between wifi and my wired network. When I do, I can't seem to connect to the VM with it's static IP address anymore. I've tried halting and restarting the VM, and even destroying and recreating the VM, to no avail.

Is there some way to reset the network connection so the private IP comes back? I'm fine with directly accessing it in VirtualBox, or whatever. The only way I can make it work it appears is to actually reboot my entire machine right now.

Best Answer

It seems that the network adapter for VirtualBox can be restarted easily at the command prompt by running something like the following (will be a little different on your system)

sudo ifconfig vboxnet1 down
sudo ifconfig vboxnet1 up

Note that you'll have to replace vboxnet1 with whatever your virtual network adapter is for VirtualBox, which you can find out by:

  • Starting Virtual Box
  • Selecting the VM you're running
  • Look at the Network section. You'll see an Adapter listed that has vboxnet in it's name. That's the adapter name you're looking for.

You can also see what network adapters you have available by just running ifconfig in a terminal:

ifconfig

If you'd like this wrapped up in a simple bash script, you can use the following:

restart_vbox_network_adapter.sh

ADAPTER=${1:-"vboxnet1"}

sudo ifconfig $ADAPTER down
sudo ifconfig $ADAPTER up

and you'd call it like so, once again using the correct network adapter for your VM:

restart_vbox_network_adapter.sh vboxnet2