- Save all of current rules:
iptables-save > /etc/iptables/iptables-rules.conf
-
Make a bash script file in
vim /etc/iptables/flush-iptables.sh
to flush iptables rules. -
Add the following commands to
/etc/iptables/flush-iptables.sh
:
iptables -P INPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -P OUTPUT ACCEPT
iptables -F
iptables -X
iptables -tnat -F
iptables -tnat -X
iptables -tmangle -F
iptables -tmangle -X
iptables -traw -F
iptables -traw -X
- Make it executable:
chmod +x /etc/iptables/flush-iptables.sh
- Write a systemd service for iptables rules, create a file on
/etc/systemd/system/iptablesd.service
and copy the following lines to it:
[Unit]
Description=Iptables Persistency Framework
DefaultDependencies=no
After=systemd-sysctl.service
Before=sysinit.target
[Service]
Type=oneshot
ExecStart=/sbin/iptables-restore /etc/iptables/iptables-rules.conf
ExecReload=/sbin/iptables-restore /etc/iptables/iptables-rules.conf
ExecStop=/etc/iptables/flush-iptables.sh
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
- Then run these commands to run service:
systemctl daemon-reload
systemctl enable iptablesd.service
systemctl start iptablesd.service