Skip to content

Instantly share code, notes, and snippets.

@pjdietz
Created June 12, 2013 19:01
Show Gist options
  • Save pjdietz/5768124 to your computer and use it in GitHub Desktop.
Save pjdietz/5768124 to your computer and use it in GitHub Desktop.
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

@leoxs22
Copy link

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
Copy link

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
Copy link

Thank you!! That solved my problem.

@sibagithub
Copy link

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
Copy link

@sibagithub, I am also facing the same problem.

@KDCinfo
Copy link

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
Copy link

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
Copy link

@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
Copy link

Thank you! problem solved

@shturec
Copy link

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
Copy link

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
Copy link

Thanks!!

@RumiLinux
Copy link

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
Copy link

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
Copy link

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
Copy link

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
Copy link

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.)

@granjerox
Copy link

Hello, one little TIP I havent found as a full example.
As long as the defautl behaviour for host-only network is the IP set by the internal DHCP server, wouldn't it better to setup static leases for our hosts?

1.- FindOut network name for dhcp server:

./VBoxManage list dhcpservers
NetworkName:    HostInterfaceNetworking-VirtualBox Host-Only Ethernet Adapter
Dhcpd IP:       192.168.56.100
LowerIPAddress: 192.168.56.101
UpperIPAddress: 192.168.56.254
NetworkMask:    255.255.255.0
Enabled:        Yes
Global Configuration:
    minLeaseTime:     default
    defaultLeaseTime: default
    maxLeaseTime:     default
    Forced options:   None
    Suppressed opts.: None
        1/legacy: 255.255.255.0
Groups:               None
Individual Config:

2.- You need also the mac address of your VM's interface

PS C:\Program Files\Oracle\VirtualBox> ./VBoxManage list vms
"accadev-ubuntu20.04" {2a98947e-4b8c-4bc3-9441-2b556d0b33a1}
"accadev-ansible01" {1d261714-9b49-47e2-a634-bc0677f5b707}

PS C:\Program Files\Oracle\VirtualBox> ./VBoxManage showvminfo accadev-ubuntu20.04 | Select-String -Pattern 'MAC'

NIC 1:                       MAC: 0800275452FB, Attachment: NAT, Cable connected: on, Trace: off (file: none), Type: 82543GC, Reported speed: 0 Mbps, Boot priority: 0, Promisc Policy: deny, Bandwidth group: none
NIC 2:                       MAC: 08002703E954, Attachment: Host-only Interface 'VirtualBox Host-Only Ethernet Adapter', Cable connected: on, Trace: off (file: none), Type: 82540EM, Reported speed: 0 Mbps, Boot priority: 0, Promisc Policy: deny, Bandwidth group: none

3.- Setup Static Lease for that MAC.
./VBoxManage dhcpserver modify --ifname "VirtualBox Host-Only Ethernet Adapter" --mac-address=08:00:27:f8:31:fa --fixed-address=192.168.56.20

4.- Restart DHCP server to apply changes
./VBoxManage dhcpserver modify --ifname "VirtualBox Host-Only Ethernet Adapter" --mac-address=08:00:27:f8:31:fa --fixed-address=192.168.56.20

And that's it. You can do it not only for host-only networks but for any Vbox network with an DHCP server attached

Reference

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment