How to set up NAT and Host-Only networking with static IP address in VirtualBox

It would be easier to troubleshoot your problem if you posted the results you are getting when trying to ping an external address (like the Google DNS servers you're using), the current network settings, and your routing table.

/sbin/ifconfig -a
/sbin/route -n

Without knowing more this is a bit of a shot in the dark, but my guesses are that either a) you are not getting a DHCP address on eth0, or b) your gateway settings for eth1 are messing with the default route DHCP assigned.

If you're not getting a DHCP address for eth0, it's probably a misconfiguration in VirtualBox (like getting your adapters backwards).

Either way, you don't need a gateway or DNS settings assigned specifically for eth1 since that will be assigned by DHCP on eth0, so I would remove the gateway, dns-search, and dns-nameservers lines from your config. Your virtual machines will still be able to communicate without a gateway setting if they are on the same network and VirtualBox is set up right.

Edit: To make sure you don't have a gateway on eth1 after a reboot, remove lines from your eth1 block so that it looks like this:

auto eth1
iface eth1 inet static
address 192.168.56.1
netmask 255.255.255.0

When you're done, you won't have any gateway lines in your interfaces file.


I came up with thissolution, which is the same as @jkt123's answer, and @Umar's question, but shorter. I'd appreciate feedback on it!

In virtualbox, enable both NAT and host-only networks. (BTW: It works on my win7 host. I don't know how to do that in linux host).

On the host - find the "host-only" interface's ip

ipconfig /all     # for windows host
ifconfig -a       # for linux host

In the guest, edit /etc/network/interfaces. The trick was reversing the order eth1 (host-only) comes BEFORE eth0 (internet/dhcp). I don't know why.

auto lo                        # keep the original loopback settings
iface lo ...                   # yeah, i don't remember, just keep it.

# ----> Ok, this is my addition <-----
auto eth1                      
allow-hotplug eth1             # i think hotplug it helps. not sure.
iface eth1 inet static
address 192.168.56.100         # arbitrary IP address between 2 and 254

auto eth0                      # This is the original content
iface eth0 inet dhcp           #  of this file, now at the end.

reboot.