Wednesday, April 26, 2023

Backing Up and Restoring iptables Rules

To backup iptables rules, we can use iptables-save command. This command is to save current iptables rules into a file called iptables.backup
sudo iptables-save > iptables.backup

To restore, simply use iptables-restore. This command is to restore iptables rules from a backup file called iptables.backup
sudo iptables-restore iptables.backup

We can also test the backup file, before committing the ruleset to sytem
sudo iptables-restore -tv iptables.backup

Where -t is for testing, and -v is for verbose.

Wednesday, April 12, 2023

Flushing and Clearing iptables Rules

Sometimes we need to clear out iptables, and start from scratch in setting up firewall rules. In order to do that, below are the commands to follow

sudo iptables -t nat -F
sudo iptables -t mangle -F
sudo iptables -F
sudo iptables -X

The first command flush out rules in nat table. The second one will flush rules in mangle table. The third command will flush all rules in all chains. The last one will delete all non default chains in iptables.

In order to verify, we can use below command:
sudo iptables -L -n

The above command will print out all rules in all chains, and all port and ip address will be printed in numeric format. 

Cleared out iptables will look like below: