Network access to Ubuntu Virtual Box VM from host laptop

1

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:

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.

image

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:

image

 

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.

image

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.

image

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:

image

For verification you can check in the GUI as well:

Open the network section of the System Settings:

image

And check the status of the second wired connection:

image

 

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.

image

Edit the file, adding the following section:

# My static IP configuration for the VirtualBox Host Only Adapter
auto enp0s8
iface enp0s8 inet static
address 192.168.188.101
netmask 255.255.255.0

image

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:

ping 192.168.188.101

image

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:

image

Here we assign the logical name ubuntu to the VM. After saving the file, we can do fun stuff such as ping ubuntu:

image

And use ubuntu as logical host name when we access web applications and services running in the VM, for example from the browser:

image

About Author

Lucas Jellema, active in IT (and with Oracle) since 1994. Oracle ACE Director and Oracle Developer Champion. Solution architect and developer on diverse areas including SQL, JavaScript, Kubernetes & Docker, Machine Learning, Java, SOA and microservices, events in various shapes and forms and many other things. Author of the Oracle Press book Oracle SOA Suite 12c Handbook. Frequent presenter on user groups and community events and conferences such as JavaOne, Oracle Code, CodeOne, NLJUG JFall and Oracle OpenWorld.

1 Comment

  1. This doesn’t work, there is no host Only Ethernet Adapter in File | Preferences Network menu. There is only NAT network there. The version is 5.2.2

Leave a Reply

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