Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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
add comment

3 Answers 3

up vote 11 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
1  
If you're getting permission denied when you try to use > then this Stack Overflow answer will be useful. –  David Tuite Feb 13 at 14:40
    
Use sudo sh -c "iptables-save > /etc/iptables.conf" instead of sudo iptables-save > /etc/iptables.conf –  user01 Mar 4 at 13:17
add comment

Better than /etc/rc.local is to add a line in /etc/network/interface/ 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/interface/if-down.d/ or /etc/network/interface/if-post-down.d/ or /etc/network/interface/if-pre-up.d/ or /etc/network/interface/if-up.d/ as your needs.

share|improve this answer
1  
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 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 at 19:11
add comment

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.

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

share|improve this answer
add comment

Your Answer

 
discard

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.