Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to permanently update iptables [closed]

I'm trying to redirect http traffic to port 8080 on the same machine and have the iptables rules below working.

iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-ports 8080 iptables -t nat -A OUTPUT -p tcp --dport 80 -j REDIRECT --to-ports 8080 

I'm trying to figure out how to make this change permanent incase of a reboot of the system.

I'm using Ubuntu 11.10 server.

like image 823
hafichuk Avatar asked Feb 17 '12 15:02

hafichuk


People also ask

Are iptables changes permanent?

That is because iptables rules, by default, will not persist after a reboot. After configuring your system's iptables rules, there is one more important step thay you must do in order to make sure the rules are still there after a reboot.

How do I save all iptables rules?

You need to use the iptables-save command. It is used to dump the contents of an IP Table in easily parseable format to screen. Using I/O-redirection provided by your shell you can save iptables firewall rules to a text file. To restore iptables rules use the iptables-restore command.


2 Answers

Ubuntu (and Debian) offer the package iptables-persistent (Debian: http://packages.debian.org/wheezy/iptables-persistent , Ubuntu: http://packages.ubuntu.com/saucy/iptables-persistent) , which does exactly what you want. As root, or via sudo:

apt-get install iptables-persistent iptables-save > /etc/iptables/rules.v4 

If you're working with ip6tables, you'll want to also ip6tables-save > /etc/iptables/rules.v6.

You must save the tables again (iptables-save > /etc/iptables/rules.v4, ip6tables-save > /etc/iptables/rules.v6) after any change you make.

On older versions (before iptables-0.5, and before Debian Wheezy) you will need write to a different file:

iptables-save > /etc/iptables/rules 
like image 130
yomimono Avatar answered Oct 03 '22 03:10

yomimono


One way to do this would be:

vim /etc/network/interfaces 

Append the below line along with your lo directives:

post-up /sbin/iptables-restore < /etc/iptables-up.rules 

Now run the below command

iptables-save > /etc/iptables-up.rules 

I hope this helps.

like image 39
vinod_garag Avatar answered Oct 03 '22 04:10

vinod_garag