Network of VirtualBox instances with static IP addresses and Internet access.

Virtual network

Professional developers know they must test their code before releasing it. It is really kind of obvious nowadays! But then why not also testing your deployment scripts before using them in production?

Ideally, you would test the deployment on a controlled environment. If you’re working on an enterprise project, chances are the company provided you with such an environment. But if you are developing a pet project at home, you may not have that luxury.

We still can make this work, though. Thanks to virtualization technologies such as VirtualBox we can create such an environment in the comfort of your development machine.
We need the instances to be able to talk to each other, so we’ll probably have to assign static IP addresses to them. We also need them to be accessible from the host if we want our Ansible playbooks to work, for example. And we’ll probably need to ensure they can access the Internet, to install packages as request by the scripts.

So let’s see how you can setup and configure a network of VirtualBox instances, with static IP addresses and Internet access.

VirtualBox configuration

For this example we’re going to create two VirtualBox instances with Ubuntu Server 16.04 on them. OSBoxes.org conveniently provides VDI image files ready to be imported by VirtualBox, but you’re free to install Ubuntu Server from scratch if you feel inclined to.

Start by creating a new instance:

VirtualBox instance creation using an existing VDI

Before we proceed to configuring each virtual machine, we must create a host-only network. The machines will use this network to communicate with each other. Simply access the Host Network Manager from VirtualBox’s main window (File… Host Network Manager) and click on that “Create” button. You should now see an entry, probably named “vboxnet0“. Since we will assign static IP addresses to those instances, you can disable the DHCP server for that network.

VirtualBox's host network manager showing a new host-only adapter

Now let’s move to configuring each of the virtual machines we created.
In the instance’s Settings… Network, make sure the Adapter 1 is attached to NAT. This will provide the Internet access to the virtual machine.
Also enable the Adapter 2 and attach it to a Host-only Adapter. Normally VirtualBox will automatically fill the adapter’s name with the “vboxnet0” network we created here above.

VirtualBox network settings showing a host-only configured adapter

Click on “OK” to save your settings and repeat the process for the second machine, using the same settings.

Port-forwarding

Port-forwarding easily enables you to access a virtual instance through a specific port.

Say for example that your Ansible script needs to access port 22 of your machine in order to work on that Ubuntu Server. We can simply tell VirtualBox to map a local port, such as 4001, to port 22 on the virtual instance. So when we type ssh osboxes@127.0.0.1 -p 4001 we will in fact tunnel to the machine’s port 22!

To configure that, simply go back to the instance’s network settings, access the Advanced options of the NAT adapter and click on “Port-Forwarding“. Then create a new rule which you can name “ssh”, set the Host Port to 4001 and the Guest Port to 22. That’s it!

An example of port-forwarding configuration

Repeat the process for the second machine.

Static IP addresses

Let’s start both machines right now and login (for OSBoxes’ images, username is usually “osboxes” and password is “osboxes.org”).

You can easily verify that the virtual instances can access the outside world, for example by typing ping codesandnotes.be. And you should normally be able to reach the machines from your host by opening a terminal and using SSH as explained above.

However, you may have trouble making both instances ping/talk to each other. This is because only one interface is configured in Ubuntu Server: the one from Adapter 1, which is the NAT adapter. We still need to configure the second network interface to point at our”vboxnet0″ host-only network adapter.

To do that, in your Ubuntu Server, type sudo nano /etc/network/interfaces and edit the file to make it look like this:

