30

My system admin gave me a file with iptables rules. What command do I type in to load this?

I watched him do it before, and he did it in 1 line! Something like...iptables > thefile.dat ????

3 Answers 3

46

My system admin gave me a file with iptables rules. What command do I type in to load this?

iptables-restore < file-with-iptables-rules.txt

I watched him do it before, and he did it in 1 line! Something like...iptables > thefile.dat ????

iptables-save > file-with-iptables-rules.txt
8
  • Is it possible to import from a URL? eg: iptables-restore < http://git.company.com/Company/iptables-rules/blob/master/iptables-rules.txt
    – Jay
    Jun 6, 2017 at 23:29
  • I believe the iptables-restore command functions local to the filesystem; however, nothing stops you from piping a data stream into it via curl or wget. So in theory, instead of just specifying a URL, you would fetch the contents of the file and pipe that to the command. Jun 7, 2017 at 21:07
  • Thanks @avery-payne Can you demonstrate what that might look like?
    – Jay
    Jun 8, 2017 at 18:07
  • 1
    @Jay Probably something similar to curl -s SOME_URL | iptables-restore. Jun 13, 2017 at 8:02
  • If you want to do this with sudo you'll also need to use tee: sudo iptables-save | sudo tee /etc/iptables/rules.txt sudo iptables-restore /etc/iptables/rules.txt
    – jorfus
    Aug 17, 2017 at 16:49
9

You load an iptables configuration file using iptables-restore

iptables-restore thefile.dat

This has effect immediately. However for this settings to be persistent over system reboot they must be saved. On most distributions this can be done with.

/etc/init.d/iptables save

As mentioned in other answers the configuration can be saved using

iptables-save > thesavefile.dat

The file itself is a text file and can be edited with any texteditor and then reloaded into iptables using the iptables-restore command.

0
3

To import an iptables script into your ruleset

iptables-restore < /path/to/firewall_script
3

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .