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

  • 一. zone的概念
    • 1.1 预定义的zone
    • 1.2 将interface和source划分到某个zone
    • 1.3 zone配置文件
  • 二. Service的概念
    • 2.1 service配置文件
    • 2.2 service相关的指令
  • 三. ipset的概念
  • 四. direct.xml

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,分别为drop,block,public,external,dmz,work,home,internal,trusted。这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端口。
dmz:与external一样,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端口。
internal:与home一样,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,那么就会走默认的zone,Centos7 默认情况下,默认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,则默认为default,default的行为与REJECT类似。
short:给该zone指定一个名字。
description:对该zone的描述
service:该zone开启的服务(service),service下一小节会讲
port:该zone打开的端口,protocol属性可以指定协议,通常为tcp或udp,port属性指定端口号
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添加到一个zone,timeout可以为这个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代替。

Centos7 防火墙配置详解(非常详细!)相关推荐

  1. Iptables防火墙配置详解

    iptables防火墙配置详解 iptables简介 iptables是基于内核的防火墙,功能非常强大,iptables内置了filter,nat和mangle三张表. (1)filter表负责过滤数 ...

  2. log4j 配置详解(超详细)

    一.Log4j简介 Log4j有三个主要的组件:Loggers(记录器),Appenders (输出源)和Layouts(布局).这里可简单理解为日志类别,日志要输出的地方和日志以何种形式输出.综合使 ...

  3. log4j配置详解(非常详细转载)

    转载:http://www.360doc.com/content/17/0824/16/46744981_681796916.shtml Log4J的配置文件(Configuration File)就 ...

  4. php.ini文件配置详解,非常详细的php.ini 配置文件详解

    ​php的配置文件详解 ;;;;;;;;;;;;;;;;; ;; 关于php.ini ;; ;;;;;;;;;;;;;;;;; ; 这个文件必须命名为'php.ini'并放置在httpd.conf中P ...

  5. 锐捷RG-WALL60防火墙配置详解

    原文出自:http://xwnet.blog.51cto.com/233677/108956 今天我们要配置一款锐捷的低端防火墙,型号为RG-WALL60. 先说一下这些日子防火墙的学习心得.防火墙的 ...

  6. java图片填充父容器_java相关:spring的父子容器及配置详解

    java相关:spring的父子容器及配置详解 发布于 2020-5-26| 复制链接 本篇文章主要介绍了spring的父子容器及配置详解,详细的介绍了spring父子容器的概念.使用场景和用法,有兴 ...

  7. [精华]世界顶级防火墙LooknStop配置详解

    世界顶级防火墙LooknStop配置详解 作者:小金 一. 难以驯服的烈马:LooknStop     http://ike.126.com 网络防火墙的选择一直是众多用户最头痛的问题,放眼看看现在的 ...

  8. java JDK安装与环境配置详解(超超超级详细)

    点击以下链接获取详细图文教程! java JDK安装与环境配置详解 https://v.xiumi.us/board/v5/3QTAV/112689421

  9. 使用LVS实现负载均衡原理及安装配置详解

    使用LVS实现负载均衡原理及安装配置详解 负载均衡集群是 load balance 集群的简写,翻译成中文就是负载均衡集群.常用的负载均衡开源软件有nginx.lvs.haproxy,商业的硬件负载均 ...

最新文章

  1. blob二进制显示在html,使用Blob获取图片并二进制显示实例页面
  2. Linux查找文件 —— whereis 、 find、 locate、 which
  3. Visual C#中的(ListBox)数据绑定
  4. golang中的big.Int
  5. C++设计模式——单例模式
  6. python:linux中升级python版本
  7. java调用kettle批量执行
  8. Python中map的使用方法
  9. 量化策略回测ocobreak
  10. yum命令 启用仓库_yum 命令详解-yum仓库配置文件详解
  11. 小甲鱼python课后题简书_MOOC_Python语言程序设计(嵩天)课后练习_第二周
  12. php开发我的收藏,我的收藏列表 · 老猫带你玩转ThinkPHP5 API开发 · 看云
  13. 无盘服务器uefi启动,易乐游无盘网吧客户机使用UEFI启动教程
  14. 学习记录 | ZigBee协议栈工作流程
  15. gp数据库与pg数据库
  16. UEFI开发探索42 – Protocol的使用1
  17. phpStorm2018安装与破解(免安装打包版)
  18. 有一个棋盘,有64个方格,在第一个方格里面放1粒芝麻重量是0.00001kg,第二个里面放2粒,第三个里面放4,棋盘上放的所有芝麻的重量。
  19. oracle10显示数据库错误,Oracle10gR2数据库出现ORA-3136错误的解决方法
  20. Java基础知识——BIO模式

热门文章

  1. linux定时备份数据库到远程ftp,Linux下自动备份MySQL数据库并上传到远程FTP服务器...
  2. 二进制、八进制、十六进制与十进制间相互转换
  3. Layer弹出层插件
  4. android 模拟器 绝地求生,绝地求生全军出击电脑模拟器怎样设置?绝地求生全军出击安卓模拟器配置教程...
  5. 导论 计算机组成 ppt,计算机科学导论五章计算机组成.ppt
  6. iOS8之后苹果屏幕旋转的一些问题总结
  7. [C++ code]买卖股票
  8. 如何部署LAMP架构
  9. 电骡eMule 0.46a VeryCD Build 0607 简体中文版
  10. 雷人语录,郁闷时看了也会咧嘴的