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

@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