[How To] Save and auto load rules of iptables (Lazymode)

At first, it’s a good idea to have some iptables rules to save and then reload. For example, let’s create following rules:

$ sudo iptables -F 
$ sudo iptables -X 
$ sudo iptables -P OUTPUT ACCEPT 
$ sudo iptables -A INPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT 
$ sudo iptables -A INPUT -i lo -j ACCEPT 
$ sudo iptables -A INPUT -p tcp -m tcp --dport 80 -j REJECT 
$ sudo iptables -P INPUT DROP

Save (Make sure that these rules are not dangerous for your computer configuration)

$ sudo iptables-save

Saving the rules and setting up their auto load.

$ sudo echo "#! /sbin/iptables-restore" > /etc/network/if-up.d/iptables-rules
$ sudo iptables-save >> /etc/network/if-up.d/iptables-rules
$ sudo chmod +x /etc/network/if-up.d/iptables-rules

Please check that the file is created and made executable.

$ sudo ls -lA /etc/network/if-up.d/ipt*

It should looks like:

-rwxr-xr-x 1 root root 1214 2018-06-10 19:08 /etc/network/if-up.d/iptables-rules

Reboot computer

$ sudo reboot

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.