17

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 ????

27

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
  • 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 '17 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. – Avery Payne Jun 7 '17 at 21:07
  • Thanks @avery-payne Can you demonstrate what that might look like? – Jay Jun 8 '17 at 18:07
  • 1
    @Jay Probably something similar to curl -s SOME_URL | iptables-restore. – samthecodingman Jun 13 '17 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 '17 at 16:49
6

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.

3

To import an iptables script into your ruleset

iptables-restore < /path/to/firewall_script

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.