启动防火墙# systemctl start firewalld
关闭防火墙# systemctl stop firewalld
重启防火墙# systemctl restart firewalld
查看防火墙状态# systemctl status firewalld
设置防火墙开机启动# systemctl enable firewalld
关闭防火墙开机启动# systemctl disable firewalld
常用命令(加--permanent参数才会写入配置文件以永久生效):
重新加载防火墙配置# firewall-cmd --reload 规则更改后必须重新加载生效
查看防火墙运行状态# firewall-cmd --state 等于#systemctl status firewalld
查看所有区域配置#firewall-cmd --list-all-zones
查看防火墙默认区域配置# firewall-cmd --list-all
查看防火墙默认区域名#firewall-cmd --get-default-zone
设置防火强默认区域#firewall-cmd --set-default-zone=public
把网卡分配给区域#firewall-cmd --zone=public --add-interface=ens32
查看指定网卡所在区域#firewall-cmd --get-zone-of-interface=ens32
更改网卡所属区域#firewall-cmd --zone=public --change-interface=ens32
应急模式管理(加--permanent参数才会写入配置文件以永久生效):
拒绝所有流量# firewall-cmd --panic-on
取消应急模式# firewall-cmd --panic-off(但需要重启firewalld后才可以远程ssh)
查看是否为应急模式# firewall-cmd --query-panic
服务添加示例(加--permanent参数才会写入配置文件以永久生效):
添加服务#firewall-cmd --add-service=Telnet
移除服务#firewall-cmd --remove-service=Telnet
查看开启的服务# firewall-cmd --list-services
端口添加示例(加--permanent参数才会写入配置文件以永久生效):
添加端口和协议# firewall-cmd --add-port=3306/tcp
删除端口和协议# firewall-cmd --remove-port=3306/tcp
查看开启的端口# firewall-cmd --list-ports
协议添加示例(加--permanent参数才会写入配置文件以永久生效):
添加协议# firewall-cmd --add-protocol=icmp
取消协议# firewall-cmd --remove-protocol=icmp
查看添加的协议# firewall-cmd --list-protocols
端口流量转发示例(加--permanent参数才会写入配置文件以永久生效):
将原本访问本机888端口的流量转发到本机22端口:
firewall-cmd --add-forward-port=port=888:proto=tcp:toport=22
将原本访问本机888端口的流量转发到ip为192.168.2.208的主机的22端口(需要开启masquerade):
firewall-cmd --add-masquerade
firewall-cmd --add-forward-port=port=888:proto=tcp:toport=22:toaddr=192.168.2.208
测试端口转发功能是否生效:在客户端尝试访问192.168.2.210主机的888端口,连上去后发现实际连接的是192.168.2.208主机,测试OK。
富规则临时添加示例(加--permanent参数才会写入配置文件以永久生效):
允许192.168.2.208主机的所有流量:
firewall-cmd --add-rich-rule="rule family="ipv4" source address="192.168.2.208" accept"
允许192.168.2.208主机的icmp协议,即允许192.168.2.208主机ping:
firewall-cmd --add-rich-rule="rule family="ipv4" source address="192.168.2.208" protocol value="icmp" accept"
取消允许192.168.2.208主机的所有流量
firewall-cmd --remove-rich-rule="rule family="ipv4" source address="192.168.2.208" accept"
允许192.168.2.208主机访问ssh服务
firewall-cmd --add-rich-rule="rule family="ipv4" source address="192.168.2.208" service name="ssh" accept"
禁止192.168.2.208访问https服务,并返回错误信息:
firewall-cmd --add-rich-rule="rule family="ipv4" source address="192.168.2.208" service name="https" reject"
注:如果是drop的话是直接丢弃,会返回timeout(连接超时)
允许192.168.2.0/24网段的主机访问22端口:
firewall-cmd --add-rich-rule="rule family="ipv4" source address="192.168.2.0/24" port protocol="tcp" port="22" accept"
允许192.168.2.1地址的主机访问22端口:
firewall-cmd --add-rich-rule="rule family="ipv4" source address="192.168.2.1" port protocol="tcp" port="22" accept"
允许任意地址的主机访问5601端口:
firewall-cmd --add-rich-rule="rule family="ipv4" port protocol="tcp" port="5601" accept"
每分钟允许2个新连接访问ftp服务:
firewall-cmd --add-rich-rule="rule service name=ftp limit value=2/m accept"
允许新的ipv4和ipv6连接ftp,并使用日志和审核,每分钟允许访问一次:
firewall-cmd --add-rich-rule="rule service name=ftp log limit value="1/m" audit accept"
拒绝来自192.168.2.0/24网段的连接,10秒后自动取消:
firewall-cmd --add-rich-rule="rule family=ipv4 source address=192.168.2.0/24 reject" --timeout=10
允许ipv6地址为2001:db8::/64子网的主机访问dns服务,并且每小时审核一次,300秒后自动取消:
firewall-cmd --add-rich-rule="rule family=ipv6 source address="2001:db8::/64" service name="dns" audit limit value="1/h" reject" --timeout=300
将来自192.168.2.0/24网段访问本机80端口的流量转发到本机的22端口:
firewall-cmd --add-rich-rule="rule family=ipv4 source address=192.168.2.0/24 forward-port port=80 protocol=tcp to-port=22"
将来自192.168.2.0/24网段访问本地80端口的流量转发到192.168.2.208主机的22端口:
firewall-cmd --add-rich-rule="rule family=ipv4 source address=192.168.2.0/24 forward-port port=80 protocol=tcp to-port=22 to-addr=192.168.2.208"
防火墙脚本示例:
#!/bin/bash
systemctl stop firewalld
\cp -p /usr/lib/firewalld/zones/drop.xml /etc/firewalld/zones/
systemctl start firewalld
firewall-cmd --set-default-zone=drop
firewall-cmd --permanent --change-interface=ens32
firewall-cmd --permanent --add-service=https
firewall-cmd --permanent --add-protocol=icmp
firewall-cmd --permanent --add-masquerade
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.2.208" port protocol="tcp" port="5210" accept"
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.2.206" port protocol="tcp" port="5210" accept"
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="116.226.230.115" port protocol="tcp" port="8023" accept"
firewall-cmd --reload
附加:
rich rules----
富规则,即更细致、更详细的防火墙规则策略,它的优先级在所有的防火墙策略中也是最高的,这个要注意。
以上端口、服务、协议、端口流量转发配置都是临时添加,重启就丢失;建议在/etc/firewalld/zones/配置文件里配置规则,以永久生效。