VirtualBox: Make your virtual machine accessible from your host system but not from the local network

With VirtualBox, a virtualization software package developed by Sun Microsystems, you run an operating system simultanously with the system that you have booted from. The operating system installed inside VirtualBox is called guest while the operating system which is running VirtualBox is called your host system.

You can assign it as much RAM and disk capacity as you want, of course by respecting the maximum available RAM and disk space. Also are you abstracting from your actual hardware. Even the network will be abstracted.

By default every virtual machine has its own local network which has access to the internet while you deny access from your host system. The technique behind the scene is called NAT which does exactly the same as your FRITZ!Box where people behind it cannot access your computer unless you forward ports.

However if you have a web server running on your virtual machine that you want to access from your host system or from other virtual machines, you should use a Bridged Adapter or a Host-only Adapter instead.

With a Bridged Adapter you tell VirtualBox to behave like a seperate computer which is directly connected to your FRITZ!box. Your virtual machine will then get an IP from it which is in the same network than your host system. Now every computer in your home or office network including your host system, can visit your website hosted on your virtual machine.

If you use your virtual machine to develop something that needs another environment than your host system provides while no other computer should have access to it, except your host system, then the Host-only Adapter should be used.

This article covers specifically the configuration of the Host-only Adapter. This option allows you to create a new virtual network interface on which you can run a DHCP server.

Step 1: Load the kernel modules

VirtualBox needs at least vboxdrv to be loaded for basic features and for networking must additionally load vboxnetadp and vboxnetflt. Maybe vboxpci is also required so for best support you should load them all.

sudo modprobe -a vboxdrv vboxnetadp vboxnetflt vboxpci

If you use systemd you can load the modules on boot time by creating the following file with a list of the above modules, one per line.

/etc/modules-load.d/virtualbox.conf:

vboxdrv
vboxnetadp
vboxnetflt
vboxpci

Step 2: Check the current available network interfaces

# ifconfig -a
eth0: flags=4163  mtu 1500
        inet 192.168.178.24  netmask 255.255.255.0  broadcast 192.168.178.255
        ether 82:8d:40:e1:fe:ac  txqueuelen 1000  (Ethernet)
        RX packets 760552  bytes 932187872 (889.0 MiB)
        RX errors 0  dropped 59  overruns 0  frame 0
        TX packets 429417  bytes 50541149 (48.1 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        loop  txqueuelen 0  (Local Loopback)
        RX packets 30038  bytes 27083200 (25.8 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 30038  bytes 27083200 (25.8 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

We see a loopback and a physical network interface.

Step 3: Create a new virtual network interface

Now open the dialog with the VirtualBox settings by clicking on File > Preferences > Network.

vbox_settings_with_marks

There are three buttons, two to add or remove an interface and a third for the actual network settings. Create a new interface and check if it has become available.

# ifconfig -a
eth0: flags=4163  mtu 1500
        inet 192.168.178.24  netmask 255.255.255.0  broadcast 192.168.178.255
        ether 82:8d:40:e1:fe:ac  txqueuelen 1000  (Ethernet)
        RX packets 760552  bytes 932187872 (889.0 MiB)
        RX errors 0  dropped 59  overruns 0  frame 0
        TX packets 429417  bytes 50541149 (48.1 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10
        loop  txqueuelen 0  (Local Loopback)
        RX packets 30038  bytes 27083200 (25.8 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 30038  bytes 27083200 (25.8 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

vboxnet0: flags=4098  mtu 1500
        ether 08:40:11:23:05:b1  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

Now define a network address. This is the gateway address to be used for the default route on the newly created interface.

vbox_dhcp

You have the option to enable a DHCP server on this interface so that your host system and your virtual machine can get a dynamic IP.

vbox_dhcp2

If you have multiple machines running that need fix IP addresses, disable the DHCP server and assign the machines a static IP. In this case run on your host system

sudo ifconfig vboxnet0 192.168.56.2

and on your virtual machine

sudo ifconfig vboxnet0 192.168.56.3

When using static IPs also set the default route on both machines.

route add default gw 192.168.56.1 vboxnet0

Step 4: Change the adapter type of your virtual machine

Now right-click on your virtual machine and click on Settings.

vbox_vm_settings

Enable an adapter and attach it to vboxnet0.

vm_host_adapter

At this point there’s no more to say. You’re done!

2 Comments

  1. techyee

    Great post. I just now stumbled upon your website along with wanted to declare that I’ve absolutely liked looking a person’s site blogposts. After all We will be signing up for your nourish so i we imagine you write again very soon!

  2. Heya! I’m at work browsing your blog from my new iphone!
    Just wanted to say I love reading your blog and look forward to all your
    posts! Keep up the superb work!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>