Vagrant Public Network issues

I recently faced an issue while testing Vagrant on Virtualbox, to run a webserver. I am assuming that you know what I am talking about, that you know what is a Web server, a Virtual machine, a Pc, Vagrant etc..

GOAL

We want to ‘simply’ visit the public folder of our web server from another device connected to our network.

The Environment

Host Operative system: Linux Ubuntu 16.04

Virtualization Provider: Virtualbox version 5.1.30

Guest OS: Ubuntu 14.04 (Scotch box)

Vagrant version: 2.0.0

VagrantBox: Scotchbox

 

The Configuration:

I have used the default configuration for this test server, please note that the lines that start with an # are comments, some are the default comments of the Vagrant file and others are the basic configuration for the private network settings that we can find in the Vagrant documentation.

Server_test/Vagrantfile

Vagrant.configure(2) do |config|
# The most common configuration options are documented and commented below.
# For a complete reference, please see the online documentation at
# https://docs.vagrantup.com.

# Every Vagrant development environment requires a box. You can search for
# boxes at https://atlas.hashicorp.com/search.
config.vm.box = "scotch/box"

# Disable automatic box update checking. If you disable this, then
# boxes will only be checked for updates when the user runs
# `vagrant box outdated`. This is not recommended.
# config.vm.box_check_update = false

# Create a forwarded port mapping which allows access to a specific port
# within the machine from a port on the host machine. In the example below,
# accessing "localhost:8080" will access port 80 on the guest machine.
config.vm.network "forwarded_port", guest: 80, host: 8082
config.vm.network "forwarded_port", guest: 3306, host: 3307

# Create a private network, which allows host-only access to the machine
# using a specific IP.
# config.vm.network "private_network", ip: "192.168.1.2"
# config.vm.network "private_network", type:"dhcp"




# Create a public network, which generally matched to bridged network.
# Bridged networks make the machine appear as another physical device on
# your network.
config.vm.network "public_network"

# Share an additional folder to the guest VM. The first argument is
# the path on the host to the actual folder. The second argument is
# the path on the guest to mount the folder. And the optional third
# argument is a set of non-required options.
config.vm.synced_folder "~/Code", "/var/www/public/"
# Provider-specific configuration so you can fine-tune various
# backing providers for Vagrant. These expose provider-specific options.
# Example for VirtualBox:
#
config.vm.provider "virtualbox" do |vb|
# # Display the VirtualBox GUI when booting the machine
vb.gui = false
#
# # Customize the amount of memory on the VM:
vb.memory = "4096"
end
#
# View the documentation for the provider you are using for more
# information on available options.

# Define a Vagrant Push strategy for pushing to Atlas. Other push strategies
# such as FTP and Heroku are also available. See the documentation at
# https://docs.vagrantup.com/v2/push/atlas.html for more information.
# config.push.define "atlas" do |push|
# push.app = "YOUR_ATLAS_USERNAME/YOUR_APPLICATION_NAME"
# end

# Enable provisioning with a shell script. Additional provisioners such as
# Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the
# documentation for more information about their specific syntax and use.
# config.vm.provision "shell", inline: <<-SHELL


# sudo apt-get update
# sudo apt-get install -y apache2
# SHELL
end

The issues

When the Virtual machine is started via the vagrant up command and the Vagrantfile is configured as described above, the first command fails and returned this message:

~/VirtualBox VMs/Server_test$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Checking if box 'scotch/box' is up to date...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
 default: Adapter 1: nat
 default: Adapter 2: bridged
==> default: Forwarding ports...
 default: 80 (guest) => 8082 (host) (adapter 1)
 default: 3306 (guest) => 3307 (host) (adapter 1)
 default: 22 (guest) => 2222 (host) (adapter 1)
There was an error while executing `VBoxManage`, a CLI used by Vagrant
for controlling VirtualBox. The command and stderr is shown below.

