Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a small problem with getting a proper configuration of iptables under Ubuntu. But before, let me describe the network configuration:

I am using xDSL line terminated with modem+router+DHCP serving 192.168.1.0/24 subnet. My laptop and PC are connected to this network and get addresses 192.168.1.6 and 192.168.1.3 respectively. On PC, I have Ubuntu running with VBox and 2 virtual machines (both Ubuntu as well). The vboxnet0 interface on PC is assigned IP 10.10.10.1 and individual virtual machines get 10.10.10.10 and 10.10.10.11. VM1 runs Apache webserver for test purposes.

Now, from my PC (192.168.1.3), I can access VM1 Apache hosted website at 10.10.10.10 by typing the address in the browser. No problems there.

However, I would like to be able to access the same website from my laptop (192.168.1.6), by typing the same 10.10.10.10 in the address bar in the browser. This does not work, though. I cannot get through the network for some reason. It seems that the ip address is just unknown on the 192.168.1.x network. If there is a way to configure that in a simple manner via iptables, it would be a great start.

Now, if that step is possible, I would further like to be able to configure a more complex setup. Using 192.168.1.3 address and proper port address, I would like to be able to forward specific service requests to proper VM instance. For example, 192.168.1.3:80 should go to VM1 and fetch the Apache hosted website. 192.168.1.3:4000 (for example), should go to VM2 and get the SFTP service hosted there. In short, I would like to perform destination port specific routing on the PC to allow for bidirectional communication between computers connected to 192.168.1.x networks and VMs running in 10.10.10.x network. Is there a simple solution for this using iptables?

here is an example that I have cooked up by now, but have not been able to make the forwarding work correctly. I am sure there are tons of errors here - it is my first day spent on iptables.

clear
# cleaning Firewall Rules , change ACCEPT to DROP if you want to shield
# the server, then you open ports as you need
iptables -F
iptables -X
iptables -t nat -F
iptables -t nat -X
iptables -t mangle -F
iptables -t mangle -X
iptables -P INPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -P OUTPUT ACCEPT

# Accepts all established inbound connections
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

# opening INPUT ports (22,80,8080)
iptables -A INPUT --protocol tcp --dport 22 -j ACCEPT && echo "rule input 22 ok"
iptables -A INPUT --protocol tcp --dport 80 -j ACCEPT && echo "rule input 80 ok"
iptables -A INPUT --protocol tcp --dport 443 -j ACCEPT && echo "rule input 443 ok"
iptables -A INPUT --protocol tcp --dport 8080 -j ACCEPT && echo "rule input 8080 ok"

#allow Loopback and networks
iptables -A INPUT -i lo -j ACCEPT  && echo "rule 7 ok"
#Accept any input from 10.10.10.0 network in vboxnet0 interface
iptables -A INPUT -s 10.10.10.0/24  -i vboxnet0 -j ACCEPT  && echo "rule 8 ok"

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

# Opening PREROUTING - Filtering : this make the port forwarding trick.
# Forward as many ports you want to certain machines of the network to provide services such web server, ftp server, etc...
iptables -t nat -A PREROUTING -p tcp -i eth1 -d 192.168.1.0/24 --dport 8080 -j DNAT --to 10.10.10.10:80  && echo "rule 9 ok"
#iptables -t nat -A PREROUTING -p tcp -i eth1 -d xxx.xxx.xxx.xxx --dport 53 -j DNAT --to 10.10.10.14:53  && echo "rule 10 ok"
#iptables -t nat -A PREROUTING -p udp -i eth1 -d xxx.xxx.xxx.xxx --dport 53 -j DNAT --to 10.10.10.14:53  && echo "rule 11 ok"
#iptables -t nat -A PREROUTING -p udp -i eth1 -d xxx.xxx.xxx.xxx --dport 21 -j DNAT --to 10.10.10.16:21  && echo "rule 12 ok"

#Opening FORWARD ports for network services on vlan
iptables -A FORWARD -s 10.10.10.0/24 -i vboxnet0 -p tcp --dport 80 -j ACCEPT && echo "rule 13 ok"
#iptables -A FORWARD -s 10.10.10.0/24 -i vboxnet0 -p tcp --dport 21 -j ACCEPT  && echo "rule 14 ok"
#iptables -A FORWARD -s 10.10.10.0/24 -i vboxnet0 -p tcp --dport 68 -j ACCEPT  && echo "rule 15 ok"
#iptables -A FORWARD -s 10.10.10.0/24 -i vboxnet0 -p tcp --dport 22 -j ACCEPT  && echo "rule 16 ok"
#iptables -A FORWARD -s 10.10.10.0/24 -i vboxnet0 -p tcp --dport 53 -j ACCEPT && echo "Rule 17 ok"
#iptables -A FORWARD -s 10.10.10.0/24 -i vboxnet0 -p udp --dport 53 -j ACCEPT && echo "Rule 18 ok"

# Opening POSTROUTING PROCESSES
# Netmasking is absolutelly necesary to protect vlan from attacks, only it hides their ip....
iptables -t nat -A POSTROUTING -s 10.10.10.0/24 -o eth1 -j MASQUERADE  && echo "rule 19 ok"

# Reject all other inbound - default deny unless explicitly allowed policy
iptables -A INPUT -j REJECT
iptables -A FORWARD -j REJECT

# test and display the rules if runs properly
iptables -L

Thank you for any help

Marek

share|improve this question

1 Answer 1

Firstly, you should consider the simpler setup of using bridged networking in VirtualBox. This will essentially put the VMs directly on the LAN, so they will get 192.168.1.0/24 addresses and can be accessed directly from other machines on the LAN.

Your DNAT rule looks correct, but I suspect that it's being blocked in the FORWARD chain. NATed packets still need to traverse the FORWARD chain, and so they need to be accepted there, with something like this:

iptables --append FORWARD --in-interface eth1 --out-interface vboxnet0 --destination 10.10.10.10 --protocol tcp --dport 80 --jump ACCEPT

However, I would suggest that you configure the FORWARD chain to ACCEPT by default while you are getting the NAT rules working. Once the NAT is working, then start restricting the FORWARD chain.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.