Wednesday, August 28, 2013

NATting with iptables on Ubuntu

/etc/sysctl.conf:
net.ipv4.ip_forward = 1

sysctl -p /etc/sysctl.conf

/etc/init.d/networking restart

# list rules
iptables -L
iptables -t nat -L
iptables -t nat -L -n -v

# flush rules & NATs
iptables --flush
iptables --table nat --flush

iptables -t nat -A PREROUTING -p tcp –-dport 80 -j DNAT –-to-destination 192.168.22.122:80
iptables -t nat -A PREROUTING -p tcp –-dport 3306 -j DNAT –-to-destination 192.168.22.122:3306
iptables -t nat -A PREROUTING -p tcp –-dport 2222 -j DNAT –-to-destination 192.168.22.122:22

iptables -t nat -A POSTROUTING -j MASQUERADE


####### Auto Loading Rules at System Start-up #######

#1 save rules to file
iptables-save > /etc/iptables.rules

#2 find interfaces
iwconfig
ifconfig

#3 edit config file
vi /etc/network/interfaces

#4 add line to relevent interface (iface eth1)
pre-up iptables-restore < /etc/iptables.rules