CentOS 7.5 firewalld 常规使用(包括屏蔽IP、状态查询、端口开放/关闭等)

系统从6升级到7之后,默认的防火墙从iptables更换到了firewalld,使用起来差别很大,现列出常用的功能,便于查阅。

如果是新安装的系统,也没有开启过firewalld,那么建议使用firewall-offline-cmd命令开启ssh端口。因为firewall-cmd在firewalld未启动时不能进行设置,一旦开启(假如还未开启ssh端口),那么你将被断开连接。
#查看firewall状态;
firewall-cmd --state

#安装
yum install firewalld

#启动
systemctl start firewalld 

#设置开机启动
systemctl enable firewalld

#关闭
systemctl stop firewalld

#取消开机启动
systemctl disable firewalld

#禁止IP(123.44.55.66)访问机器,记得加 permanent 参数,否则系统重启后无效
firewall-cmd --permanent --add-rich-rule='rule family=ipv4 source address="123.44.55.66" drop'

#禁止一个IP段,比如禁止116.255.*.*
firewall-cmd --permanent --add-rich-rule='rule family=ipv4 source address="116.255.0.0/16" drop'

#禁止一个IP段,比如禁止116.255.196.*
firewall-cmd --permanent --add-rich-rule='rule family=ipv4 source address="116.255.196.0/24" drop'

#禁止机器IP(123.44.55.66)从防火墙中删除
firewall-cmd --permanent --remove-rich-rule='rule family=ipv4 source address="123.44.55.66" drop'

#允许http服务(对应服务策略目录:/usr/lib/firewalld/services/)
firewall-cmd --permanent --add-service=http

#关闭http服务(对应服务策略目录:/usr/lib/firewalld/services/)
firewall-cmd --permanent --remove-service=http

#允许端口:3389
firewall-cmd --permanent --add-port=3389/tcp

#允许端口:1-3389
firewall-cmd --permanent --add-port=1-3389/tcp

#关闭放行中端口:3389
firewall-cmd --permanent --remove-port=3389/tcp

#查看防火墙规则(只显示/etc/firewalld/zones/public.xml中防火墙策略,在配置策略前,我一般喜欢先CP,以后方便直接还原)
firewall-cmd --list-all 

#查看所有的防火墙策略(即显示/etc/firewalld/zones/下的所有策略)
firewall-cmd --list-all-zones 

#查看防火墙所有开放的端口
firewall-cmd --zone=public --list-ports

#重新加载配置文件,更改配置后一定要重新加载配置文件
firewall-cmd --reload

标签: linux, Centos, iptables, firewalld

添加新评论