How to map a local virtual machine to be network accessable on Linux

  • warning: include(/tmp/fortune.txt): failed to open stream: No such file or directory in /home/mohawksoft/org/www/htdocs/includes/common.inc(1696) : eval()'d code on line 1.
  • warning: include(): Failed opening '/tmp/fortune.txt' for inclusion (include_path='.:/usr/share/php:/usr/share/pear') in /home/mohawksoft/org/www/htdocs/includes/common.inc(1696) : eval()'d code on line 1.

All these steps require root access.
Assumptions:
physical network interface: eth0
real network: 192.168.1.0/255.255.255.0
physical alias IP: 192.168.1.200

virtual network interface: virbr0
virtual network: 192.168.122.0/255.255.255.0
virtual host IP: 192.168.122.10

First, create a network interface alias:

root> ifconfig eth0:1 192.168.1.200 netmask 255.255.255.255

It is important to note that the netmask being 255.255.255.255 is very important! Otherwise Linux will gladly supply a network mask on the interface that will probably be wrong, especially if you intend to use the virtual machines as an Internet server.

The next step is to route the alias on the host machine to the virtual machines IP address.

root> iptables -t nat -A PREROUTING -d 192.168.1.200 -i eth0 -j NAT --to-destination 192.168.122.10

Now, route the internal data back out to the alias.

root> iptables -t nat -A POSTROUTING -s 192.168.122.10 -o eth0 -j SNAT --to-source 192.168.1.200

Now forward data accordingly

root> iptables -A FORWARD -p tcp -i eth0 -O virbr0 -d 192.168.122.10 -j ACCEPT