Virtualbox port forwarding with iptables

Posted by jverdeyen on Server Fault See other posts from Server Fault or by jverdeyen
Published on 2012-06-29T21:47:21Z Indexed on 2012/06/30 9:17 UTC
Read the original article Hit count: 252

I'm using a virtualmachine (virtualbox) as mailserver. The host is an Ubuntu 12.04 and the guest is an Ubuntu 10.04 system.

At first I forwarded port 25 to 2550 on the host and added a port forward rule in VirtualBox from 2550 to 25 on the guest. This works for all ports needed for the mailserver. The guest has a host only connection and a NAT (with the port-forwarding). My mailserver was receiving and sending mail properly. But all connections are comming from the virtualbox internal ip, so every host connection is allowed, and that's not what I want.

So.. I'm trying to skip the VirtualBox forwarding part and just forward port 25 to my host only ip of the guest system.

I used these rules:

iptables -F
iptables -P INPUT ACCEPT
iptables -P OUTPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -t nat -P PREROUTING ACCEPT
iptables -t nat -P POSTROUTING ACCEPT
iptables -A INPUT --protocol tcp --dport 25 -j ACCEPT
iptables -A INPUT -i lo -j ACCEPT
iptables -A INPUT -s 192.168.99.0/24  -i vboxnet0 -j ACCEPT
echo 1 > /proc/sys/net/ipv4/ip_forward
iptables -t nat -A PREROUTING -p tcp -i eth0 -d xxx.host.ip.xxx --dport 25 -j DNAT --to 192.168.99.105:25
iptables -A FORWARD -s 192.168.99.0/24 -i vboxnet0 -p tcp --dport 25 -j ACCEPT
iptables -t nat -A POSTROUTING -s 192.168.99.0 -o eth0 -j MASQUERADE 
iptables -L -n

But after these changes I still can't connect with a simple telnet. (Which was possible with my first solution). The guest machine doesn't have any firewall.

I only have one network interface on the host (eth0) and a host interface (vboxnet0).

Any suggestions? Or should I go back to my old solution (which I don't really like).

Edit: bridge mode isn't an option, I have only on IP available for the moment.

Thanks!

© Server Fault or respective owner

Related posts about ubuntu

Related posts about iptables