To save and restore iptables rules on Ubuntu Linux, you can use the iptables-save
and iptables-restore
commands. The iptables-save
command writes the current iptables rules to a file, while the iptables-restore
command reads the rules from a file and applies them to the system.
Here are the steps to save and restore iptables rules on Ubuntu Linux:
- Open a terminal or SSH session to the Ubuntu system.
- To save the current iptables rules to a file, type the following command:
sudo iptables-save > /etc/iptables.rules
This command writes the current iptables rules to the file /etc/iptables.rules
. You can choose any filename and location for the rules file.
- To restore the iptables rules from a file, type the following command:
sudo iptables-restore < /etc/iptables.rules
This command reads the iptables rules from the file /etc/iptables.rules
and applies them to the system.
- If you want the iptables rules to be automatically loaded at boot time, you can add a script to the system startup process. On Ubuntu, you can create a script in the
/etc/network/if-up.d/
directory that loads the iptables rules when a network interface is brought up.
For example, create a new file called /etc/network/if-up.d/iptablesload
with the following contents:
iptables-restore < /etc/iptables.rules
Save the file and make it executable with the following command:
sudo chmod +x /etc/network/if-up.d/iptablesload
This script will automatically load the iptables rules from the file /etc/iptables.rules
when a network interface is brought up.
That’s it! You have successfully saved and restored iptables rules on Ubuntu Linux, and configured the system to automatically load the rules at boot time.