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:
- Host Operating System – Linux Mint 18
- Virtual Machine OS – CentOS 7 and Ubuntu 16.10
Requirements
- A working Oracle Virtualbox installed on Host machine.
- 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.
- 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
:
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.
Below is a screen shot showing a new host-only network has been created called vboxnet0.
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.
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.
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 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:
- Check the option: “Enable Network Adapter” to turn it on.
- In the field Attached to: select Host-only Adapter
- Then select the Name of the network: vboxnet0
As in the screen shot below and click OK to save the settings:
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:
- Check the option: “Enable Network Adapter” to activate it.
- In the field Attached to: select NAT
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
From the screen shot above, you can see that there are three interfaces enabled on the virtual machine:
lo
– loopback interfaceenp0s3
(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.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
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.
This needs me to first create the host network manager: File » Host Network Manager » Create.
Hi. I want to know what the steps would be for windows 7 when it’s the guest OS, as the command line inputs you are using are invalid for windows of any form. My host OS is Linux mint 20, and the guest OS is Windows 7. My assumption is that the steps are the same for setting up the VM itself, but once you are working inside the VM, obviously the steps will be different even if very similar.
Nice post. It was as very helpful.
Manage Virtual Machines From Host Using SSH: This portion of the document is not explanatory.
@GANIYU
Oh yes, the purpose of this section was simply to demonstrate how to connect to a VM using its IP address, via SSH once there is a host-only connection established between the VM and the host.
Hi all,
My friend Vijay just changed the following interfaces
ONBOOT=No --> ONBOOT=yes
and restart the machine, I am able to ping and resolve the issue.
Thanks for the great tutorial! But I have an issue, everything works fine for me except when I do a ping from my guest VM to other website on the internet (ex. http://www.google.com), 100% of the sending packet was loss. I don’t know how to fix this.
I’m using windows 10 for my host and Ubuntu Server 18.04 for my guest VM.
I really need your help.
Thanks in advance.
Fantastic tutorial! It was a little confusing that you just included a link to the tutorial for “configure static IP address”. Also, that tutorial’s screenshots do not match what I saw in CentOS 7. The values are in quotation marks in the screenshots, but there aren’t any quotation marks around the values in my CentOS 7 files.
@Sam
The quotation marks are not a must to use, you can ignore them.
Thank you. This helped me a lot
@Aster
We are glad that this helped you. Thanks for the feedback.
I found that when I got to add a Host-only adapter to my VM’s Network, I could not select any Name in the dropdown, and the settings were invalid. As mentioned at https://askubuntu.com/questions/198452/no-host-only-adapter-selected you need to close the VM’s settings, go to File->Host Network Manager, and create a network there.
Then, go into the VM’s Network Settings and you can choose the newly created Host Only network there. This may be what the first section in this article “Create a Network For Guests and Host Machine” was trying to do, but the UI has changed so much I found it impossible to follow.
Hi Nick,
Thanks.
Your comment was so apt for the problem I am also having. I also realized you need to work with Host Network Manager first.
Hi Aaron
I do not see ifcfg-enp0s8 file in network-scripts location and unlike in your screenshots, I have no IP address for enp0s3 & enp0s8 attached but virbr0 has IP. ( Is that default?)
I wonder which file to update now for static IP configuration to connect from win10 through Putty?
Looking forward for your help. Thanks
@Ravi
Which distro are you using? Use the appropriate file for your distro, it could be different from what is in the screenshot.
for NAT add:
@Alex
Thanks for the tip.
Hello, I have created multiple linux machines on Oracle VM box, it has internal connectivity each other and also have internet access from just one linux machine but unable to access internet from other linux machine. It will be great help if you can suggest to access internet from other machine without disturbing internal connectivity.
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?
@Tidywolf
Oh, yes, that is a correct observation, thanks for the heads up.
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:
@Mark
Check if the port 8080 is open, or if your guest OS is CentOS/RHEL/Fedora or one in that family, you need to open that port in the firewall.
@Aaron Kili,
The firewall was activated by default :-(
I deactivated it and everything is working fine now.
@Mark
We are glad to know that everything is now working fine for you. Thanks for the feedback.
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 toonboot=no
.You have to change that manually.
Then you can switch the host-only to static and everything works fine.
@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.
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.
@Mark
Okay, many thanks for the getting back to us.
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.
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
@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.
If dhcp is enabled in host, why to use a static ip in guest, i think iface enp0s3 inet dhcp works better than static
@george
That’s true, but i prefer to use a static IP, although dhcp works well.
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?
@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.
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!
@Anand
Welcome, thanks for appreciating this piece of work.
Very quick, simple and succinct. Well done.
@Surren
Thanks for reading through, and for the feedback.
Very good, thanks :)
@Lucas
Thanks a lot for reading, and for following us.
“From the following interface, there are two options; choose Host-only Networks by clicking on it.”
I only have one option “NAT Networks“, the “Host-only Networks” option is missing?
@Micheal
Really, it should be there, or else download and install the latest version of Oracle VM and check it again.
Ok I have figured it out. The “Host-Only networks” have been moved from the “Network” section of the VirtualBox Preferences to the “Host Network Manager” tab of the “Global Tools” in version 5.2.0.
@Micheal
Okay, many thanks for sharing this.
Aaron,
Great article! Thanks for making my life easier! :0-)
Quick question…
I have a bunch of VMs on my Debian server and applied your expertise above.
All are working fine except OpenSUSE.
Just wondering what I need to configure to get it working as well.
I tried using the default for CentOS and it does not like my entry:
As noted, I used .15 for my IP as I used .5 for Ubuntu, .10 for CentOS.
Restarted network, rebooted and the config still is not showing enp0s3.
Wondering if you could point me in the right direction to find a test config for OpenSUSE?
Thanks a million!
@Micheal
You need to check out the OpenSUSE documentation, specifically network settings.
Aaron,
Fabulous article! Thank you for making my life easier! :0-)
Quick question, I have a lot of VMs and this worked perfectly for all, except for OpenSUSE.
How and where do I add the configuration file you have listed?
Not a pro with OpenSUSE but trying to get there.
I add the config file quite like the CentOS example and OpenSUSE doesn’t like it.
Help!
Hi! After using this procedure I can ssh my guest VMs from the host. Thanks!
But I lost internet connection from the guest through NAT. It was working before I add a the Host Only Adapter to be guests.
I looked for different solutions on the net and found nothing fixing this.
Any clue?
Thanks!
I got it to work with the following definition for Host Only and NAT. I can now do both access the internet from guests and ssh guests from host.
Host Only Network:
And with this one for NAT:
@Martin
This is a great way to go, it’s good that you fixed it yourself. Thanks for sharing this with us, i hope this will be useful to Linux users who encounter the same issue.
Still work like a charm with this setup. My only remaining problem is when I connect through corporate VPN. This screws up routing rules and I cannot connect to the VM through SSH anymore. Looked into different options but no resolution yet. Installing VPN client on guests is the only option I didn’t tried Yet.
But thanks again for you great article. You helped many of us to ramp up.
Hi, I had installed two guest Vms in oracle virtual box. But I am able to access internet only in one guest VM but not able to access internet from other VM. In both the guest VMs I had added two network adapter with Host-only adapter and for other adapter given NAT. And able to do ssh to both guest VM vice versa . Kindly help me to figure out
@sandeep
I occasionally faced this issue even after performing the correct configurations. To connect to the Internet from your VMs, use NAT; try to install a new VM and add the net adapter before installing the OS image, then configure it as before and see if it works.
Hi Aaron , Thanks for you reply . Now its working . Thanks a ton .
@sandeep
Welcome, thanks for the feedback, and for following us.
@sandeep
Or just add a second adapter for Host-only network(do not change settings for default adapter for NAT), and configure it as explained above.
Hi , I had two guest os installed in oracle virtual box the problem is I can access internet only from one guest os but for the other guest os I am unable to access internet . Kindly help me figure it out
It worked till it get to the password. my password for the virtual machine is :root, but it didn’t go through.
@sam
Edit you sshd configuration file and allow root user to use password:
#vi /etc/ssh/sshd_config
Look for the directive PermitRootLogin and change its value to:
PermitRootLogin yes
Save and close the file, then restart sshd service. And finally try to connect once more.
Cheers mate !! I’m just starting with VM and your article have helped me how to deal with network in an virtual environment.
@Tiago
Hope you enjoyed reading through, and thanks for the feedback.
Thank you for these steps, i had configured my Centos VM with DHCP and while I could connect to the Internet, I wasn’t able to ssh from my host (windows).
After creating a second Host-only-network It worked. Now I can just use putty instead on my host
i cant thank you enough.
@priti
Welcome, hope you enjoyed the article. Thanks for the feedback.
wow this actually worked, thank you so much
@:)
Welcome, thanks for the response and for following us as well.