Skip to content

Latest commit

 

History

History
58 lines (48 loc) · 1.29 KB

iptables-persistency.md

File metadata and controls

58 lines (48 loc) · 1.29 KB

HOW TO MAKE IPTABLES RULES PERSISTENT ON UBUNTU 18.04

  1. Save all of current rules:
iptables-save > /etc/iptables/iptables-rules.conf
  1. Make a bash script file in vim /etc/iptables/flush-iptables.sh to flush iptables rules.

  2. 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
  1. Make it executable:
chmod +x /etc/iptables/flush-iptables.sh
  1. 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
  1. Then run these commands to run service:
systemctl daemon-reload
systemctl enable iptablesd.service
systemctl start iptablesd.service