Centos7 防火墙管理

2020-12-11

centos7 默认使用 firewall,执行 iptables-services 会有相关报错,iptables.service: Unit iptables.service not loaded

解决办法:

# 安装 iptables-services
sudo yum install iptables-services

# 关闭 firewalld
systemctl stop firewalld

# 开机自启
systemctl enable iptables  或  chkconfig iptables on

# 启动
systemctl start iptables  或  service iptables start

 

扩展:

iptables-services

# 停止
systemctl stop iptables  或  service iptables stop
# 重启
systemctl restart iptables  或  service iptables restart
# 重载
systemctl reload iptables  或  service iptables reload
# 保存 iptables 的修改
service iptables save
# 关闭开机自启
systemctl disable iptables  或  chkconfig iptables off
# 开启端口 9899
iptables -I INPUT -ptcp --dport 9899 -j ACCEPT
# 修改配置文件,开放 80 端口
vim /etc/sysconfig/iptables
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
# 关端口
iptables -A  INPUT -p tcp --dport 9899 -j DROP
# 禁 ip 192.168.1.2
iptables -A INPUT -p tcp -s 192.168.1.2 -j DROP

 

firewall

# 临时打开 3306 端口,重启失效
firewall-cmd --add-port=3306/tcp
# 打开 80 端口,重启仍有效
firewall-cmd --zone=public --add-port=80/tcp --permanent
# 打开 1000-1500 端口
firewall-cmd --add-port=1000-1500/tcp --permanent
# 重新载入
firewall-cmd --reload
# 查看
firewall-cmd --query-port=80/tcp
# 删除
firewall-cmd --remove-port=80/tcp --permanent
# 启动 firewall
systemctl start firewalld.service
# 停止 firewall
systemctl stop firewalld.service
# 禁止 firewall 开机启动
systemctl disable firewalld.service
# 开启
service firewalld start
# 重启
service firewalld restart
# 关闭
service firewalld stop
# 查看防火墙规则
firewall-cmd --list-all 

 

ConstXiong 备案号:苏ICP备16009629号-3