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

I am trying to set up a set of guests in VirtualBox, such that each of them can access the Internet as well as be visible to each other and to the host. I also want the guests to have static IP addresses.

Here's the procedure that I have followed so far:

  1. Switch off the DHCP Server feature of the Host-Only network to be used in this setup
  2. Change the IP address assigned to the host to 192.168.56.254
  3. Create a guest machine with 2 networking interface cards (NIC)
  4. Configure the 1st NIC to use NAT
  5. Configure the 2nd NIC to use Hot-Only Networking
  6. Install the OS (Ubuntu Server 13.10) on each of them
  7. Update the OS
  8. Configure the networking as follows by editing /etc/network/interfaces
auto lo
iface lo inet loopback

auto eth0
iface eth0 inet dhcp

auto eth1
iface eth1 inet static
address 192.168.56.1
netmask 255.255.255.0
network 192.168.56.0
broadcast 192.168.56.255
gateway 192.168.56.254
dns-search cloudspace.local
dns-nameservers 8.8.8.8 8.8.4.4
  1. Save the file
  2. Reboot

When the guest boots up again, the Host-Only networking is working fine. The host/guest and guest/guest pairs of machines can ping each other, however the Internet isn't working as apt-get fails.

If I then issue a service networking restart command, the networking begins to work as it should.

What am I doing wrong?

I have tried switching the order in which the NICs appear in the /etc/network/interfaces file. I have also swapped the NAT/Host-Only Network around between the 2 NICs. Nothing worked.

The host is Windows 8.1 and the guest is Ubuntu Server 13.10. I have tried the same on Mac OS X with similar results.

I would greatly appreciate any help in this.

Update:

I have included the output from the following commands below, to help in diagnostics:

  • cat /etc/network/interfaces
  • ifconfig -a
  • route -n
$ cat /etc/network/interfaces
auto lo
iface lo inet loopback

auto eth0
iface eth0 inet dhcp

auto eth1
iface eth1 inet static
address 192.168.56.1
netmask 255.255.255.0
network 192.168.56.0
broadcast 192.168.56.255
gateway 192.168.56.254

$ ifconfig -a
eth0      Link encap:Ethernet  HWaddr 08:00:27:75:47:64
          inet addr:10.0.2.15  Bcast:10.0.2.255  Mask:255.255.255.0
          inet6 addr: fe80::a00:27ff:fe75:4764/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:2 errors:0 dropped:0 overruns:0 frame:0
          TX packets:10 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:1180 (1.1 KB)  TX bytes:1332 (1.3 KB)

eth1      Link encap:Ethernet  HWaddr 08:00:27:93:98:d8
          inet addr:192.168.56.1  Bcast:192.168.56.255  Mask:255.255.255.0
          inet6 addr: fe80::a00:27ff:fe93:98d8/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:140 errors:0 dropped:0 overruns:0 frame:0
          TX packets:225 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:14418 (14.4 KB)  TX bytes:27378 (27.3 KB)

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:16 errors:0 dropped:0 overruns:0 frame:0
          TX packets:16 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:1184 (1.1 KB)  TX bytes:1184 (1.1 KB)

$ route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.56.254  0.0.0.0         UG    0      0        0 eth1
10.0.2.0        0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.56.0    0.0.0.0         255.255.255.0   U     0      0        0 eth1

Update 2:

After I execute sudo service networking restart, the output from route -n becomes:

$ route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         10.0.2.2        0.0.0.0         UG    0      0        0 eth0
10.0.2.0        0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.56.0    0.0.0.0         255.255.255.0   U     0      0        0 eth1

So how do I ensure this configuration on boot?


Solution 1:

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.

Solution 2:

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.