Redirect and Save iptables on Ubuntu 12.04

less than 1 minute read

Redirect port 8080 to 80

sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8080  

Check iptables setting

sudo iptables -t nat -L  

Save configure to iptables.rules

sudo iptables-save > /etc/iptables.rules  

Save Solution #1

Configre /etc/network/interfaces

iface eth0 inet dhcp  
    pre-up iptables-restore < /etc/iptables.rules  

Save Solution #2

Configure /etc/network/if-pre-up.d/iptablesload

iptables-restore < /etc/iptables.rules
exit 0

Configure /etc/network/if-post-down.d/iptablessave

iptables-save -c > /etc/iptables.rules
if [ -f /etc/iptables.downrules ]; then
    iptables-restore < /etc/iptables.downrules
exit 0

Change permissions

sudo chmod +x /etc/network/if-post-down.d/iptablessave  
sudo chmod +x /etc/network/if-pre-up.d/iptablesload  

Tomcat: redirecting traffic from port 8080 to 80 using iptables

Leave a Comment