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 problem was solved

do.

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:

 #!/bin/sh
 iptables-restore < /etc/iptables.rules
 exit 0

 4. After, in /etc/network/if-post-down.d/iptables,put:
 #!/bin/sh
 iptables-save -c > /etc/iptables.rules
 if [ -f /etc/iptables.rules ]; then
 iptables-restore < /etc/iptables.rules
 fi
 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: https://help.ubuntu.com/community/IptablesHowTo#Saving_iptables Good luck!

Best Answer


The easy way is to use iptables-persistent .

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 with a 16.04 server. The following commands work with that version.

sudo netfilter-persistent save
sudo netfilter-persistent reload