Command: ["modifyvm", "b669cd35-d3f1-4cd3-bb86-bd72c961f58a", "--natpf1", "tcp8082,tcp,,8082,,80", "--natpf1", "tcp3307,tcp,,3307,,3306", "--natpf1", "ssh,tcp,127.0.0.1,2222,,22"]

Stderr: VBoxManage: error: A NAT rule of this name already exists
VBoxManage: error: Details: code NS_ERROR_INVALID_ARG (0x80070057), component NATEngineWrap, interface INATEngine, callee nsISupports
VBoxManage: error: Context: "AddRedirect(Bstr(strName).raw(), proto, Bstr(strHostIp).raw(), RTStrToUInt16(strHostPort), Bstr(strGuestIp).raw(), RTStrToUInt16(strGuestPort))" at line 1820 of file VBoxManageModifyVM.cpp

And the VM won’t start. Most of the time I have probably done something else, probably searched the impossible but not sure if I have tried again the same command.. but in case if you do, you pass the next step where actually the Virtual Machine starts like so:

~/VirtualBox VMs/Server_test$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Checking if box 'scotch/box' is up to date...
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
default: Adapter 1: nat
default: Adapter 2: bridged
==> default: Forwarding ports...
default: 80 (guest) => 8082 (host) (adapter 1)
default: 3306 (guest) => 3307 (host) (adapter 1)
default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Running 'pre-boot' VM customizations...
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
default: SSH address: 127.0.0.1:2222
default: SSH username: vagrant
default: SSH auth method: private key

Until it reaches the Remote connection Warning message and If you don’t panic like I did and just wait, quite a while, that the terminal throws the following warning message repetitively for few minutes …

 default: Warning: Remote connection disconnect. Retrying...
 default: Warning: Connection reset. Retrying...
 default: Warning: Remote connection disconnect. Retrying...
 default: Warning: Connection reset. Retrying...
 default: Warning: Remote connection disconnect. Retrying...

The workaround

Patience. If we open VirtualBox and click to show the machine screen we see that the machine is still booting up but seems stuck on the network settings and it’s reconfiguring them.

It should appear a message telling that the machine is waiting 60 more seconds to configure the network ..it may take a while, then the machine boots and the terminal where vagrant  was running will finally look like this:

default: Warning: Remote connection disconnect. Retrying...
default: Warning: Connection reset. Retrying...
default: Warning: Remote connection disconnect. Retrying...
default: Warning: Connection reset. Retrying...
default: Warning: Remote connection disconnect. Retrying...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
default: The guest additions on this VM do not match the installed version of
default: VirtualBox! In most cases this is fine, but in rare cases it can
default: prevent things such as shared folders from working properly. If you see
default: shared folder errors, please make sure the guest additions within the
default: virtual machine match the version of VirtualBox you have installed on
default: your host and reload your VM.
default: 
default: Guest Additions Version: 4.3.28
default: VirtualBox Version: 5.1
==> default: Configuring and enabling network interfaces...
==> default: Mounting shared folders...
default: /vagrant => /home/fabio/VirtualBox VMs/Server_test
default: /var/www/public => /home/fabio/Code
==> default: Machine already provisioned. Run `vagrant provision` or use the `--provision`
==> default: flag to force provisioning. Provisioners marked to run always will still run.

 

In VirtualBox, we could configure a bridge connection between the guest machine and the host and set the first adapter eth0 to the bridged connection and that actually would work fine.

But it won’t work if we want to use vagrant to boot our virtual machine.

I have tried to set a simple configuration as described inside the Vagrant documentation –  one adapter with a static IP for the private network was my first choice when I tried the minimal default setting as shown before in the Vagrantfile.

Actually what worked has been:

Open the VirtualBox and click show to start the virtual machine monitor and wait until the machine was completely started.

Then either ssh into via vagrant ssh or login via the machine screen to check the ip address of the machine using

ifconfig

Then, you should be able to visit the IP address and find your folders.

like so: