firewalld详解

https://blog.csdn.net/gg_18826075157/article/details/72834694

从CentOS7(RHEL7)开始,官方的标准防火墙设置软件从iptables变更为firewalld,相信不少习惯使用iptables的人会感到十分不习惯,但实际上firewalld更为简单易用。 
大致用法就是:把可信任的IP地址添加到“trusted”区域,把不可信任的IP地址添加到“block”区域,把要公开的网络服务添加到“public”区域。

配置方式

firewalld的配置文件一般存放在下面两个目录:

  1. /etc/firewalld/
  2. /usr/lib/firewalld/

当需要一个文件时firewalld会优先使用第一个目录的。

  
这两个配置目录的结构很简单,主要是两个文件和三个目录:

  1. 文件: 
    • firewalld.conf:主配置文件(只有5个配置项) 
      • ①DefaultZone:默认使用的zone;
      • ②MinimalMark:使用标记的最小值;
      • ③CleanuupOnExit:退出后是否清除防火墙规则;
      • ④Lockdown:是否限制别的程序通过D-BUS接口直接操作firewalld;
      • ⑤IPv6_rpfilter:判断所接受到的包是否是伪造的
    • lockdown-whitelist.xml:当Lockdown设置为yes时,规定哪些程序可以对firewalld进行操作
    • direct.xml:直接使用类似iptables的语法来进行规则的增删
  2. 目录: 
    • zones:保存zone配置文件
    • services:保存service配置文件
    • icmptypes:保存和icmp类型相关的配置文件

什么是区域zone

所谓的区域就是一个信赖等级,某一等级下对应有一套规则集。划分方法包括:网络接口、IP地址、端口号等等。一般情况下,会有如下的这些默认区域:

  1. drop:丢弃所有进入的数据包
  2. block:拒绝所有进入的数据包
  3. public:只接受部分选定的数据包
  4. external:应用在NAT设定时的对外网络
  5. dmz:非军事区
  6. work:使用在公司环境
  7. home:使用在家庭环境
  8. internal:应用在NAT设定时的对内网络
  9. trusted:接受所有的数据包

比如,public区域由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"/>
</zone>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

当然,你也可以定义自己的区域,只要在zones目录下新建一个同名的xml文件即可。

什么是服务service

iptables时代习惯使用端口号来匹配规则,但是如果某一个服务的端口号改变了,那就要同时更改iptables的规则,十分不方便,同时也不方便阅读理解。

service配置文件的命名为<服务名>.xml,比如ssh的配置文件是ssh.xml。


<?xml version="1.0" encoding="utf-8"?>
<service>
  <short>SSH</short>
  <description>Secure Shell (SSH) is a protocol for logging into and executing commands on remote machines. It provides secure encrypted communications. If you plan on accessing your machine remotely via SSH over a firewalled interface, enable this option. You need the openssh-server package installed for this option to be useful.</description>
  <port protocol="tcp" port="22"/>
</service>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

配置实例

1.开启firewalld

# systemctl enable firewalld
# systemctl start firewalld
  • 1
  • 2

2.往trusted区域中注册IP地址

# firewall-cmd --zone=trusted --add-source=xxx.xxx.xxx.xxx
  • 1

3.往public区域中注册网络接口

# firewall-cmd --zone=public --add-interface=eth0
  • 1

4.往public区域中注册新服务

# firewall-cmd --zone=public --add-service=http
# firewall-cmd --zone=public --add-service=https
  • 1
  • 2

5.删除public区域中不需要的服务

# firewall-cmd --zone=public --remove-service=dhcpv6-client
# firewall-cmd --zone=public --remove-service=ssh
  • 1
  • 2

6.查看各个区域的配置情况

# firewall-cmd --get-active-zones
public
  interfaces: eth0
trusted
  sources: xxx.xxx.xxx.xxx
  • 1
  • 2
  • 3
  • 4
  • 5

7.查看某个区域的服务

# firewall-cmd --zone=public --list-services
# firewall-cmd --zone=trusted --list-services
  • 1
  • 2
CentOS7防火墙firewalld简单配置和使用

    网上找了好多文章,关于CentOS7的防火墙配置和使用,都没有比较理想的说明firewalld的用法,还有一些网上摒弃centos7 firewalld防火墙,使用旧版本的iptables的替代的做法,这里笔者非常不赞同其再使用iptables。

    CentOS7使用的是Linux Kernel 3.10.0的内核版本,新版的Kernel内核已经有了防火墙netfilter,并且firewalld的使用效能更高,稳定性更好

        CentOS7配置防火墙的两种方法:

    一、使用xml配置文件的方式配置;

