Centos7 防火墙配置详解(非常详细!)

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

Centos7 防火墙配置详解非常详细


Centos7中使用firewalld来作为防火墙其底层调用的命令仍然是iptables等命令但是在配置上发生了较大的变化。

Centos7中有两个位置存放了firewall的配置文件一个是/etc/firewalld一个是/usr/lib/firewalld前者是用户配置目录后者是系统配置目录。/usr/lib/firewalld目录中存放的是firewalld提供的一些默认和备份的配置文件一般不随意改变/etc/firewalld目录下才是用户配置的真正生效的配置文件只有在/etc/firewalld目录不存在或该目录下不存在配置文件的情况下/usr/lib/firewalld目录下的配置文件才会生效。

一. zone的概念

zone定义了防火墙对某个连接、网口interface或源地址的信任等级我们可以把他理解为防火墙对不同的连接connection、网口interface或源地址source address划分到不同的zone当中而不同的zone定义了不同的规则因此防火墙可以针对不同的连接、网口interface或源地址做出不同的行为。例如我们将10.12.18.201这个地址划分到zone1中将10.12.18.202这个地址划分到zone2中然后zone1中定义的规则为允许访问3306端口其余的端口都拒绝访问zone2中定义的规则为拒绝访问3306端口其余的端口都允许访问。那么10.12.18.201就仅能访问本机的3306端口10.12.18.202就仅不能访问本机的3306端口。每个zone的防火墙规则是通过/etc/firewalld/zones目录下的xml配置文件来配置的。
zone和connection、interface、source address之间是一对多的关系即一个connection、interface或source address仅能划分到一个zone中而一个zone中可以包含多个connection、interface或source address。

1.1 预定义的zone

Centos7中firewalld为用户预定义了9个zone分别为dropblockpublicexternaldmzworkhomeinternaltrusted。这9个zone的配置文件在/usr/lib/firewalld/zones目录下通过查看他们的配置文件可以得知这9个zone的规则是怎么样的。
drop任何传入本机的网络数据包都会被丢弃并且不会回复只允许本机对外访问其他服务器。
block任何传入本机的网络连接请求都会被拒绝并且会回复一条拒绝访问的消息。
public用于本机处于公共网络环境的场景下仅接受特定的连接请求如仅接受某些特定的IP的连接请求或仅对外部开放特定的某些端口。Centos 7 默认的public.xml文件中仅开放用于ssh连接请求的22端口和dhcpv6-client服务的546端口。
external与public类似Centos 7 默认仅开放用于ssh连接请求的22端口。
dmzexternal一样Centos 7 默认也是仅开放用于ssh连接请求的22端口。
work用于工作网络环境场景下信任大部分的其他的计算机不会对本机进行攻击。Centos 7 默认开放用于ssh连接请求的22端口dhcpv6-client服务的546端口以及IPP协议的631端口。
home用于家庭网络环境信任网络上的其他计算机不会攻击本机。Centos 7默认开放用于ssh连接请求的22端口dhcpv6-client服务的546端口IPP协议的631端口samba服务的137、138端口mDNS服务的5353端口。
internalhome一样Centos 7默认开放用于ssh连接请求的22端口dhcpv6-client服务的546端口IPP协议的631端口samba服务的137、138端口mDNS服务的5353端口。
trusted所有对本机的网络连接请求都会被接受。

1.2 将interface和source划分到某个zone

将某个source划分到某个zone的命令如下

firewall-cmd [--permanent] [--zone=zone] --add-source=source

例如

firewall-cmd --permanent --zone=trusted --add-source=192.168.5.112

这条命令会将192.168.5.112这个网口划分到trusted这个zone–permanent参数会将该配置写入trusted这个zone的配置文件trusted.xml中使其永久生效如果不加–permanent则只会临时生效重启防火墙或者调用firewall-cmd --reload重新加载配置文件会使得该项配置失效。

