10

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.

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
Jared Eitnier
  • 7,012
  • 12
  • 68
  • 123
  • 3
    This question belongs on [Server Fault](http://serverfault.com), not SO. – cdhowie Jan 24 '13 at 15:55
  • 1
    `iptables-save` and `iptables-restore` – JeffS Jan 24 '13 at 15:56
  • 1
    @cdhowie I think this is actually an edge case of a coding issue mixed with server issues. If you have ever handled iptable rules, there is far more programming logic at it's core than server know-how. But then again, it's basically a firewall so that is a server thing. So I believe this question should be here given the scope of what's asked. – Giacomo1968 Jan 24 '13 at 16:25
  • 1
    @JakeGould I disagree, but respect your opinion. – cdhowie Jan 24 '13 at 16:27

3 Answers3

20

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
Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
2

Use

sudo iptables-save > [filename]

to save them in a file, then use

sudo iptables-restore < [filename]

once you've copied the file over.

Ian Atkin
  • 6,302
  • 2
  • 17
  • 24
2

You may use iptables-save utility to save rules to file and iptables-restore to restore them form file.

And then use scp to copy saved rules form one serve to another.

bronislav
  • 782
  • 7
  • 27