方法一
cp /usr/lib/firewalld/services/http.xml /etc/firewalld/services/
firewall-cmd --reload

    二、使用命令的方式配置;

方法二
##Add
firewall-cmd --permanent --zone=public --add-port=80/tcp

##Remove
firewall-cmd --permanent --zone=public --remove-port=80/tcp

##Reload
firewall-cmd --reload

 

   其中,方法二的配置方式是间接修改/etc/firewalld/zones/public.xml文件,方案一也需要在public.xml里面新增<service name="http"/>,否则http的防火墙规则不会生效,而且两种配置方式都需要重新载入防火墙。

https://blog.csdn.net/gg_18826075157/article/details/72834694

从CentOS7(RHEL7)开始,官方的标准防火墙设置软件从iptables变更为firewalld,相信不少习惯使用iptables的人会感到十分不习惯,但实际上firewalld更为简单易用。 
大致用法就是:把可信任的IP地址添加到“trusted”区域,把不可信任的IP地址添加到“block”区域,把要公开的网络服务添加到“public”区域。

配置方式

firewalld的配置文件一般存放在下面两个目录:

  1. /etc/firewalld/
  2. /usr/lib/firewalld/

当需要一个文件时firewalld会优先使用第一个目录的。

  
这两个配置目录的结构很简单,主要是两个文件和三个目录:

  1. 文件: 
    • firewalld.conf:主配置文件(只有5个配置项) 
      • ①DefaultZone:默认使用的zone;
      • ②MinimalMark:使用标记的最小值;
      • ③CleanuupOnExit:退出后是否清除防火墙规则;
      • ④Lockdown:是否限制别的程序通过D-BUS接口直接操作firewalld;
      • ⑤IPv6_rpfilter:判断所接受到的包是否是伪造的
    • lockdown-whitelist.xml:当Lockdown设置为yes时,规定哪些程序可以对firewalld进行操作
    • direct.xml:直接使用类似iptables的语法来进行规则的增删
  2. 目录: 
    • zones:保存zone配置文件
    • services:保存service配置文件
    • icmptypes:保存和icmp类型相关的配置文件

什么是区域zone

所谓的区域就是一个信赖等级,某一等级下对应有一套规则集。划分方法包括:网络接口、IP地址、端口号等等。一般情况下,会有如下的这些默认区域:

  1. drop:丢弃所有进入的数据包
  2. block:拒绝所有进入的数据包
  3. public:只接受部分选定的数据包
  4. external:应用在NAT设定时的对外网络
  5. dmz:非军事区
  6. work:使用在公司环境
  7. home:使用在家庭环境
  8. internal:应用在NAT设定时的对内网络
  9. trusted:接受所有的数据包

比如,public区域由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"/>
</zone>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

当然,你也可以定义自己的区域,只要在zones目录下新建一个同名的xml文件即可。

什么是服务service

iptables时代习惯使用端口号来匹配规则,但是如果某一个服务的端口号改变了,那就要同时更改iptables的规则,十分不方便,同时也不方便阅读理解。

service配置文件的命名为<服务名>.xml,比如ssh的配置文件是ssh.xml。


<?xml version="1.0" encoding="utf-8"?>
<service>
  <short>SSH</short>
  <description>Secure Shell (SSH) is a protocol for logging into and executing commands on remote machines. It provides secure encrypted communications. If you plan on accessing your machine remotely via SSH over a firewalled interface, enable this option. You need the openssh-server package installed for this option to be useful.</description>
  <port protocol="tcp" port="22"/>
</service>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

配置实例

1.开启firewalld

# systemctl enable firewalld
# systemctl start firewalld
  • 1
  • 2

2.往trusted区域中注册IP地址

# firewall-cmd --zone=trusted --add-source=xxx.xxx.xxx.xxx
  • 1

3.往public区域中注册网络接口

# firewall-cmd --zone=public --add-interface=eth0
  • 1

4.往public区域中注册新服务

# firewall-cmd --zone=public --add-service=http
# firewall-cmd --zone=public --add-service=https
  • 1
  • 2

5.删除public区域中不需要的服务

# firewall-cmd --zone=public --remove-service=dhcpv6-client
# firewall-cmd --zone=public --remove-service=ssh
  • 1
  • 2

6.查看各个区域的配置情况

# firewall-cmd --get-active-zones
public
  interfaces: eth0
trusted
  sources: xxx.xxx.xxx.xxx
  • 1
  • 2
  • 3
  • 4
  • 5

7.查看某个区域的服务

# firewall-cmd --zone=public --list-services
# firewall-cmd --zone=trusted --list-services
  • 1
  • 2

猜你喜欢

转载自blog.csdn.net/michaelwubo/article/details/80998556
今日推荐