I add this rule:
sudo iptables -t nat -A OUTPUT -d a.b.c.d -p tcp \
--dport 1723 -j DNAT --to-destination a.b.c.d:10000
- When restart computer rules are deleted. Why?
- What I can do to make the rules persist?
I add this rule:
sudo iptables -t nat -A OUTPUT -d a.b.c.d -p tcp \
--dport 1723 -j DNAT --to-destination a.b.c.d:10000
On Debian, install iptables-persistent
:
sudo apt-get install iptables-persistent
The package will automatically load /etc/iptables/rules
for you during boot.
Any time you modify your rules, run /sbin/iptables-save > /etc/iptables/rules
to save them. You can also add that to the shutdown sequence if you like.
There is no option in iptables which will make your rules permanent. But you can use iptables-save
and iptables-restore
to fulfill your task.
First add the iptable rule using the command you gave.
Then save iptables rules to some file like /etc/iptables.conf
using following command:
$ iptables-save > /etc/iptables.conf
Add the following command in /etc/rc.local
to reload the rules in every reboot.
$ iptables-restore < /etc/iptables.conf
iptables-persistent
package which will do this.
– bahamat
Oct 20 '12 at 00:09
rc.local
since there would be an open window gap between services start and firewall policy apply. I do prefer using pre-up
hook for loopback interface in /etc/network/interfaces
to overcome this.
– poige
Oct 20 '12 at 11:07
rc.local
might have the intended effect, but it's a kludge in this situation.
– J. M. Becker
Oct 20 '12 at 14:52
After installing iptables-persistent
above you can also save rules with the following shorter command on Ubuntu 16.04+:
sudo netfilter-persistent save
And they can also be restored back to how they were last time you saved them with:
sudo netfilter-persistent reload
Because you did not save the iptables rules.
You can do that by using sudo iptables-save
iptables-persistent
packageiptables-save -f /etc/iptables/rules.v4
(for iptables)
iptables-save -f /etc/iptables/rules.v6
(for ip6tables)
First install the persist iptables (ubunut or debian)
apt install iptables-persistent
Run your statement:
iptables -A INPUT -s 0/0 -p tcp --dport 5433 -j ACCEPT
Then save the settings
iptables-save
Finally restart the machine to verify
reboot
iptables-save
does not save anything. Read the manual: man iptables-save
.
– stackprotector
Mar 11 '22 at 15:39
/etc/iptables/rules.v4
and/etc/iptables/rules.v6
for IPv4 and IPv6 respectively. If you want a table to apply to both kinds of connections you have to save it to both rule files. – PetaspeedBeaver Jan 23 '14 at 15:51ip6tables
andip6tables-save
command. So, it'siptables-save > /etc/iptables/rules.v4
for IPv4 iptables rules andip6tables-save > /etc/iptables/rules.v6
for IPv6 iptables rules. – miu Aug 30 '23 at 14:13