Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy IP Tables rules from one server to another [closed]

I have a server with iptables rules setup. I want to use the same rules on a new server. Can I somehow copy/paste them or download/re-upload them using terminal? I'm on Ubuntu.

like image 299
Jared Eitnier Avatar asked Jan 24 '13 15:01

Jared Eitnier


People also ask

How do I copy iptables from one server to another?

Import Iptables Rules Once you are ready to load the rules from the iptables-export file into iptables, let's use the iptables-restore command to do so. On Server B, the destination server, run this command to load the firewall rules: sudo iptables-restore < /tmp/iptables-export.

How do I dump iptables rules?

To flush a specific chain, which will delete all of the rules in the chain, you may use the -F , or the equivalent --flush , option and the name of the chain to flush. For example, to delete all of the rules in the INPUT chain, run this command: sudo iptables -F INPUT.

Does iptables rules persistent after reboot?

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.


2 Answers

Yes. Save it as follows:

sudo iptables-save > iptables.conf

Restore it as follows:

sudo iptables-restore < iptables.conf

And since it’s all in a text file—in this case iptables.conf—you can then do further editing and tweaks based on machine specific parameters and scenarios.

Also, if you plan on retaining rules on reboot, consider having iptables-persistent installed and then copying the rules into the area that iptables-persistent loads them:

sudo cp iptables.conf /etc/iptables/rules.v4

Above is for IPv4 rules. For for IPV6 rules do this:

sudo cp iptables.conf /etc/iptables/rules.v6
like image 122
Giacomo1968 Avatar answered Oct 21 '22 22:10

Giacomo1968


Use

sudo iptables-save > [filename]

to save them in a file, then use

sudo iptables-restore < [filename]

once you've copied the file over.

like image 24
Ian Atkin Avatar answered Oct 21 '22 23:10

Ian Atkin