防火墙做为保护服务器不受外部网络流量影响的一种方式。可让用户定义一系列规则来控制外部网络中流入的流量,从而达到容许或阻塞的效果。firewalld
是防火墙服务的一个守护程序,实现了动态修改拥有 D-Bus
接口基于主机的防火墙,也就是说在不须要重启防火墙程序的基础上,就能够实现对防火墙规则,增、删、改的操做。html
firewalld
提供了域(Zone)和服务(Services)的概念,来简化流量的管理操做。在本篇文章中,会依次介绍这些概念,并记录一些常见防火墙的操做方法。python
在阅读后,应该掌握以下的概念:linux
其实 Zone 就是为了方便预先定义好的一组规则,让用户根据当前服务器所在网络中的位置(内网,公网)的受信任程度来选取不一样 Zone 默认规则。但须要注意的是,一个网络链接只能被一个 zone 处理,但一个 zone 能够用于多个网络链接。shell
预先定义的 zone 规则被放在 /usr/lib/firewalld/zones/
目录下。当修改 zone 的规则时,这些 zone 会被拷贝到 /etc/firewalld/zones/
目录下,实际生效的防火墙会在该目录下的文件。centos
对于每个 zone 都有一个默认的行为(target),来处理流入的流量。每一个 target 会有四个选项:default
, ACCEPT
, REJECT
和 DROP
.服务器
ACCEPT
除了被明确写好的规则,会接受全部流入的数据包。网络
REJECT
除了被明确写好容许的规则,会拒绝全部流入的数据包, 会给发起链接的机器回复被拒绝的消息。ssh
DROP
除了被明确写好容许的规则,会拒绝全部流入的数据包, 不会给发起链接的机器回复任何消息。tcp
下面是对常见 Zone 的一些描述:ide
Zone 名称 | Zone 描述 |
---|---|
block | 全部进入的网络链接都会被拒绝。对于 IPV4,回复 icmp-host-prohibited 消息。对于 IPV6,回复 icmp6-adm-prohibited 消息。只有由内部发起的网络链接能够通行。 |
dmz | 对于在非军事区域的服务器,外部网络能够在受限制的状况下进入内网,只有特定的网络链接请求被接受。 |
drop | 全部的进入的网络包都会被丢掉,而且没有任何的回应。只有向发起的链接请求能够被放行。 |
external | 用于开始假装的外部网络,特别是做为路由器。任务外部的网络会损坏你的计算机,只有特定的网络链接请求被接受。 |
home | 在家使用,信任网络上的大多数计算机。只有特定的网络链接请求被接受。 |
internal | 在内部网络使用,信任当前网络下其余的计算机。只有特定的网络链接请求被接受。 |
public | 在公共网络使用,不信任网络上的其余计算机。只有特定的网络链接请求被接受。 |
trusted | 全部的网络链接都会被接受。 |
work | 在工做网络中使用,信任网络上的其余计算机。只有特定的网络链接请求被接受。 |
简单看一下配置的具体内容,把上面 target 和 zone 的概念联在一块儿:
target 表示当发来的数据包,没有匹配到任何的规则(好比端口号,服务等),采起的默认行为。
short 表示对 zone 的一个简短描述名字。
description 表示对 zone 的一段详细描述。
# 获取默认域 (base) [root@localhost zones]# firewall-cmd --get-default-zone public # 查询存在的全部域 (base) [root@localhost zones]# firewall-cmd --get-zones block dmz drop external home internal public trusted work # 设置默认域 (base) [root@localhost zones]# firewall-cmd --set-default-zone=public # 查询某个域下的配置 (base) [root@localhost zones]# firewall-cmd --zone=public --list-all # 查询指定网卡的域 (base) [root@localhost zones]# firewall-cmd --get-zone-of-interface=ens192 public # 为某个网卡指定域 (base) [root@localhost zones]# firewall-cmd --zone=public --add-interface=ens33 # 修改网卡的域 (base) [root@localhost zones]# firewall-cmd --zone=block --change-interface=ens37 # 删除指定网卡的域 (base) [root@localhost zones]# firewall-cmd --zone=block --remove-interface=ens37 # 查询系统中正在使用的域 firewall-cmd --get-active-zones
Services 在网络通讯时,须要使用一个或者多个端口,地址。防火墙会基于端口来对通讯的内容进行过滤。若是一个服务想要容许网络的流量进入,必须打开端口。firewalld
会默认的阻断全部未被显式打开的端口。换成本身的理解来讲,在服务器提供的一些服务,经常须要打开不少端口,而且会限制一些源和目的 IP 甚至一些协议的访问,若是把这些同时都定义在 zone 中,会形成最后的 zone 配置文件很大,提取成 services 至关因而解耦的概念,有利于后期的维护。
和 zone 相似,在 /usr/lib/firewalld/services/
目录中存放着一些默认的 services 配置,在 services 被修改或者添加时,这里的文件会被做为参考。目前正被使用的 services 配置会在 /etc/firewalld/services/
目录。
# 查看正在使用的服务: (base) [root@localhost firewalld]# firewall-cmd --list-services ssh dhcpv6-client http https # 查看服务的被使用状况,能够看到 zone 文件中引入了 services: (base) [root@localhost firewalld]# cat zones/public.xml <?xml version="1.0" encoding="utf-8"?> <zone> <short>Public</short> <description>For use in public areas. You do not trust the other computers on networks to not harm your computer. Only selected incoming connections are accepted.</description> <service name="ssh"/> <service name="dhcpv6-client"/> <service name="http"/> <service name="https"/> <port protocol="tcp" port="8080"/> <port protocol="tcp" port="3306"/> </zone> # 使用 service: firewall-cmd --add-service=http --permanent # 查看预先定义的 services: firewall-cmd --get-services # 增长一个空 services: firewall-cmd --permanent --new-service=service-name.xml # 增长和某个 services 相同的配置: firewall-cmd --permanent --new-service-from-file=/etc/firewalld/services/service-name.xml --name service-name1.xml # 修改 services 配置: firewall-cmd --permanent --service=myservice --set-description=description firewall-cmd --permanent --service=myservice --set-short=description firewall-cmd --permanent --service=myservice --add-port=portid[-portid]/protocol firewall-cmd --permanent --service=myservice --add-protocol=protocol firewall-cmd --permanent --service=myservice --add-source-port=portid[-portid]/protocol firewall-cmd --permanent --service=myservice --add-module=module firewall-cmd --permanent --service=myservice --set-destination=ipv:address[/mask]
打开端口的方式,除了使用上面的 services ,也可使用 port 直接添加:
# 端口操做,直接在 zone 的级别进行更改 # 查看并无 http 端口 sudo firewall-cmd --list-ports # 增长 80 端口 sudo firewall-cmd --add-port=80/tcp # 永久增长 80 端口 firewall-cmd --add-port=80/tcp --permanent # 删除端口 firewall-cmd --zone=public --remove-port=8080/tcp # 将修改后的某些配置持久化保存: firewall-cmd --runtime-to-permanent # 其余查询端口的一些方式 netstat -ap | grep 8080 lsof -i:6800 # centos 7 ss -ant # centos 7 before netstat -ant
防火墙的运行相关操做:
# 查询防火墙状态 systemctl status firewalld # 打开防火墙 systemctl start firewalld # 在修改配置后,重载防火墙 firewall-cmd --reload # 添加直接规则,例如 Keepalived。这里须要注意的是,当使用 zone 和 services 都没法知足要求时,再采用直接添加的方案。 firewall-cmd --direct --permanent --add-rule ipv4 filter OUTPUT 0 --out-interface ens192 --destination 224.0.0.18 --protocol vrrp -j ACCEPT firewall-cmd --direct --permanent --add-rule ipv4 filter INPUT 0 --in-interface ens192 --destination 224.0.0.18 --protocol vrrp -j ACCEPT # 查询配置的规则 firewall-cmd --direct --get-all-chains firewall-cmd --direct --get-all-rules
遇到紧急状况的操做:
# 在遇到服务器正在被攻击时,切断全部网络链接 firewall-cmd --panic-on # 恢复至正常的防火墙设置 firewall-cmd --panic-off # 查询 panic mode 是否开启 firewall-cmd --query-panic