Home > VirtualBox > Internet access in Virtualbox Host-only networking

Internet access in Virtualbox Host-only networking



SITUATION: After enabling host-only networking mode for a VM in virtualbox, from inside the vps I am unable to ping the default router of host or connect to internet for installing packages.
In bridge mode it was working fine. The details of host and guest vm is provided below,


Host Details:

Main IP 				: 	--> eth0
Gateway IP				:
Virtual Interface(Created by Virtualbox): 	--> vboxnet0	
DNS					:
OS					: Ubuntu 11.04


Guest VM Details:

IP					:
Gateway					:
Network					:
DNS					:
Networking Mode				: Host-only networking
OS					: Ubuntu 10.10


From inside the vm, I am able to ping the IP addresses of host( and virtual interface( But I am unable to ping to the default gateway( from inside vm. So, I wasn’t able to install additional packages in that vm.




SOLUTION: Configure packet forwarding from interface “vboxnet0” to “eth0”. Follow the steps mentioned below,


1) Add the following iptable rules from host machine’s command line

iptables -A FORWARD -o eth0 -i vboxnet0 -s -m conntrack --ctstate NEW -j ACCEPT
iptables -A FORWARD -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
iptables -A POSTROUTING -t nat -j MASQUERADE


2) Install dnsmasq in host and start it. dnsmasq is a small caching DNS proxy and DHCP/TFTP server.

 apt-get install dnsmasq
 /etc/init.d/dnsmasq start


3) Set the nameserver in guest vm to host server ip

root@heuristics3:~# cat /etc/resolv.conf 


4) Add the following entries in /etc/sysctl.conf of host and run “sysctl -p” in host command line.

net.ipv4.ip_forward = 1
net.ipv4.conf.all.proxy_arp = 1


That’s all:)









Categories: VirtualBox
  1. tolaris
    May 16, 2012 at 9:30 pm

    Why is proxy arp required?

    • Ken
      August 3, 2012 at 2:27 am

      In this case, it may not be required. It is typically used to build a DMZ (I think). I’ll see if I can research a little more.

  2. July 4, 2012 at 3:03 pm

    not working for me(

  3. Ken
    August 2, 2012 at 3:17 am

    This worked great thanks much.
    How do I port forward to the VM through this setup?

    • August 2, 2012 at 11:40 am

      Ken, try:

      iptables -t nat -A PREROUTING -p tcp -m multiport –dports 8000 -j DNAT –to-destination

      This will forward your PC’s port 8000 to the VM’s port 80. You can also forward multiple ports as long as they are sequential, in one rule:

      iptables -t nat -A PREROUTING -p tcp -m multiport –dports 8000:9000 -j DNAT –to-destination

      • Ken
        August 3, 2012 at 2:25 am

        Thanks. I will give it a shot and let you know what happens.

  4. July 19, 2013 at 12:45 pm

    Hi, after reading this awesome piece of writing i
    am as well delighted to share my know-how here with mates.

  5. Martin
    July 21, 2013 at 3:00 pm

    Thnx, this was one of the first hits when looking for access to the internet from within a virtual server. Easy to follow and suddenly apt-get update worked 😉

  6. this neither
    September 6, 2013 at 6:27 pm

    Works like charm. Thank you so much!

  7. April 10, 2014 at 1:35 am

    Thnx, This worked great .

  8. tom
    August 10, 2014 at 6:18 pm

    Well done, works great. Are these iptable rules permanent or are they cleared after the host reboots?

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: