文章目录
- firewalld 介绍
- firewall的基本概念
- firewall-cmd 配置防火墙
- 富规则配置
- 直接规则配置
firewalld 介绍
在CentOS 7里有几种防火墙共存:firewalld、iptables、ebtables。默认是使用firewalld来管理netfilter子系统,不过底层调用的命令仍然是iptables,如下图,关于iptalbes 和netfilter 概念可以参见 iptables详细教程
启用 firewalld 防火墙服务后通过 iptables -nvL 命令仍然可以看到系统中的各种配置,如下
可以看到firewalld其实就是基于iptables 建立了一套基础的规则配置框架,当我们采用firewall-cmd 进行配置时(如开放端口)就是在这个规则框架中的相应chain中补充特定的规则。除此之外对于比较复杂的iptables规则比如 limit 等 ,firewalld提供了direct rule 用来直接使用iptables 语法配置规则。但是官方文档提示慎用。
个人理解:firewalld 就是在iptables基础、语法之上,引入了服务、区域等概念,提供了 界面化配置工具firewall-config,使配置工作变的更简单。
firewall的基本概念
区域
firewalld将网卡对应到不同的区域(zone),zone 默认共有9个:block dmz drop external home internal public trusted work。不同的区域对待数据包的默认行为不同,根据区域名字我们可以很直观的知道该区域的特征,在CentOS7系统中,默认区域被设置为public。
阻塞区域(block):任何传入的网络数据包都将被阻止。
工作区域(work):相信网络上的其他计算机,不会损害你的计算机。
家庭区域(home):相信网络上的其他计算机,不会损害你的计算机。
公共区域(public):不相信网络上的任何计算机,只有选择接受传入的网络连接。
隔离区域(DMZ):隔离区域也称为非军事区域,内外网络之间增加的一层网络,起到缓冲作用。对于隔离区域,只有选择接受传入的网络连接。
信任区域(trusted):所有的网络连接都可以接受。
丢弃区域(drop):任何传入的网络连接都被拒绝。
内部区域(internal):信任网络上的其他计算机,不会损害你的计算机。只有选择接受传入的网络连接。
外部区域(external):不相信网络上的其他计算机,不会损害你的计算机。只有选择接受传入的网络连接。
配置文件
针对9个区域firewalld提供了九个配置文件:block.xml、dmz.xml、drop.xml、external.xml、 home.xml、internal.xml、public.xml、trusted.xml、work.xml。他们保存在/usr/lib/firewalld/zones/ 下。默认情况下,在/etc/firewalld/zones下面只有一个public.xml。如果给另外一个zone做一些改动,并永久保存,那么会自动生成对应的配置文件。如通过如下命令给work zone增加一个端口
firewall-cmd --permanent --zone=work --add-port=1000/tcp
此时/etc/firewalld/zones下就会生成一个work.xml的配置文件,如下:
其实firewalld服务所有的规则配置都是持久化在配置文件中的,除了上述的zone配置文件,services 和 icmptype 以及富规则、直接规则都有自己的配置文件,/usr/lib/firewalld/ 目录下是默认配置的配置文件(尽量不要修改),/etc/firewalld下是自定义配置的配置文件。firewalld服务加载配置文件中的内容到内容,并发挥过滤等作用。
服务
firewalld将常用的服务的端口等相关信息进行了封装形成服务(如http服务对应端口80),管理人员可以直接通过服务名称来配置规则。更加人性化也更加高效。
服务也都是通过配置文件定义保存的,firewalld默认支持 70多种常见的服务,我们也可以根据需要新增、修改服务。
如:我们修改ftp服务的端口号为2121
就可以复制模版/usr/lib/firewalld/services/ftp.xml到 /etc/firewalld/services/并修改,如下:
配置方式
firewalld 提供了firewall-cmd 、firewall-config、配置文件修改三种方式来修改规则配置。支持运行时配置和持久化配置,运行时配置会立即生效但是重新加载规则后失效,持久化配置必须重新加载规则后才能生效.
firewall-cmd :通过命令来修改规则,支持运行时和持久化修改,持久化修改后需要重新加载规则来使规则生效。
firewall-config:通过界面来修改规则,支持运行时和持久化修改,持久化修改后需要重新加载规则来使规则生效。
配置文件修改:修改文件就是修改持久化规则,修改后必须重新加载来生效.
以下命令为重新加载规则命令。
firewall-cmd 配置防火墙
firewalld服务操作
[root@localhost ~]# yum install firewalld firewall-config – 安装服务
[root@localhost ~]# systemctl start firewalld --启动服务
[root@localhost ~]# systemctl enable firewalld --开机启动
[root@localhost ~]# systemctl status firewalld --查看服务状态
[root@localhost ~]# systemctl stop firewalld --关闭服务
[root@localhost ~]# systemctl disable firewalld --取消开机启动
[root@localhost ~]# systemctl is-enabled firewalld --查看firewalld是否开启
弃用FirewallD防火墙,改用iptables
[root@localhost ~]# yum install iptables-services
[root@localhost ~]# systemctl start iptables
[root@localhost ~]# systemctl enable iptables
firewall-cmd基础操作
所有关于 firewall-cmd 的配置 使用–permanent 参数的就是持久化修改,需要重新加载规则才能生效,否则就是运行时修改,重新加载规则后失效。
[root@localhost ~]# firewall-cmd --version – 查看版本
[root@localhost ~]# firewall-cmd --state --查看服务状态
[root@localhost ~]# firewall-cmd --help --帮助信息
应急操作
[root@localhost ~]# firewall-cmd --panic-on – 拒绝所有包 紧急使用
[root@localhost ~]# firewall-cmd --panic-off --取消拒绝所有包 紧急使用
[root@localhost ~]# firewall-cmd --query-panic – 查看是否处于紧急拒绝状态
重新加载规则
[root@localhost ~]# firewall-cmd --reload --重启加载防火墙(以 root 身份输入以下命令,重新加载防火墙,并不中断用户连接,即不丢失状态信息)
[root@localhost ~]# firewall-cmd --complete-reload --完全重启防火墙 (以 root 身份输入以下命令,重新加载防火墙并中断用户连接,即丢弃状态信息)
区域操作
firewall-cmd进行的所有规则操作,如果未使用了–zone=YY 参数指定操作区域,那么都是对默认区域的操作。
[root@localhost ~]# firewall-cmd --get-active-zones --查看活动的区域
[root@localhost ~]# firewall-cmd --get-zone-of-interface=XX --根据网口查看区域
[root@localhost ~]# firewall-cmd --get-default-zone --显示默认区域
[root@localhost ~]# firewall-cmd --zone=XX --add-interface=eth0 --添加接口到区域
[root@localhost ~]# firewall-cmd --set-default-zone=XX --设置默认区域,立即生效无需重启
[root@localhost ~]# firewall-cmd --get-zones – 显示支持的区域列表
[root@localhost ~]# firewall-cmd --list-all-zones --列出全部区域启用的特性
[root@localhost ~]# firewall-cmd --zone=XX --list-all --显示XX区域 开放的端口和服务
[root@localhost ~]# firewall-cmd --get-active-zones – 查看当前活跃区域
绑定网络接口
[root@localhost ~]# firewall-cmd --get-zone-of-interface=XX --显示XX接口所属区域
[root@localhost ~]# firewall-cmd --zone=YY --query-interface=XX --查询YY区域中是否包含XX接口
[root@localhost ~]# firewall-cmd --zone=YY --remove-interface=XX --删除指定XX网卡所在的zone(以YY为例)
[root@localhost ~]# firewall-cmd --zone=YY --change-interface=XX – 临时修改XX接口为YY区域(永久修改加参数–permanent)
开放端口
[root@localhost ~]# firewall-cmd --zone=XX --list-ports --查看XX区域打开的端口
[root@localhost ~]# firewall-cmd --zone=XX --add-port=8080/tcp --临时加一个端口到XX区域
[root@localhost ~]#firewall-cmd --permanent --add-port=443/tcp 永久开放443端口
[root@localhost ~]#firewall-cmd --add-port=443/tcp 临时开放443端口
指定端口的时候一定要指定是什么协议,tcp 还是 udp,通过指定端口号开放的就要通过指定端口号关闭
[root@localhost ~]#firewall-cmd --list-ports 查看开放的所有端口
开放服务
开放服务也使开放端口的一种方式,前文可知服务中会定义需要的端口,开放服务就会开放服务中定义的端口。通过指定服务名开放的就要通过指定服务名关闭。使用的服务名成必须已经在 service 目录下的配置文件中定义好
[root@localhost ~]# firewall-cmd --zone=XX --list-services --查看XX区域加载的服务
[root@localhost ~]# firewall-cmd --zone=work --add-service=smtp --打开一个服务,类似于将端口可视化,服务需要在配置文件中添加,/etc/firewalld 目录下有services文件夹
[root@localhost ~]# firewall-cmd --zone=work --remove-service=smtp – 移除服务
[root@localhost ~]# firewall-cmd --add-service=mysql --开发mysql服务
[root@localhost ~]# firewall-cmd --remove-service=mysql–删除musql服务
[root@localhost ~]# firewall-cmd --list-services 查看开放的所有服务
富规则配置
Firewalld中的富规则表示更细致、更详细的防火墙策略配置,它可以针对系统服务、端口号、源地址和目标地址等诸多信息进行更有针对性的策略配置, 优先级在所有的防火墙策略中也是最高的。
firewall-cmd --list-rich-rules 查看富规则
相关命令
–add-rich-rule=‘’ #在指定的区添加一条富规则
–remove-rich-rule=‘’ #在指定的区删除一条富规则
–query-rich-rule=‘’ #找到规则返回0 ,找不到返回1
–list-rich-rules #列出指定区里的所有富规则
示例:firewall-cmd --permanent --add-rich-rule=“rule family=“ipv4” source address=“192.168.142.166” port protocol=“tcp” port=“5432” accept”
根据上述示例我们来看语法
通过查看 man firewalld.richlanguage 富规则手册,发现没有对 iptables 中 --flags 的支持,对于这种 还是要使用direct 规则进行配置
示例配置
firewall-cmd --add-rich-rule=‘rule family=“ipv4” source address=“192.168.122.0” accept’ --接收来自 192.168.122.0的请求
firewall-cmd --add-rich-rule=‘rule service name=ftp limit value=2/m accept’ --每分钟允许2个新连接访问ftp服务。
irewall-cmd --add-rich-rule=‘rule service name=ftp log limit value=“1/m” audit accept’ --同意新的 IP v4 和 IP v6 连接 FT P ,并使用审核每分钟登录一次
firewall-cmd --add-rich-rule=‘rule family=“ipv4” source address=“192.168.122.0/24” service name=ssh log prefix=“ssh” level=“notice” limit value=“3/m” accept’ --允许来自192.168.122.0/24地址的新 IPv4连接连接TFTP服务,并且每分钟记录一次
firewall-cmd --permanent --add-rich-rule=‘rule protocol value=icmp drop’ --丢弃所有icmp包
firewall-cmd --add-rich-rule=‘rule family=ipv4 source address=192.168.122.0/24 reject’ --timeout=10 – 当使用source和destination指定地址时,必须有family参数指定ipv4或ipv6。如果指定超时,规则将在指定的秒数内被激活,并在之后被自动移除。
firewall-cmd --add-rich-rule=‘rule family=ipv6 source address=“2001:db8::/64” service name=“dns” audit limit value=“1/h” reject’ --timeout=300-- 拒绝所有来自2001:db8::/64子网的主机访问dns服务,并且每小时只审核记录1次日志
firewall-cmd --permanent --add-rich-rule=‘rule family=ipv4 source address=192.168.122.0/24 service name=ftp accept’ --允许192.168.122.0/24网段中的主机访问ftp服务
firewall-cmd --add-rich-rule=‘rule family=“ipv6” source address=“1:2:3:4:6::” forward-port to-addr=“1::2:3:4:7” to-port=“4012” protocol=“tcp” port=“4011”’ – 转发来自ipv6地址1:2:3:4:6::TCP端口4011,到1:2:3:4:7的TCP端口4012
firewall-cmd --zone=public --add-rich-rule ‘rule family=“ipv4” source address=192.168.0.14 accept’ --允许来自主机 192.168.0.14 的所有 IPv4 流量
firewall-cmd --zone=public --add-rich-rule ‘rule family=“ipv4” source address=“192.168.1.10” port port=22 protocol=tcp reject’–拒绝来自主机 192.168.1.10 到 22 端口的 IPv4 的 TCP 流量。
直接规则配置
对于最高级的使用,或 iptables 专家,Firewalld 提供了一个direct接口,允许你给它传递原始 iptables 命令。持久化的添加direct规则会被保存在 一个叫direct.xml 的文件中。重新加载规则后会生效。direct规则完全支持 iptables/ip6tables/ebtable 的语法。
如下示例添加一条ipv4规则:
firewall-cmd --direct --get-all-rules 查看所有direct 规则
firewall-cmd --direct (direct规则) --permanent(持久化) --add-rule (添加规则) ipv4(ipv4族) filter(表) INPUT(链) 4(优先级 必填) -p tcp --dport 22 --tcp-flags SYN,ACK,FIN,RST SYN -m limit --limit 10/s
firewall-cmd --direct --add-chain { ipv4 | ipv6 | eb } 为表增加一个新链
firewall-cmd --direct --remove-chain { ipv4 | ipv6 | eb } 从表中删除链 。
firewall-cmd --direct --query-chain { ipv4 | ipv6 | eb } 查询链是否存在与表如果是,返回0,否则返回1.
firewall-cmd --direct --get-chains { ipv4 | ipv6 | eb } 获取所有链名称 逗号分割
firewall-cmd --direct --add-rule { ipv4 | ipv6 | eb } 添加规则
firewall-cmd --direct --remove-rule { ipv4 | ipv6 | eb } 删除规则
firewall-cmd --direct --query-rule { ipv4 | ipv6 | eb } 查询规则
firewall-cmd --direct --get-rules { ipv4 | ipv6 | eb } 以iptables的命令允许端口号
添加直接规则时必须要有优先级,否则无法保证规则顺序。优先级越小 位置越往前。
也可以通过 man firewalld.direct
命令查看 手册
示例配置