I want to save the current iptables configuration to a file and restore it from a file. I have a lot of rules and modifying the rules really hard for me. So, how can i do that?!
If you want to modify the configuration and rules, so you must save the current configuration to a file. So step one is to save the rules configuration by typing the following commands:
$ sudo iptables-save > /root/my-iptables.rules
To restore it just use the command iptables-restore:
$ sudo iptables-restore < /root/my-iptables.rules
⚠️ When you changed the existing rules after saving the file, You should save it again or change the existing file and restore it from the file.
The first thing to point out, is that you should already be writing IP tables rules to a file, and restoring them from that file at start up - otherwise you will lose all of the rules when the server reboots.
The standard commands to do this on Debian are:
sudo iptables-save > /etc/iptables.up.rules sudo iptables-restore < /etc/iptables.up.rules
DO NOT EDIT THIS FILE
If you make a change that breaks your networking, killing your access to the server, then you can only recover by walking to the computer's console, and logging in there to fix the problem. If thats a Cloud server, or in a different city / country / continent you have a bigger problem.
The next issue that i want to cover, is if writing individual rules is hard, the config files just capture the same command that you would type into the terminal window. Using a configuration file does not reduce the complexity here.
Personally, I would use the existing file to help with the syntax (e.g. find something that already does something that i want to do), and then run that command manually in the shell, and verify that it does what i need it to do by testing the network connections carefully, and thoroughly. I would then save rules, knowing that they are already working and tested.
If i messed up, a server reboot, using the management panel for the server would at least restore the previous IP table rules.