将某个网口interface划分到某个zone的命令如下

 firewall-cmd [--permanent] [--zone=zone] --add-interface=interface

例如

 firewall-cmd --permanent --zone=block --add-interface=ens33

这条命令会将ens33这个网口划分到block这个zone这样其他机器访问本机的ens33网口时就会默认走这个zone。
如果某个连接请求没有划分到任何一个zone那么就会走默认的zoneCentos7 默认情况下默认zone为public。可以通过以下命令查看或者修改默认zone。

获取默认的zone

firewall-cmd --get-default-zone

修改默认的zone

firewall-cmd --permanent --set-default-zone=[zone]

其他的一些相关的命令

列出该zone下绑定了哪些interface
firewall-cmd [--zone=zone] --list-interfaces

将该interface绑定到另一个zone上
firewall-cmd [--permanent] [--zone=zone] --change-interface=interface

如果该interface之前绑定到了某个zone则取消绑定这样就会走默认zone
firewall-cmd [--permanent] --remove-interface=interface

与source相关的
firewall-cmd [--permanent] --remove-source=source
firewall-cmd [--permanent] [--zone=zone] --list-sources
firewall-cmd [--permanent] [--zone=zone] --add-source=source

1.3 zone配置文件

一个zone的xml配置文件的示例如下

<?xml version="1.0" encoding="utf-8"?>
<zone target="DEFAULT">
  <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"/>
  <port protocol="tcp" port="80"/>
  <port protocol="tcp" port="8080"/>
  <source address="192.168.5.112">
  <source address="10.12.18.0/24"/>
  <rule family="ipv4">
    <source address="10.12.18.0/24"/>
    <port protocol="tcp" port="7180-7190"/>
    <accept/>
  </rule>
</zone>

每个标签的含义
zone给一个zone指定target

target=“ACCEPT|%%REJECT%%|DROP”

可用于接受ACCEPT、拒绝%%REJECT%%或丢弃DROP与任何规则端口、服务等都不匹配的每个数据包即指定该zone的默认的对连接connection、网口interface或源地址source address的行为如果不指定target则默认为defaultdefault的行为与REJECT类似。
short给该zone指定一个名字。
description对该zone的描述
service该zone开启的服务serviceservice下一小节会讲
port该zone打开的端口protocol属性可以指定协议通常为tcp或udpport属性指定端口号
source绑定到该zone的source其效果等同于 firewall-cmd [–permanent] [–zone=zone] --add-source=source 指令
rule为该zone添加的富语言规则rich language rule。rich language rule的写法可以参照官网https://firewalld.org/documentation/man-pages/firewalld.richlanguage
上述示例的富语言规则的含义为对10.12.18.X网段的source开放7180至7190所有端口的tcp连接请求。

二. Service的概念

service是预定义的一系列的本机的协议、端口、目标ip等service可以让我们更加方便的让防火墙限制外部对本机的某些端口的访问。service的配置文件在/etc/firewalld/services或/usr/lib/firewalld/services文件夹下每个service的配置都是一个xml文件。

2.1 service配置文件

系统在/usr/lib/firewalld/services文件夹下为我们预定义了一些列的service配置文件我们也可以在/etc/firewalld/services定义自己的service。例如ftp.xml的内容如下

<?xml version="1.0" encoding="utf-8"?>
<service>
  <short>FTP</short>
  <description>FTP is a protocol used for remote file transfer. If you plan to make your FTP server publicly available, enable this option. You need the vsftpd package installed for this option to be useful.</description>
  <port protocol="tcp" port="21"/>
  <module name="nf_conntrack_ftp"/>
</service>

这样在某个zone的配置文件中可以引用这个service例如在public.xml中引入ftp service

<?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="dhcpv6-client"/>
  <service name="ssh"/>
  <service name="ftp"/>
</zone>

等价于

<?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="dhcpv6-client"/>
  <service name="ssh"/>
  <port protocol="tcp" port="21"/>
</zone>

2.2 service相关的指令

打印所有预定义的service/usr/lib/firewalld/services或/etc/firewalld/services下每个xml配置文件就是一个预定义的service

firewall-cmd [--permanent] --get-services

列出此zone开启的服务列表

firewall-cmd [--permanent] [--zone=zone] [--permanent] [--policy=policy] --list-services

将一个service添加到一个zonetimeout可以为这个zone设置这个service的生效时间过了这个生效时间此service将从该zone中被移除。–timeout参数和–permanent参数是不兼容的

firewall-cmd [--permanent] [--zone=zone] [--permanent] [--policy=policy] --add-service=service [--timeout=timeval]

三. ipset的概念

ipset顾名思义就是可用于将多个IP或MAC地址分组在一起。通过使用ipset可以将不同的ip地址进行分组简化ip地址的管理和zone的配置。ipset的配置文件在/etc/firewalld/ipsets目录下该目录下一个xml配置文件对应一个ipset。
例如将以下ip地址组合为一个ipset配置文件命名为ipset1.xml

<?xml version="1.0" encoding="utf-8"?>
<ipset type="hash:net">
  <entry>10.12.18.201</entry>
  <entry>192.168.5.201</entry>
</ipset>

将以下mac地址组合为一个ipset配置文件命名为ipset2.xml

<?xml version="1.0" encoding="utf-8"?>
<ipset type="hash:mac">
  <entry>00:11:22:33:44:55</entry>
  <entry>11:22:33:44:55:66</entry>
</ipset>

在public.xml中引用这两个ipset

<?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>
  <source ipset="ipset1" />
  <source ipset="ipset2" />
  <service name="dhcpv6-client"/>
  <service name="ssh"/>
  <rule family="ipv4">
    <source ipset="ipset1" />
    <port port="3306" protocol="tcp" />
    <accept />
  </rule>
  <rule family="ipv4">
    <source ipset="ipset2" />
    <port port="8080" protocol="tcp" />
    <accept />
  </rule>
</zone>

这等同于

<?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>
  <source address="10.12.18.201" />
  <source address="192.168.5.201" />
  <source mac="00:11:22:33:44:55" />
  <source mac="11:22:33:44:55:66" />
  <service name="dhcpv6-client"/>
  <service name="ssh"/>
  <rule family="ipv4">
    <source address="10.12.18.201" />
    <port port="3306" protocol="tcp" />
    <accept />
  </rule>
  <rule family="ipv4">
    <source address="192.168.5.201" />
    <port port="3306" protocol="tcp" />
    <accept />
  </rule>
  <rule family="ipv4">
    <source mac="00:11:22:33:44:55" />
    <port port="8080" protocol="tcp" />
    <accept />
  </rule>
  <rule family="ipv4">
    <source mac="11:22:33:44:55:66" />
    <port port="8080" protocol="tcp" />
    <accept />
  </rule>
</zone>

显然采用ipset可以极大简化配置。

四. direct.xml

除了使用zone来配置防火墙以外还可以直接为防火墙添加iptables规则这些规则会放在配置文件/etc/firewalld/direct.xml中。默认情况下Centos 7没有这个文件只有当用户为direct interface添加iptables规则时才会生成这个文件。通过该方法添加的防火墙规则具有最高优先度也就是说如果direct.xml中的某条规则与zone的规则冲突时以direct.xml为准。但firewalld官网并不推荐我们使用direct.xml。

给direct interface添加一条防火墙规则的指令如下

firewall-cmd --permanent --direct --add-rule ipv4 filter INPUT 1 -s 10.48.186.6 -j ACCEPT

使用direct.xml需要用户本身对iptables的概念有一定基础(tables, chains, commands, parameters, targets)具体可以参考https://firewalld.org/documentation/man-pages/firewalld.direct.html
direct interface后续可能会被弃用被policies代替。

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6
标签: centos