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

This question already has an answer here:

I create the rules to iptables. But, when I restart the computer, the rules don't work! How to save the rules on Ubuntu ?

The was problem solved!


After of the write the commands iptables, do:

 1. sudo su
 2. iptables-save > /etc/iptables.rules
 3. In /etc/network/if-pre-up.d/iptables,put:

 iptables-restore < /etc/iptables.rules
 exit 0

 4. After, in /etc/network/if-post-down.d/iptables,put:
 iptables-save -c > /etc/iptables.rules
 if [ -f /etc/iptables.rules ]; then
 iptables-restore < /etc/iptables.rules
 exit 0
 5. After, give permission to the scripts:
 sudo chmod +x /etc/network/if-post-down.d/iptables
 sudo chmod +x /etc/network/if-pre-up.d/iptables

More information: Good luck!

share|improve this question

marked as duplicate by guntbert, Jorge Castro, BuZZ-dEE, 123456, Avinash Raj May 22 '14 at 2:22

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

I've done everything! And don't work!! – Gustavo Apr 6 '12 at 6:27
It would be helpful if you updated the ticket with specifics on what you have tried, on what results you have encountered. It would also be good to know whatever it is a desktop install or a server install; whatever it uses NetworkManager or not. – andol Apr 6 '12 at 6:30
Since you found a solution, you should accept the answer that led you to that result or you should put your answer content in the "answer" box and then accept your own answer. This allows this question to be marked as "answered" in the system, which will help other users if they have a similar problem. Have a good day! – weberc2 Apr 6 '12 at 20:24

please use iptables-persistent; it's the easy way: Install iptables-persistent:

    sudo apt-get install iptables-persistent

After it's installed, you can save/reload iptables rules anytime:

    sudo /etc/init.d/iptables-persistent save 
    sudo /etc/init.d/iptables-persistent reload

Ubuntu 16.04 Server

The installation as described above works without a problem, but the two commands for saving and reloading above do not seem to work in 16.04 server. These commands did work in 16.04 server:

    sudo apt-get install iptables-persistent

    sudo netfilter-persistent save
    sudo netfilter-persistent reload
share|improve this answer

The generic method of saving iptables rules is to use the command iptables-save, which writes to stdout.

iptables-save > /etc/network/iptables.rules

The output created by iptables-save can then by read on stdin by iptables-restore. If on a server, without NetworkManager, a common approach is then to use a pre-up command in /etc/network/interfaces.

iface eth0 inet static
        pre-up iptables-restore < /etc/network/iptables.rules

If you are using NetworkManager it should be possible to run the same command from a script created under /etc/NetworkManager/dispatcher.d/. In the Community Documentation - iptables howto, see Configuration on Startup for NetworkManager for more information.

Do note that the commands iptables, iptables-save and iptables-restore are IPv4 only. For IPv6 traffic the equivalent commands are ip6tables, ip6tables-save and ip6tables-restore.

share|improve this answer

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