執行
service iptables save
報錯
The service command supports only basic LSB actions (start, stop, restart, try-restart, reload, force-reload, status). For other actions, please try to use systemctl.
原因
從 CentOS 7.x 開始,CentOS 開始使用 systemd 服務來代替 daemon,原來管理系統啟動和管理系統服務的相關指令全部由 systemctl 指令來代替。service 指令之保留了極少部分使用,大部分指令都要改用 systemctl 指令來使用。
在 RHEL 7 和 CentOS 7 中, firewalld 被引入來管理 iptables。
解決方案
首先停止防火牆:
systemctl stop firewalld
systemctl mask firewalld
在 CentOS 7 和 RHEL 7 中,沒有 /etc/sysconfig/iptables 這個配置檔案,也不能執行 service iptables restart 指令,需要通過安裝 iptables-services 才有。
# 檢視目前系統版本
[[email protected] ~]# cat /etc/redhat-release
CentOS Linux release 7.3.1611 (Core)
[[email protected] ~]# rpm -qa|grep iptables
iptables-1.4.21-35.el7.x86_64
iptables-services-1.4.21-35.el7.x86_64
[[email protected] ~]# yum -y install iptables-services
然後就可以使用 service iptables [start | stop | restart | save …] 指令
# 這樣就可以儲存防火牆規則了
[[email protected] ~]# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[ OK ]
[[email protected] ~]# ll /etc/sysconfig/iptables
-rw-------. 1 root root 6479 Nov 7 04:00 /etc/sysconfig/iptables
# 或者 使用如下指令
[[email protected] ~]# /usr/libexec/iptables/iptables.init save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[ OK ]
添加端口
# 添加9000
iptables -I INPUT 4 -p tcp -m state --state NEW -m tcp --dport 9000 -j ACCEPT
# 儲存規則
service iptables save