2

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.

5
  • Don't you have a subnet conflict? What networks do you use on WiFi and Wired networks?
    – Glueon
    Oct 16, 2014 at 12:33
  • i don't think i have a conflict? nothings complained about the IP as far as I'm aware... I don't really want to use it on another network, I just want it to come back when I return to my wired connection. It'd be great if it could work on both of course, with a static ip. I'd be totally fine with having to run a command to fix it up when switching networks, if need be....
    – Brad Parks
    Oct 16, 2014 at 12:35
  • 2
    I mean that if you use a network 10.11.12.0/24 for your wired network when you plug it in packets to your VPS 10.11.12.13 may go not to the vbonxnet interface but to your network card.
    – Glueon
    Oct 16, 2014 at 12:37
  • ok... good to know! i don't know much about networking, and I just picked an IP from the private networking space that I thought would be easy to remember. So should I use something that starts with 192.168.? instead? Do you think this is the problem?
    – Brad Parks
    Oct 16, 2014 at 12:45
  • 1
    It may be a problem. You can post ifconfig output with WiFi and a network card enabled. Yes, you can try to change network to something like 192.168.63.0/24. Like config.vm.network "private_network", ip: "192.168.63.12", netmask: "255.255.255.0"
    – Glueon
    Oct 16, 2014 at 12:51

2 Answers 2

6

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
1
  • 1
    ADAPTERS=${1:-"vboxnet1"} is shorter and works in all POSIX shells as far as I know
    – user158140
    Mar 3, 2015 at 13:58
1

ssh into your vagrant virtual machine and fire the following command

sudo /etc/init.d/network restart

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .