If you are seeing Vagrant’s Warning: Connection timeout. Retrying... when you start your vm on one network, but not when you start it on another, I may have the solution.
TL;DR
VirtualBox configures its NAT to use 10.0.2.x by default. It may be conflicting with your network if you’re on the same IP range.
Symptoms
- Your vm starts up and works
- You see the
Connection timeoutwarning and vagrant doesn’t mount your shared directories - Your shared directories are mounted when your computer is connected to one network, but not when connected to another
- You’re using VirtualBox as your provider
Background
I was experiencing these symptoms on my laptop. When I’d start my Ubuntu VM while connected to my home network, everything worked as expected. When I’d take my laptop to the office and attempt to start the VM there, I’d receive multiple connection warnings, the VM would start, but vagrant would not mount the shared folders.
Each week, during my office visit, I’d spend an hour (or more) searching the web looking for an answer. Nothing seemed to work, until I stumbled across a statement that mentioned VirtualBox’s default IP range for a NAT connection was 10.0.2/24 (10.0.2.0 – 10.0.2.255).
That IP range looks familiar. It happens to be the IP range that I use at my office.
Coincidence? I think not!
What if I’m seeing a conflict where the NAT address is set to something like 10.0.2.10, then vagrant attempts to SSH to 10.0.2.10 and a workstation on the network (NOT my VM) simply ignores the SSH connection request, because, well, it doesn’t accept SSH? I’d probably see a Connection timeout, right?
A little more research led me to VirtualBox’s Fine-tuning the VirtualBox NAT engine page which states:
In NAT mode, the guest network interface is assigned to the IPv4 range 10.0.x.0/24 by default where x corresponds to the instance of the NAT interface +2. So x is 2 when there is only one NAT instance active. In that case the guest is assigned to the address 10.0.2.15, the gateway is set to 10.0.2.2 and the name server can be found at 10.0.2.3.
If, for any reason, the NAT network needs to be changed, this can be achieved with the following command:
VBoxManage modifyvm “VM name” –natnet1 “192.168/16”
This command would reserve the network addresses from 192.168.0.0 to 192.168.254.254 for the first NAT network instance of “VM name”. The guest IP would be assigned to 192.168.0.15 and the default gateway could be found at 192.168.0.2.
Ah hah!
Fixing the Problem
Vagrant provides support for modifying VirtualBox machines using the customize command.
I made some changes to my Vagrantfile.
Before:
|
1 2 3 4 5 6 7 8 9 10 |
config.vm.provider "virtualbox" do |vb| # Display the VirtualBox GUI when booting the machine vb.gui = true # Customize the amount of memory on the VM: #vb.memory = "1024" end |
And after:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
config.vm.provider "virtualbox" do |vb| # Display the VirtualBox GUI when booting the machine vb.gui = true # Customize the amount of memory on the VM: #vb.memory = "1024" # By default, the NAT network is located at 10.0.2.0/16. If the local # network is using this range, vagrant ssh will be unable to connect, # thus preventing shared folders from being configured. # 192.168.100/24 means addresses will be assigned in the # range 192.168.100.0 - 192.168.100.255 (ie. 24 bits are locked down). vb.customize ["modifyvm", :id, "--natnet1", "192.168.100/24"] end |
I set the IP range to use 192.168.100/24 because my home network runs on 192.168.1/24 and I didn’t want to end up with a conflict there.
I shut down the VM and restarted it with vagrant up and voila! Vagrant made the SSH connection and configured my shared directories. I tested it again at home with no issues.
Problem solved!
Summary
If Vagrant can’t connect via SSH and you’re on a 10.0.2.x network. The VM’s NAT is creating a conflict when it configures its IP address.
Fix it with VirtualBox’s modifyvm --natnet1 command.
