Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there a "best practice" or standard to make a few iptables rules permanent? I mean: automatically applied upon a system reboot?

I am using a VPS with Ubuntu Server 10.04 LTS (Lucid Lynx).

Thank you.

BIG EDIT: I don't want ANY rule to be persisted (like iptables-persistent package does). I want only my own specific set to be reloaded... if other rules are eventually added by running iptables, these should be discarded...

share|improve this question
up vote 27 down vote accepted

The simplest method is to use iptables-save and iptables-restore to save the currently-defined iptables rules to a file and (re)load them (e.g., upon reboot).

So, for instance, you would run sudo iptables-save > /etc/iptables.conf to save your current iptables rules to /etc/iptables.conf, and then insert these lines in /etc/rc.local:

# Load iptables rules from this file
iptables-restore < /etc/iptables.conf
share|improve this answer
+1 Your answer is perfect to my original question. It also made me realize what I really want, so I had to change the question from "How can I make iptables rules permanent?" to "How can I make a specific set of iptables rules permanent?" – J. Bruni Oct 15 '11 at 15:15
If you can, please, do a little edit to your answer to reflect the change in the question... I'll be happy to accept it! I need to use iptables-save once, and reload always (iptables-restore at /etc/rc.local sounds good). – J. Bruni Oct 15 '11 at 15:18
@J.Bruni Done, thanks! – Riccardo Murri Oct 15 '11 at 15:25
If you're getting permission denied when you try to use > then this Stack Overflow answer will be useful. – David Tuite Feb 13 '14 at 14:40
Use sudo sh -c "iptables-save > /etc/iptables.conf" instead of sudo iptables-save > /etc/iptables.conf – user01 Mar 4 '14 at 13:17

Better than /etc/rc.local is to add a line in /etc/network/interfaces after saving the iptable's rules, like this

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

or it's the same to put the file inside /etc/network/if-down.d/ or /etc/network/if-post-down.d/ or /etc/network/if-pre-up.d/ or /etc/network/if-up.d/.

share|improve this answer
Thank you for this valuable contribution. – J. Bruni May 3 '12 at 12:46
@Kreker: please do clarify, how your approach is better than executing a restoring script from rc.local ? does the other approach not work in some cases ? – user01 Mar 16 '14 at 17:27
Adding it to the network configuration causes it to be re-loaded after you run the "service networking restart" command. It's not limited to system starts. – K. Darien Freeheart Apr 23 '14 at 19:11
Why would one post-up iptables-restore when an interface is going down? – alphadogg Aug 15 '14 at 14:28

A Quick Update to this as you might be using 12.04 now and things are better.

The iptables-persistent package now solves this issue. To install,

sudo apt-get install iptables-persistent

The rules defined when the package is installed are saved and used on each subsequent boots. New rules loaded are discarded at reboot.

The config file if you do need to change them (once iptables-persistent is installed) is /etc/iptables/rules.v4 or /etc/iptables/rules.v6 for ipv4 and ipv6 iptables respectively.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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