# 开启防火墙
systemctl start firewalld.service
# 防火墙开机启动
systemctl enable firewalld.service
# 关闭防火墙
systemctl stop firewalld.service
# 查看防火墙状态
firewall-cmd --state
# 查看现有的规则
iptables -nL
firewall-cmd --zone=public --list-ports
# 重载防火墙配置
firewall-cmd --reload
# 添加单个单端口
firewall-cmd --permanent --zone=public --add-port=81/tcp
# 添加多个端口
firewall-cmd --permanent --zone=public --add-port=8080-8083/tcp
# 删除某个端口
firewall-cmd --permanent --zone=public --remove-port=81/tcp
# 删除 ssh 端口
firewall-cmd --remove-port=22/tcp --permanent
firewall-cmd --remove-service=ssh --permanent
# 针对某个 IP开放端口
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.142.166" port protocol="tcp" port="6379" accept"
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.0.233" accept"
# 删除某个IP
firewall-cmd --permanent --remove-rich-rule="rule family="ipv4" source address="192.168.1.51" accept"
# 针对一个ip段访问
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.0.0/16" accept"
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.1.0/24" port protocol="tcp" port="9200" accept"
# 添加操作后别忘了执行重载
firewall-cmd --reload
# 查看当前起作用的zone
firewall-cmd --get-active-zones
当遇到不方便重载的时候,可以使用
firewall-cmd --zone=public --add-port=80/tcp
# 在 /etc/firewalld/zones/public.xml 下 <zone> </zone>标签中添加
<port protocol="tcp" port="80"/>
# 或 针对源IP地址端添加
<rule family="ipv4">
<source address="192.168.1.0/24"/>
<port protocol="tcp" port="80"/>
<accept/>
</rule>