Networking with Virtual machines

This page discusses only about the networking aspects of virtual machines and to be more precise, about host interface networking (bridging). If you want to know about installation of the virtualization software and virtual machines visit the respective sites (www.virtualbox.org, www.vmware.com, http://user-mode-linux.sourceforge.net/).

Just as a reminder: This is just one way for doing this. There might be ways one considers better in some way. That might be true and this is just the way it worked for me.

One thought about the installation of virtualbox. If it complains about the missing kernel headers use following command and it will compile the kernel modules (just remember to replace the path to your corresponding one).

#sudo KERN_DIR=/usr/src/linux-source-2.6.25.hipl /etc/init.d/vboxdrv setup

Install packet called dhcp and modify its configuration file /etc/dhcpd.conf to contain the following lines. If needed the IP range can be changed, but you have to remember to change the IPs in the following scripts also.

  option subnet-mask 255.255.255.0;
  default-lease-time 6000;
  max-lease-time 72000;
  option domain-name-servers 172.16.121.1;
  option routers 172.16.121.1;
  server-name "172.16.121.1"; 
  subnet 172.16.121.0 netmask 255.255.255.0 {
     range 172.16.121.20 172.16.121.25;
  }

Script to build up the interfaces and iptables rules to get the host networking to work. First the script loads necessary kernel modules. Stops previous instances of dhcpd. Creates the interfaces for the virtual machine. "-u <username>" option is necessary only for virtualbox (replace <username> with your username. If you are using virtualbox the user has to belong to vboxusers group.).

  # modprobe needed modules for TUN/TAP devices
    modprobe tun

  # shutdown earlier instance of dhcpd
    /etc/init.d/dhcp stop

  # create device for the uml/virtualbox/vmware and dhcp service running for it
  # -u <username> option is necessary only for virtualbox
    tunctl -t tap1 -u <username>
    ifconfig tap1 172.16.121.1
    dhcpd tap1

  # modprobe needed modules for iptables
    modprobe ip_tables
    modprobe iptable_filter
    modprobe iptable_nat

  # enable NIC forwarding 
    echo 1 > /proc/sys/net/ipv4/ip_forward

  # set forwarding rules to the host iptables for the virtualbox/vmware/uml guest
    iptables -t nat -I POSTROUTING -o eth0 -j MASQUERADE
    iptables -I FORWARD -i tap1 -j ACCEPT
    iptables -I FORWARD -o tap1 -j ACCEPT

When the dhcp is configured and the script above is run, you should be able to start your virtual machine with host interface networking (virtualbox) and there should be connectivity between the Internet, host and the guest.