<p>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:</p>
<pre><code>$ sudo iptables-save &gt; /root/my-iptables.rules
</code></pre>
<p>To restore it just use the command iptables-restore:</p>
<pre><code>$ sudo iptables-restore &lt; /root/my-iptables.rules
</code></pre>
<p>⚠️ 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.</p>

CC BY-SA 4.0
2020-07-13T10:21:21.617Z
2020-07-13T10:21:21.617Z
Amintabar
<p>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.</p>
<p>The standard commands to do this on Debian are:</p>
<pre><code>sudo iptables-save &gt; /etc/iptables.up.rules
sudo iptables-restore &lt; /etc/iptables.up.rules
</code></pre>
<p><strong>DO NOT EDIT THIS FILE</strong></p>
<blockquote>
<p>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.</p>
</blockquote>
<p>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.</p>
<p>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.</p>
<p>If i messed up, a server reboot, using the management panel for the server would at least restore the previous IP table rules.</p>

CC BY-SA 4.0
2020-07-14T06:09:58.253Z
2020-07-14T06:09:58.253Z
Michael Shaw
<p>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?!</p>

<linux><networking>
How to Save and Restore the Iptables rule and configuration from file?
Amintabar
# How to Save and Restore the Iptables rule and configuration from file?

(Asked by: username_0 on 2020-07-13)

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?!

**Tags:** linux, networking


---

## 2 answers

### Answer by username_0 on 2020-07-13. Score: 2

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.


---

### Answer by username_1 on 2020-07-14. Score: 0

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.


---

