The context is: Windows laptop running Virtual Box. I have received a VM from a colleague that contains an Ubuntu (16.04 LTS ) based environment. The VM runs nicely. However, I do not know how to reach it from my laptop. The web application for example that runs inside, and that I can work with in Firefox inside the VM, should also be accessible from the browsers on my laptop. But it currently is not. In this article I will show the steps I took to establish the network connection from host to client.
My main sources of information were:
- A short guide to networking in Virtual Box with Oracle Linux inside – by Job Oprel (AMIS Technology Blog)
- Chapter on Network Configuration in the Official Ubuntu Documentation
The steps I went through:
- define Host Only Ethernet Adapter in Virtual Box on my laptop; note: this adapter is not specific to the VM
- enable a network adapter for the specific VM of type Virtual Box Host Only Ethernet Adapter, based on the adapter configured in the previous step
- start Ubuntu VM
- temporarily configure an IP address for the network interface associated with this wired connection (as super user, using ifconfig); note: the IP address can also be configured as a static assignment to the network interface
- ping the VM from the laptop (and do a little whooping when it works)
It is perfectly possibly that I am not doing this the smartest way. I got it to work, I used advise from experts and I added my own little lack of aptitude with both Virtual Box, Networking and Ubuntu. So this will probably help you to get it to work – and perhaps further promote a suboptimal approach. For that, I am sorry.
Define Host Only Ethernet Adapter in Virtual Box on my laptop
In the VirtualBox client, open menu option File | Preferences | Network and open the tab Host-only networks. Create a new Virtual Box Host Only Ethernet Adapter (in my case #6). Set an IP address in the range in which you want to assign an IP address to your VM. I have set 192.168.188.100 and I will assign the IP address 192.168.188.101 to the VM client. Do not define a DHCP Server on the second tab.
Note: this adapter is not specific to the VM. It is defined on the host and can be associated with or enabled in each of your VMs, though probably not to multiple VMs that are running simultaneously. If you check on your host, you will find evidence of this new ethernet adapter:
Enable a network adapter for the specific VM of type Virtual Box Host Only Ethernet Adapter
( based on the adapter configured in the previous step)
Select the VM in the Virtual Box client – before it is started. Press Settings. Click on Network. Open one of the currently unconfigured Adapter tabs. Select the Host Only Ethernet Adapter that was created in the previous step, #6 for me. Check Enable the Network Adapter. Make sure that the checkbox Cable Connected is checked.
At this point, you can start the Virtual Machine.
Temporarily configure an IP address for the network interface associated with this wired connection
Open a terminal window. Switch to super user mode: sudo su. Using ifconfig verify the state of the network interfaces and determine the name of the network interface associated with the Network Adapter configured in Virtual Box for the Host-Only Adapter. In my case, this would be the second network interface in the Ubuntu VM.
The logical name of the network interface I need to configure is enp0s8. The statement I can use to temporarily configure IP address 192.168.188.101 for it is:
ifconfig enp0s8 192.168.188.101 netmask 255.255.255.0
Execute that command. Then check with ifconfig what the status is of the network interface:
For verification you can check in the GUI as well:
Open the network section of the System Settings:
And check the status of the second wired connection:
After restarting the VM, this IP configuration will be lost – it is a temporary assignment. The IP address can also be configured as a static assignment to the network interface. To configure your system to use a static IP address assignment, add the static method to the inet address family statement for the appropriate interface in the file /etc/network/interfaces.
Edit the file, adding the following section:
# My static IP configuration for the VirtualBox Host Only Adapter
iface enp0s8 inet static
And save the file.
Now when the VM is restarted, the configuration of network interface enp0s8 will already be active.
Ping the VM from the laptop
(and do a little whooping when it works)
Finally, we establish if the VM is indeed accessible from the host. Open a command line window on the host and use good old ping to verify the connection:
It can be helpful to assign a logical host name to the VM (and more specifically, to the VMs IP address). This is done on Windows by adding an entry to the hosts file: C:\Windows\System32\drivers\etc\hosts:
Here we assign the logical name ubuntu to the VM. After saving the file, we can do fun stuff such as ping ubuntu:
And use ubuntu as logical host name when we access web applications and services running in the VM, for example from the browser: