How to Configure Network Between Guest VM and Host in Oracle VirtualBox

Once you have installed different operating systems in Oracle VirtualBox, you may want to enable communication between the host and the virtual machines.

In this article, we will describe the simplest and direct method of setting up a network for guest virtual machines and the host in Linux.

For the purpose of this tutorial:

  1. Host Operating System – Linux Mint 18
  2. Virtual Machine OS – CentOS 7 and Ubuntu 16.10

Requirements

  1. A working Oracle Virtualbox installed on Host machine.
  2. You must have installed a guest operating system such as Ubuntu, Fedora, CentOS, Linux Mint or any of your choice in the Oracle virtual box.
  3. Power off the virtual machines as you perform the configurations up to the step where your required to turn them on.

In order for the guest and host machines to communicate, they need to be on the same network and by default, you can attach up to four network cards to your guest machines.

The default network card (Adapter 1) is normally used to connect the guest machines to the Internet using NAT via the host machine.

Important: Always set the first adapter to communicate with the host and the second adapter to connect to the Internet.

Create a Network For Guests and Host Machine

At the Virtualbox manager interface below, start by creating a network on which the host and guests will operate.

Go to File –> Preferences or hit Ctrl + G:

Virtualbox Preferences Window
Virtualbox Preferences Window

From the following interface, there are two options; choose Host-only Networks by clicking on it. Then use the + sign on the right to add a new host-only network.

Set Guest Network
Set Guest Network

Below is a screen shot showing a new host-only network has been created called vboxnet0.

Virtualbox Preferences Window
Virtualbox Preferences Window

If you want, you can remove it by using the - button in the middle and to view the network details/settings, click on the edit button.

You can as well change the values as per your preferences, such as the network address, network mask, etc.

Note: The IPv4 address in the interface below is the IP address of your host machine.

Host Network Details
Host Network Details

In the next interface, you can configure the DHCP server that is if you want the guest machines to use a dynamic IP address (make sure it is enabled before using it). But I recommend using a static IP address for the virtual machines.

Now click OK on all network settings interfaces below to save the changes.

Set Guest Static IP aAddress
Set Guest Static IP aAddress

Configure Virtual Machine Network Settings

Note: You can follow the steps below for every virtual machine that you want to add on the network to communicate with the host machine.

Back at the virtual box manager interface, select your guest virtual machine such as Ubuntu 16.10 server or CentOS 7 and click on the Settings menu.

Configure VM Settings
Configure VM Settings

Configure Adapter to Connect Virtual Machine to Host

Choose the Network option from the interface above. Afterwards, configure first network card (Adapter 1) with the following settings:

  1. Check the option: “Enable Network Adapter” to turn it on.
  2. In the field Attached to: select Host-only Adapter
  3. Then select the Name of the network: vboxnet0

As in the screen shot below and click OK to save the settings:

Enable Network Adapter for Guest VM
Enable Network Adapter for Guest VM

Configure Adapter to Connect Virtual Machine to Internet

Then add a second network card (Adapter 2) to connect virtual machine to the Internet via the host. Use the settings below:

  1. Check the option: “Enable Network Adapter” to activate it.
  2. In the field Attached to: select NAT
Enable Network Adapter for VM
Enable Network Adapter for VM

Setup Static IP Address for Guest Virtual Machine

At this stage, power on the guest virtual machine, login and configure static IP address. Run the command below to show all the interfaces on the guest machine and allocated IP addresses:

$ ip add
Configure Static IP Address for VM
Configure Static IP Address for VM

From the screen shot above, you can see that there are three interfaces enabled on the virtual machine:

  1. lo – loopback interface
  2. enp0s3 (Adapter 1) – for host-only communication which is using the DHCP as set in one of the previous steps and later configured with a static IP address.
  3. enp0s8 (Adapter 2) – for connection to the Internet. It will use DHCP by default.
On Debian/Ubuntu/Linux Mint

Important: Here, I used Ubuntu 16.10 Server: IP address: 192.168.56.5.

Open the file /etc/network/interfaces using your favorite editor with super user privileges:

$ sudo vi /etc/network/interfaces

Use the following settings for the interface enp0s3 (use your preferred values here):

auto  enp0s3
iface enp0s3 inet static
address  192.168.56.5
network  192.168.56.0
netmask  255.255.255.0
gateway  192.168.56.1
dns-nameservers  8.8.8.8  192.168.56.1

Save the file and exit.

Then restart network services like so:

$ sudo systemctl restart networking

Alternatively, reboot the system and closely, check if the interface is using the new ip addresses:

$ ip add
On RHEL/CentOS/Fedora

Important: For this section, I used CentOS 7: IP address: 192.168.56.10.

Begin by opening the file for enp0s3 – host-only network interface; /etc/sysconfig/network-scripts/ifcfg-enp0s3 using your favorite editor with super user privileges:

$ sudo vi /etc/sysconfig/network-scripts/ifcfg-enp0s3

Create/modify the following settings (use your preferred values here):

BOOTPROTO=static
ONBOOT=yes
IPADDR=192.168.56.10
NETWORK=192.168.56.0
NETMASK=255.255.255.0
GATEWAY=192.168.56.1
DNS=8.8.8.8 192.168.56.1
NM_CONTROLLED=no     #use this file not network manager to manage interface

Save the file and exit. Then restart network service as follows (you can as well reboot):

$ sudo systemctl restart network.service 

Check if the interface is using the new IP addresses as follows:

$ ip add

Manage Virtual Machines From Host Using SSH

On the host machine, use SSH to manage your virtual machines. In the following example, am accessing the CentOS 7 (192.168.56.10)server using SSH:

$ ssh [email protected]
$ who
Connect Guest VM using SSH
Connect Guest VM using SSH

That’s it! In this post, we described a straightforward method of setting up a network between a guest virtual machines and the host. Do share your thoughts about this tutorial using the feedback section below.

If you liked this article, then do subscribe to email alerts for Linux tutorials. If you have any questions or doubts? do ask for help in the comments section.

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Support Us

We are thankful for your never ending support.

63 thoughts on “How to Configure Network Between Guest VM and Host in Oracle VirtualBox”

  1. In the configuration file for the host only adapter why is it necessary to include gateway and dns server entries? Aren’t these only required for communication with outside of the local network?

    Reply
  2. I have all of the above working now, but I can’t hit the webserver which is running on the guest, from the host.

    I get:

    /Users/markchassy/bitbucket/orson_ansible/playbooks
    (venv27)==> curl 192.168.56.10:8080
    curl: (7) Failed to connect to 192.168.56.10 port 8080: Connection refused
    
    Reply
  3. Really clear, but a couple questions/comments:

    1. Is there a reason to set the host only network as the first adapter and the NAT as the second. On Ubuntu, it does not seem to make a difference, but on CENTOS/RHEL it does.

    On VirtualBox, the NAT is usually there by default, so one naturally puts the host only second. If you try to switch the host only to static when the adapters are in that that order, the NAT seems to break.

    If you leave both as dynamic and switch the NAT to second place, the onboot=yes switches to onboot=no.

    You have to change that manually.

    Then you can switch the host-only to static and everything works fine.

    Reply
    • @Mark

      There is actually no difference, you can leave NAT for connecting to the internet on adapter 1 and add a second adapter for host-only connection. And thanks for sharing these useful tips.

      Reply
      • Yes, now that I have deactivated the firewall, I realize that I lost half a day just because of that and that I had my adapters set up correctly before. At least now, I actually understand what I am doing.

        Reply
  4. And after going through all these instructions I still can’t access nginx server that is running on VirtualBox from my host PC. It just gets stuck on connecting. I guess Windows 10 just can’t do it properly.

    Reply
  5. Great article.

    But I have a problem. I want to configure an IP on the virtualbox Centos6.9 instance as follows: 10.156.147.xxx. Its default IP is 10.0.2.15.

    My own network on my pc is which is hosting the VirtualBox software has the IP 192.168.1.xxx.

    I am try and on the Centos6.9 VB IP to 10.156.147.xxx and try and restart the network and it says Ok but the IP is not changed its still 10.0.2.15

    Reply
    • @Richard

      Take not that you need to enable two network interfaces for your guest; one will be used to connect to the internet which should have the IP address 10.0.2.15 by default and another for host-only connection which will probably have the IP address 192.168.1.xxx.

      Follow the steps at the start of the article carefully for this to work i.e create a network for create a for guests and host machine, then add the necessary network interfaces as explained above.

      Also note this article is based on CentOS 7.

      Reply
  6. After making adapter 1 host-only and adapter 2 NAT, when I try to start up my VM it gets stuck on “Start job is running for Wait for Network to be Configured” and eventually says that its failed.

    So I end up with no internet. Any idea how to fix this?

    Reply
    • @JT

      Then login to your system and configure static IPs as explained in the article, either for Debian/Ubuntu/Linux Mint or RHEL/CentOS/Fedora systems. Then restart the system for the changes to take effect.

      Reply
  7. This is by far the best detailed documentation on this topic. I have been searching on how to setup n/w between guest hosts with static IP on my Virtualbox. Nothing even came close. Thanks a million. Awesome!

    Reply

Got something to say? Join the discussion.

Have a question or suggestion? Please leave a comment to start the discussion. Please keep in mind that all comments are moderated and your email address will NOT be published.

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