Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
VirtualBox Host-Only Adapter with Static IP

VirtualBox Host-Only Static IP

My typical setup for a development box in VirtualBox uses two NICs. The first uses NAT to allow the box to communicate with the outside world through my host computer’s network connection. (NAT is the default, so shouldn't require any setup.) The second is a "host-only" connection that allows my host and guest to interact.

To create a host-only connection in VirtualBox, start by opening the preferences in VirtualBox. Go to the "Network" tab, and addd a Host-only Network. Modify the host-only network, and disable DHCP. Make a note of the IP address. (Feel free to set the IP address as well, if you like.)

Next, assign this host-only adapter to the virtual machine. Select the VM and press "Settings". Go to the "Network" tab, and select "Adpater 2". Enable the adapter, set it to a "Host-only Adapter", and select the adpater you created above.

Temporary

To get the static IP address working temporarily, access the termainal on the client, and enter the following to assign a static IP to eth1. (I'm assuming 192.168.56.101 because that's VirtualBox's default. Make sure it matches the IP for your host-only adapter.)

# On GUEST: Temporarily assign a static IP to eth1
ifconfig eth1 192.168.56.101 netmask 255.255.255.0 up

Persistent

The last command should work until you have to restart the virtual machine. To make the change persistent, you'll need to modify /etc/network/interfaces

# On GUEST: Modify the configuration file
sudo nano /etc/network/interfaces

Add the following to configure eth1 to use a static IP address and come up when the system starts.

# The host-only network interface
auto eth1
iface eth1 inet static
address 192.168.56.101
netmask 255.255.255.0
network 192.168.56.0
broadcast 192.168.56.255

Accessing the Guest from the Host

Finally, you'll probably want to add some names to your host machine's /etc/hosts file. Make sure to add each hostname you will use on your guest.

# On HOST: edit /etc/hosts
sudo nano /etc/hosts

Add a line that starts with the static IP address for the guest, followered by whitespace, then each of the hostnames that should point to your guest.

# Inside /etc/hosts on your HOST
192.168.56.101 myguest.home.local www.mycoolsite.com anyother.fakesite.com

Source: http://christophermaier.name/blog/2010/09/01/host-only-networking-with-virtualbox

@Manjesh80

This comment has been minimized.

Copy link

@Manjesh80 Manjesh80 commented Dec 18, 2016

This is cool .. and works like a charm

@webian

This comment has been minimized.

Copy link

@webian webian commented Feb 22, 2017

Nice tutorial, thank you!

@numberwhun

This comment has been minimized.

Copy link

@numberwhun numberwhun commented Mar 24, 2017

Excellent, thank you!

@deglot

This comment has been minimized.

Copy link

@deglot deglot commented Mar 31, 2017

It's important to bare in mind that if you specify the gateway (as the value taken from VirtualBox, when you are configuring the Host-Only Adapter) you may end-up with a non-internet connection. Just do not put any gateway information when configuring static IP Address and you should be find.

@JLudwig2552

This comment has been minimized.

Copy link

@JLudwig2552 JLudwig2552 commented Apr 6, 2017

Thanks for the instructions.
I had to deviate slightly for Windows 7 host and Ubuntu 16.04 guest. I didn't create the temporary eth1 adapter which may be why.

I used the host's IP of the "VirtualBox Host-Only network" for the network value, and added one to this for the address value.

The host's IP of the "VirtualBox Host-Only network" for me was 192.168.228.1 so the address I used was 192.168.228.2

Thus my /etc/network/interfaces looked like this:

auto enp0s10
iface enp0s10 inet static
address 192.168.228.2
netmask 255.255.255.0
network 192.168.228.1
  • Note enp0s10 was used because it was present inside /sys/class/net/ - run ls /sys/class/net/ to see all your network adapters. One of them should represent the host-only network.
@mmorejon

This comment has been minimized.

Copy link

@mmorejon mmorejon commented Apr 17, 2017

Thanks @JLudwig2552 !!!!

@dxramax

This comment has been minimized.

Copy link

@dxramax dxramax commented Jun 14, 2017

after hours of searching, this is the only way it worked, from the first time.
Thanks a lot @pjdietz

@NarasimhaChinimilly

This comment has been minimized.

Copy link

@NarasimhaChinimilly NarasimhaChinimilly commented Jul 7, 2017

Supreb !!! Works like a charm...

@jsami

This comment has been minimized.

Copy link

@jsami jsami commented Jul 12, 2017

Nice!!! Thank you.

@liuxinjn

This comment has been minimized.

Copy link

@liuxinjn liuxinjn commented Sep 27, 2017

thx so much.

@leoxs22

This comment has been minimized.

Copy link

@leoxs22 leoxs22 commented Oct 22, 2017

@deglot
OMG, I was going crazy trying to fix the lost internet connection. Two days with that problem.
Thanks a lot!

@huoxiaoyao

This comment has been minimized.

Copy link

@huoxiaoyao huoxiaoyao commented Nov 8, 2017

Adapter 2 does not always correspond with eth1 (might also be eth0). If you are running into problems, switching the settings from those interfaces might be your solution

@anvis2000

This comment has been minimized.

Copy link

@anvis2000 anvis2000 commented Dec 26, 2017

Thank you!! That solved my problem.

@sibagithub

This comment has been minimized.

Copy link

@sibagithub sibagithub commented Jan 4, 2018

I am not getting a option to add a Host-only Network in VirtualBox-->preferences-->"Network" tab.
I am only getting a option to add new "NAT Network". How can I add a Host-only Network.

I have VirtualBox installed on my mac. I have installed ubuntu & centOS on VirtualBox.
(I am not getting eth0 or eth1 in ifconfig. I have enp0s3, enps08, lo & vibr0 under ifconfig.)

screen shot 2018-01-04 at 7 23 23 pm

@chaitanya9186

This comment has been minimized.

Copy link

@chaitanya9186 chaitanya9186 commented Jan 5, 2018

@sibagithub, I am also facing the same problem.

@KDCinfo

This comment has been minimized.

Copy link

@KDCinfo KDCinfo commented Jan 7, 2018

@sibagithub @chaitanya9186 -- I'm using VirtualBox 5.2.4 on Win10 x64 (with Ubuntu 16.04 LTS x64 ISO running inside the VM), and I found the host-only network setting from the main VirtualBox interface (with no VM open) File->Host Network Manager->Create. Although, after I created one, I realized I didn't even see the one already sitting there (VirtualBox Host-Only Ethernet Adapter #2). Upon realizing that, and it was already assigned to my Host-only Adapter (in Settings->Network), I removed the new one I had just created, and moved on to the "make it permanent" part.

Unfortunately, the specifics of this guide didn't get me through, so I found and successfully used instructions from: https://www.tecmint.com/network-between-guest-vm-and-host-virtualbox/. Although I did not, however, go through the NAT part (all I needed was dev access from the host).

With just my settings below, I was able to hit Apache from my Win10 host at the http://192.168.56.5 address, and I could still access my Samba share (although slightly off-topic, I've also provided those settings below).

	VBox -> File -> Host Network Manager (Ctrl-W)
		Configure Adapter Manually
		IPV4 Address:	192.168.56.1
		IPv4 Mask:	255.255.255.0
		IPv6 Address:	fe80::grayed out
		IPv6 Prefix Ln:	64

	VBox -> Settings -> Network
		Attached to:	Host-only Adapter
		Name:		VirtualBox Host-Only Ethernet Adapter #2
		Adapter Type:	Intel PRO/1000 MT Desktop (82540EM)
		Promiscious:	Deny
		Cable:		Connected

	**Open VM**
	sudo vi /etc/network/interfaces
		source /etc/network/interface.d/*
		auto lo
		iface lo inet loopback
		auto enp0s3 inet static
		address:	192.168.56.5
		netmask:	255.255.255.0
		network:	192.168.56.0
		gateway:	192.168.56.1

	**Samba Share**
	sudo vi /etc/samba/smb.conf
		#This 'Code' section added at end of file.
		[Code]
		path = /var/www/html/
		available = yes
		valid users = yourubuntulogin
		read only = no
		browsable = yes
		public = no
		writable = yes
	sudo smbpasswd -a yourubuntulogin
	sudo service smbd restart
@sibagithub

This comment has been minimized.

Copy link

@sibagithub sibagithub commented Jan 8, 2018

Thanks @KDCinfo.
Using file—>Host Network Manager, I could create a "Host-only Network”.
But I am not getting eth0 or eth1 using ifconfig command. I have enp0s3, enps08, lo & vibr0 using ifconfig.

@sibagithub

This comment has been minimized.

Copy link

@sibagithub sibagithub commented Jan 10, 2018

@chaitanya9186

After adding a 'Bridged Adapter', I got a new entry enps10 in ifconfig. And I could SSH into the inet IPAddr of enps10
screen shot 2018-01-10 at 9 29 23 am
.

@mohamma548

This comment has been minimized.

Copy link

@mohamma548 mohamma548 commented Jan 18, 2018

Thank you! problem solved

@shturec

This comment has been minimized.

Copy link

@shturec shturec commented Apr 16, 2018

Thanks for the guide!
I used it with Ubuntu 17.10.01 server and one thing that bugged me was that ifup/down utilities were missing in the guest OS, so updating the /etc/network/interfaces was no good for bringing up the host-only adapter after restart. The ifconfig based, manual setup was working completely fine.
Therefore apt install ifupdown2 was necessary for me to complete the process successfully. Having this installed was probably an obvious prerequisite given that the interfaces file starts with the comment "... configuration file for ifup(8), ifdown(8)", but yeah, well, documentation... :-)
I hope that helps.

@saty9999

This comment has been minimized.

Copy link

@saty9999 saty9999 commented May 4, 2018

I went into preferences.I added on host only network .Given ip .but what about NAT NETWORKING THERE.it should be disabled or what?what is the significance of NAT NETWORKING THERE ? I did what you all said stilll windows is not able to ping outside world .what to do .

@sagespidy

This comment has been minimized.

Copy link

@sagespidy sagespidy commented Jun 13, 2018

Thanks!!

@RumiLinux

This comment has been minimized.

Copy link

@RumiLinux RumiLinux commented Nov 3, 2018

Hi inside my virtual box i have 2 vms of ubuntu. My virtual box is hosted on windows 10. I am trying to ping host from vms but not working. I tried setting NAT in both vm for adapter1. But still in vain. Can some1 explain in detail how to set it up and configure the NIC if needed?

@n6151h

This comment has been minimized.

Copy link

@n6151h n6151h commented Nov 17, 2018

This all goes out the window if you're using Ubuntu 18.04 (bionic beaver) server, which now uses netplan. /etc/init.d/networking isn't there anymore, though /etc/network/interfaces still is (for reasons I cannot fathom.) Instead, you'll want to modify /etc/netplan/50-whatever.yaml to include your interface. This link describes what to modify and how: https://blog.ubuntu.com/2017/12/01/ubuntu-bionic-netplan

@krsurajd

This comment has been minimized.

Copy link

@krsurajd krsurajd commented Mar 1, 2019

Host only Network is moved in the versions in 5.2.0 .
If you are unable to find the host only network card in Virtual Box .
This tool is moved in 5.2.0. Look on the main UI toolbar, specifically the "Global Tools" split button on the right.

@ioana996

This comment has been minimized.

Copy link

@ioana996 ioana996 commented May 10, 2020

I've been searching for hours for a good alternative. Yours is simple, well explained and functional. Thank you! You spared me some serious time.

@JoergMittagLawo

This comment has been minimized.

Copy link

@JoergMittagLawo JoergMittagLawo commented May 24, 2020

Please, do not use 192.168.56.101 as a fixed IP address. By default, VirtualBox runs a DHCP server on the host-only network that hands out IP addresses from 192.168.56.101 onwards. The range 192.168.56.2-100 is free for static IPs. (192.168.56.1 is the host itself.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.