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 : 192.168.1.3 --> eth0 Gateway IP : 192.168.1.3 Virtual Interface(Created by Virtualbox): 192.168.56.1 --> vboxnet0 DNS : 192.168.1.1 OS : Ubuntu 11.04
Guest VM Details:
IP : 192.168.56.101 Gateway : 192.168.56.1 Network : 192.168.56.0/24 DNS : 192.168.1.3 Networking Mode : Host-only networking OS : Ubuntu 10.10
From inside the vm, I am able to ping the IP addresses of host(192.168.1.3) and virtual interface(192.168.56.1). But I am unable to ping to the default gateway(192.168.1.1) 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 192.168.56.0/24 -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 nameserver 192.168.1.3 root@heuristics3:~#
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:)
Ref:
http://www.cyberciti.biz/faq/caching-dns-server/
http://serverfault.com/questions/127636/force-local-ip-traffic-to-an-external-interface
Why is proxy arp required?
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.
not working for me(
This worked great thanks much.
How do I port forward to the VM through this setup?
Ken, try:
iptables -t nat -A PREROUTING -p tcp -m multiport –dports 8000 -j DNAT –to-destination 192.168.56.101:80
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 192.168.56.101:80-1080
Thanks. I will give it a shot and let you know what happens.
Hi, after reading this awesome piece of writing i
am as well delighted to share my know-how here with mates.
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 😉
Works like charm. Thank you so much!
Thnx, This worked great .
Well done, works great. Are these iptable rules permanent or are they cleared after the host reboots?