source /etc/network/interfaces.d/*

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface (that's the NAT)
auto enp0s3
iface enp0s3 inet dhcp

# The secondary network interface (that's the host-only network)
auto enp0s8
iface enp0s8 inet static
  address 192.168.56.101
  netmask 255.255.255.0

With this configuration we enabled the secondary network interface, named “enp0s8” in Ubuntu, which is attached to our configured host-only network. We also assigned it a static IP: 192.168.56.101
The address stems from the IPv4 address configured for our “vboxnet0” adapter, which is 192.168.56.1 with a mask of 255.255.255.0. This means we can assign addresses from 192.168.56.2 upwards to our VirtualBox instances.

Okay time’s out: where did those enp0s3 and enp0s8 bits come from? As pointed out by “winter” in the comment below, those are the network interface names known to the OS. You can run ifconfigto display a list of those. If you do that before editing that “interfaces” configuration, you should see that one of the interfaces has an IP: that’s the one matching the NAT adapter we have set up in the Virtual Box’s Network options. The other interface will be the one we need to set up the host-only network.
Another useful trick is to ping a website by specifying the interface we want to use: ping -I enp0s3 codesandnotes.be. This way you can figure out which network interface provides Internet access.

Do the same for the second machine you set up: give it 192.168.56.201 as the static address, for example.

Now restart both virtual instances to take those network changes into account, and do an ifconfig to make sure the static addresses have been correctly assigned.

I like it when a plan comes together

If all went according to plans, should now be able to:

  1. Access your VirtualBoxes from the host with ssh, using the ports configured to forward to the instances’ port 22
  2. Attain the web from inside the virtual instances
  3. Reach one virtual machine from the other one, using the configured static IP addresses

Not bad, right?

Cheers!

10 comments

  1. Roberts Reply
    01/03/2019 at 17:54

    Hello, somehow i doesnt get it working. By looking at ifconfig inet configurations for both machines looks identical. Restarted network many times. Can you help please?

    • Diego Reply
      01/03/2019 at 18:33

      Hi there!

      Hum, out of my head I can’t tell you what may have gone wrong… Let’s see:

      • Have you created the host-only network so that both VMs can talk to each other? Make sure you disable the DHCP servers (not shown in the post’s screenshots)!
      • Double-check that the network adapters are correctly configured for each : Adapter 1 should be set to NAT, Adapter 2 to Host-only Adapter AND the name of the selected network should be the same than the one from the host-only network.
      • Make sure the Ubuntu network interface configurations are similar, exception made of the static IP address!
      • After saving the network interfaces, restart the Ubuntu VMs and try to ping VM2 from VM1 and VM1 from VM2.<:li>

      If all fails, try to start from the beginning by using the exact same IP addresses I wrote in the post: I can’t remember how many times I’ve made a mistake in one IP or the other.

      Let me know how it goes!
      (but don’t forget to post some more details so we can figure out what may be missing 😉 )

  2. Josue Reply
    03/07/2019 at 02:53

    Thank you Diego

    • Diego Reply
      03/07/2019 at 12:13

      You’re welcome!

  3. Patrick Wang Reply
    04/08/2019 at 00:02

    I have attempted to follow your instruction for Windows OS Guest. May I ask what is the Default Gateway and DNS Server to be used for the Guest (192.168.56.101 and 192.168.56.102) ?

    Thanks

  4. David Reply
    28/08/2019 at 13:17

    Very helpful; thanks. Although more recent Ubuntu versions (at least 18.04) have switched to using NetPlan and I had to merge your guide with this: https://www.tecmint.com/configure-network-static-ip-address-in-ubuntu/

    • Diego Reply
      28/08/2019 at 18:45

      Hi David,
      Yes, you’re right: I’m a bit behind with the Ubuntu Server version 😉
      Thanks for the link!

  5. IT.Competence Reply
    12/09/2019 at 18:40

    thanks

  6. winter Reply
    27/09/2019 at 21:19

    Thanks man, very informative.
    You should add that enp0s8 name was taken from the interface names, e.g: running ifconfig will show you all your interfaces, one of them will have an IP, and it enp0s3 – this is the NAT with access to the web, and the other is enp0s8 which doesn’t have IP, and this is the one we want to configure.
    If someone doesn’t understand, just copy-paste will not let him have a very good idea what he is doing at that specific part, all the rest of the parts were just great! =)

    • Diego Reply
      28/09/2019 at 09:40

      Hi!
      That’s absolutely correct, thanks for pointing that out!
      In fact I’m going to add a paragraph directly in the article to clarify that 😉
      Cheers to you!

Leave A Comment

Please be polite. We appreciate that. Your email address will not be published and required fields are marked

This site uses Akismet to reduce spam. Learn how your comment data is processed.