注意说明:确保firewall防火墙是开启状态具体可用

systemclt status firewalld.service

或者service firewalld status进行查看是否在开启状态。

以5555端口进行举例:

1、执行如下语句关闭5555端口--移除公共区域5555端口

systemctl enable firewalld

systemctl start firewalld

firewall-cmd --set-default-zone=public

firewall-cmd --zone=public --remove-port=5555/tcp --permanent

2、允许其他主机指定ip允许访问本机的5555端口

firewall-cmd --permanent --add-rich-rule=&34;rule family=&34;ipv4&34; source address=&34;其他主机ip&34; port protocol=&34;tcp&34; port=&34;5555&34; accept&34;

移除其他主机指定ip允许访问本机5555端口firewall-cmd --permanent --remove-rich-rule=&34;rule family=&34;ipv4&34; source address=&34;其他主机ip&34; port protocol=&34;tcp&34; port=&34;5555&34; accept&34;

允许指定ip源进行访问本机firewall-cmd --permanent --add-source=其他主机ip

允许指定ip段进行访问本机firewall-cmd --permanent --add-source=其他主机ip段/24

允许指定ip段进行访问本机的5555端口到6666端口firewall-cmd --permanent --add-rich-rule=&34;rule family=&34;ipv4&34; source address=&34;其他主机ip段/24&34; port protocol=&34;tcp&34; port=&34;5555-6666&34; accept&34;

禁止指定ip访问本机的5555端口firewall-cmd --permanent --add-rich-rule=&34;rule family=&34;ipv4&34; source address=&34;其他主机ip&34; port protocol=&34;tcp&34; port=&34;5555&34; reject&34;

注意如需移除设置防火墙对应的规则,把add替换为remove即可

3、重新加载防火墙配置、重启防火墙服务

firewall-cmd --reload

systemctl restart firewalld

frewall-cmd --list-all 查看防火墙规则-是否设置成功

其他查看方式:firewall-cmd --get-active-zones 查看区域信息

firewall-cmd --list-all-zones