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:
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.
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.
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!
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 ifconfig
to 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.
And now remove port-forwarding
**Edouard** rightfully points out that, after the static IP has been set on your instances, you can actually remove the port-forwarding entry in VirtualBox. In this tutorial for instance, it means you should now access your VM with ssh osboxes@195.168.56.101
. Neat!
I like it when a plan comes together
If all went according to plans, should now be able to:
- Access your VirtualBoxes from the host with ssh
- Attain the web from inside the virtual instances
- Reach one virtual machine from the other one, using the configured static IP addresses
Not bad, right?
Cheers!
15 comments
Roberts
01/03/2019 at 17:54Hello, 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
01/03/2019 at 18:33Hi there!
Hum, out of my head I can’t tell you what may have gone wrong… Let’s see:
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 😉 )
Josue
03/07/2019 at 02:53Thank you Diego
Diego
03/07/2019 at 12:13You’re welcome!
Patrick Wang
04/08/2019 at 00:02I 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
David
28/08/2019 at 13:17Very 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
28/08/2019 at 18:45Hi David,
Yes, you’re right: I’m a bit behind with the Ubuntu Server version 😉
Thanks for the link!
IT.Competence
12/09/2019 at 18:40thanks
winter
27/09/2019 at 21:19Thanks 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
28/09/2019 at 09:40Hi!
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!
Edouard
05/01/2020 at 19:16Thanks for this very useful tutorial!
And don’t forget to remove the port forwarding you added on the NAT for SSH connection, it is not useful anymore 😉
Diego
05/01/2020 at 20:21Thanks Edouard!
I didn’t know about the port-forwarding thing… I really should freshen up this thing, possibly using the latest version of Ubuntu Server! One more “to do” I’m adding to my list of things for 2020 😉
Cheers!
Edouard
06/01/2020 at 00:22I just wanted to point out the fact that what you first does in your tutorial (setting the port forwarding in the NAT configuration) in order to be able to connect via SSH to the machine, so that you can setup the static IP is not longer needed afterward, given that you can connect via the new (static) IP and the 22 port directly!
Cheers
Diego
10/01/2020 at 10:18Indeed. I just hoped to do a “big update” of this post, but in the end your remark is so useful I just added it to the post right now! Ubuntu server 18 and netplan will wait 😉
Thanks again for pointing that out!
binu
21/04/2020 at 22:32Thanks for the blog. Have a query which i am unable to solve. Could you help?
Have 2 windows machines (with IPs in range 192.168.0.*)
Virtualbox installed in 1 machine and have created a CentOS VM, Bridged Adapter used for network, which is given the static IP address 192.168.0.14. With this configuration, am able to access the CentOS VM from both the Windows machines. Also able to connect to the internet.
However, if I change the static IP address to 192.168.5.14, am unable to access the VMs from both windows machines. Also, unable to access internet from VM.
Any help to correct this?