Centos 7 save iptables settings
Problem: iptables resets to default settings after server reboot.
I'm trying to set rule like this:
iptables -I INPUT -p tcp --dport 3000 -j ACCEPT
after that I do:
service iptables save
and it writes back something like this
iptables: Saving firewall rules to /etc/sysconfig/iptables:[ OK ]
and after this I just ran (this was done once):
chkconfig iptables on
(I have read that this has to be done in order to restore settings after reboot)
After that I reboot and run this command:
systemctl list-unit-files | grep iptables
and I see that iptables.service is enabled, however, the rule (to open port 3000) does not work anymore.
How do I persist these settings?
CentOS 7 is using FirewallD now! Use the --permanent
flag to save settings.
Example:
firewall-cmd --zone=public --add-port=3000/tcp --permanent
Then reload rules:
firewall-cmd --reload
Disable firewalld by the following command:
systemctl disable firewalld
Then install iptables-service by following command:
yum install iptables-services
Then enable iptables as services:
systemctl enable iptables
Now you can save your iptable rules by following command:
service iptables save
On CentOS 7 Minimal you may need to install the iptables-services
package (thanks to @RichieACC for the suggestion):
sudo yum install -y iptables-services
And then enable the service using systemd
:
sudo systemctl enable iptables.service
And run the initscript to save your firewall rules:
sudo /usr/libexec/iptables/iptables.init save
iptables-save > /etc/sysconfig/iptables
will save the current configuration without the need to install any other libraries or services.