Linux中的防火墙————Firewalld

firewalld介绍和原理

firewalld是一个服务,这个服务提供了防火墙配置的工具

只要开启了firewalld服务,那么就可以通过firewall服务提供的工具来配置防火墙

Linux本身不具备防火墙功能,而是通过内核的net_filter模块来实现软防火墙功能,而且你必须通过iptables才能和net_filter进行交互

默认在rhel7和fedora20,centos7以上的版本默认安装了firewalld服务,默认是开启的

##  开启防火墙
[root@client ~]# systemctl start firewalld

## 查看firewalld状态
[root@client ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
     Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; preset: enabled)
     Active: active (running) since Wed 2024-03-20 15:47:15 CST; 1min 23s ago
       Docs: man:firewalld(1)
   Main PID: 4076 (firewalld)
      Tasks: 2 (limit: 10803)
     Memory: 25.7M
        CPU: 375ms
     CGroup: /system.slice/firewalld.service
             └─4076 /usr/bin/python3 -s /usr/sbin/firewalld --nofork --nopid

Mar 20 15:47:14 client systemd[1]: Starting firewalld - dynamic firewall daemon...
Mar 20 15:47:15 client systemd[1]: Started firewalld - dynamic firewall daemon.

## 查看iptables规则

firewalld的服务提供了一个更加简单的方式来配置防火墙,原理就是firewalld服务将你的防火墙配置命令转换成iptables规则

firewalld提供了两中管理工具,一种图形化管理工具firewall-offline-cmd,另一种命令行管理工具firewall-cmd

firewall-cmd原理

firewalld提供了zone的概念,zone是区域的意思,firewalld将我们的系统划分成了一个个zone。

znoe的边界取决于网卡

firewalld提供了zone,一个网卡只能属于一个zone,那zone的边界就是属于该zone的网卡。

firewalld的zone里面有网卡,有规则(rule)。如果一个网卡属于firewalld的某个zone,那么这个zone里面的所有规则都会应用到该zone内的网卡

  • 网卡,zone和规则

zone包含网卡和规则,网卡在zone里面就应用这个zone里面的规则

我们把firewalld学明白,就搞定这三个感念

zone的概念比较简单,将网卡划分入zone也很简单,复杂的就只有规则(rule)。

firewalld的操作

zone

列出所有的zone

[root@server ~]# firewall-cmd --list-all-zones 
block
  target: %%REJECT%%
  icmp-block-inversion: no
  interfaces: 
  sources: 
  services: 
  ports: 
  protocols: 
  forward: yes
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 

dmz
  target: default
  icmp-block-inversion: no
  interfaces: 
  sources: 
  services: ssh
...

 只要不是特别复杂的网络场景,一个zone可以解决我们所有的问题

列出"缺省的zone"的规则

[root@server ~]# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens160 ens224
  sources: 
  services: cockpit dhcpv6-client ssh
  ports: 
  protocols: 
  forward: yes
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 

firewall-cmd添加规则正常来说必须指定zone,因为规则实在zone里面,那你肯定添加规则的时候要指定这个规则在哪个zone里面。

有些时候如果每一次添加规则都指定zone会显得比较复杂,因为大多数情况下一个zone就能满足我的需求,所以firewalld给我们提供一个缺省的zone,没有指定zone的操作,都相当于操作缺省的zone。

列出指定zone的规则

## 列出home zone的规则
[root@server ~]# firewall-cmd --list-all --zone=home 
home
  target: default
  icmp-block-inversion: no
  interfaces: 
  sources: 
  services: cockpit dhcpv6-client mdns samba-client ssh
  ports: 
  protocols: 
  forward: yes
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 

查看缺省的zone

[root@server ~]# firewall-cmd --get-default-zone 
public

可以看到缺省的zone的名字为public

  • 将缺省的zone更改为其他zone

## 将缺省的zone更改为home zone
[root@server ~]# firewall-cmd --set-default-zone=home 
success
[root@server ~]# firewall-cmd --get-default-zone 
home

可以看到现在缺省的zone变成了home zone

## 将默认的zone改回来
[root@server ~]# firewall-cmd --set-default-zone=public 
success
[root@server ~]# firewall-cmd --get-default-zone 
public

网卡(interface)

默认Linux的所有网卡都在默认的zone里面

注意:像我们讲的这种防火墙,无论iptables还是firewalld,都只能应用于内核管理的网卡,有些网卡如果不受内核管理,那防火墙的规则也无法应用,应用管理防火墙的工具(DBDK)

列出当前zone中的网卡

[root@server ~]# firewall-cmd --list-interfaces --zone=public 
ens224 ens160

不加--zone=public,列出的是缺省的zone的网卡

移除一个zone中的网卡

[root@server ~]# firewall-cmd --remove-interface=ens160 
success
[root@server ~]# firewall-cmd --list-interfaces 
ens224

给一个zone添加网卡

[root@server ~]# firewall-cmd --add-interface=ens160 --zone=public 
success
[root@server ~]# firewall-cmd --list-interfaces 
ens160 ens224

规则(rule)

查看指定zone的规则

[root@server ~]# firewall-cmd --list-all --zone=home 
home
  target: default
  icmp-block-inversion: no
  interfaces: 
  sources: 
  services: cockpit dhcpv6-client mdns samba-client ssh
  ports: 
  protocols: 
  forward: yes
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 
[root@server ~]# firewall-cmd --list-all 
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens160 ens224
  sources: 
  services: cockpit dhcpv6-client ssh
  ports: 
  protocols: 
  forward: yes
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 

如果一个zone里面没有网卡,那么这个zone就是非active状态 一个zone里面必须要有网卡,这个zone才有实际意义

三种规则

第一种:service规则

第二种:port规则

第三种:rich rule(富规则)

service规则

查看指定zone的service规则
## 查看home和缺省的zone的service规则
[root@server ~]# firewall-cmd --list-services --zone=home 
cockpit dhcpv6-client mdns samba-client ssh    
[root@server ~]# firewall-cmd --list-services
cockpit dhcpv6-client ssh
添加service规则
## 给home和缺省的zone添加ftp规则
[root@server ~]# firewall-cmd --add-service=ftp
success
[root@server ~]# firewall-cmd --add-service=ftp --zone=home 
success
[root@server ~]# firewall-cmd --list-services --zone=public 
cockpit dhcpv6-client ftp ssh
[root@server ~]# firewall-cmd --list-services --zone=home 
cockpit dhcpv6-client ftp mdns samba-client ssh

可以看到,在两个中有了ftp的规则

现在ftp的流量就可以过来,但不是所有的流量都能进来

删除service规则
## 删除刚才定义的ftp的规则
[root@server ~]# firewall-cmd --remove-service=ftp
success
[root@server ~]# firewall-cmd --remove-service=ftp --zone=home 
success
[root@server ~]# firewall-cmd --list-services --zone=home 
cockpit dhcpv6-client mdns samba-client ssh
[root@server ~]# firewall-cmd --list-services 
cockpit dhcpv6-client ssh
测试

可以搭建一个web服务器进行测试

1、在server搭建http服务

[root@server ~]# yum install -y httpd
[root@server ~]# echo firewalld-test >> /var/www/html/index.html
[root@server ~]# systemctl restart httpd

2、访问

[root@server ~]# curl 192.168.200.128
firewalld-test
[root@client ~]# curl  192.168.200.128
curl: (7) Failed to connect to 192.168.200.128 port 80: No route to host

3、写入规则

[root@server ~]# firewall-cmd --add-service=http
success

4、再次访问

[root@client ~]# curl  192.168.200.128
firewalld-test

把规则写入之后就可以对http进行访问了

service的原理

/usr/lib/firewalld/services/目录下有许多xml文件,这些文件就定义一些常用service的的端口号

## 查看https.xml
[root@client ~]# cat /usr/lib/firewalld/services/https.xml 
<?xml version="1.0" encoding="utf-8"?>
<service>
  <short>Secure WWW (HTTPS)</short>
  <description>HTTPS is a modified HTTP used to serve Web pages when security is important. Examples are sites that require logins like stores or web mail. This option is not required for viewing pages locally or developing Web pages. You need the httpd package installed for this option to be useful.</description>
  <port protocol="tcp" port="443"/>
</service>

在添加service的规则的时候,实际上就是firewalld将443端口开放,让外面的服务可以通过这个端口号进行访问

这个service规则只是一个名字,至于service到底放行的是什么,就要看这个service代表的端口号是多少,这个service也可以自己来编写

如果你要放行的端口并没有在service规则的xml文件里面定义,那么你就不能用service规则满足你的条件

如果是复杂的流量的话,你需要通过port规则放行流量,也可以自己编写service规则

port规则
添加port规则
[root@server ~]# firewall-cmd --add-port=3333/tcp
success
[root@server ~]# firewall-cmd --add-port=5555/udp
success
查看port规则
[root@server ~]# firewall-cmd --list-ports --zone=public 
3333/tcp 5555/udp
[root@server ~]# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens160 ens224
  sources: 
  services: cockpit dhcpv6-client ssh
  ports: 3333/tcp 5555/udp
  protocols: 
  forward: yes
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 
删除port规则
[root@server ~]# firewall-cmd --remove-port=3333/tcp
success
[root@server ~]# firewall-cmd --remove-port=5555/udp
success
[root@server ~]# firewall-cmd --list-ports --zone=public 

[root@server ~]# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens160 ens224
  sources: 
  services: cockpit dhcpv6-client ssh
  ports: 
  protocols: 
  forward: yes
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 
rich rule

有点像iptables

添加rich rule
[root@server ~]# firewall-cmd --add-rich-rule="rule family=ipv4 source address=192.168.200.0/24 port port=8080 protocol=tcp reject"
success
使添加的规则永久生效

上面做的规则都是临时的

方式一

如果让上面做的临时规则变成永久的,可以使用一下命令

[root@server ~]# firewall-cmd --runtime-to-permanent 
success

如果使用firewall-cmd --reload命令之后规则还会存在,就是永久的规则

firewall-cmd --reload:表示重新加载防火墙规则

[root@server ~]# firewall-cmd --reload 
success
[root@server ~]# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens160 ens224
  sources: 
  services: cockpit dhcpv6-client ssh
  ports: 
  protocols: 
  forward: yes
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 
        rule family="ipv4" source address="192.168.200.0/24" port port="8080" protocol="tcp" reject

方式二

使用--permanent,在规则后面添加这一条命令,就可以直接创建永久规则

但是不会立即生效,在list中不会显示,如果要立即生效,就需要使用--reload

[root@server ~]# firewall-cmd --add-rich-rule="rule family=ipv4 source address=192.168.200.0/24 port port=443 protocol=tcp accept" --permanent 
success
## 进行查看
[root@server ~]# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens160 ens224
  sources: 
  services: cockpit dhcpv6-client ssh
  ports: 
  protocols: 
  forward: yes
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 
        rule family="ipv4" source address="192.168.200.0/24" port port="8080" protocol="tcp" reject
## 可以看到定义的规则并没有立即生效

## 使用--reload
[root@server ~]# firewall-cmd --reload 
success
[root@server ~]# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens160 ens224
  sources: 
  services: cockpit dhcpv6-client ssh
  ports: 
  protocols: 
  forward: yes
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 
        rule family="ipv4" source address="192.168.200.0/24" port port="8080" protocol="tcp" reject
        rule family="ipv4" source address="192.168.200.0/24" port port="443" protocol="tcp" accept

可以看到在使用--permanent之后并没有立即生效,在执行--reload之后,配置就会重新加载

建议

建议先写一遍有--permanent,在写一遍没有--permanent

[root@server ~]# firewall-cmd --add-rich-rule="rule family=ipv4 source address=192.168.200.0/24 port port=443 protocol=tcp accept" --permanent 
[root@server ~]# firewall-cmd --add-rich-rule="rule family=ipv4 source address=192.168.200.0/24 port port=443 protocol=tcp accept" 

一条命令执行两边

删除rich rule
[root@server ~]# firewall-cmd --remove-rich-rule="rule family=ipv4 source address=192.168.200.0/24 port port=443 protocol=tcp accept" --permanent 
success
[root@server ~]# firewall-cmd --remove-rich-rule="rule family=ipv4 source address=192.168.200.0/24 port port=443 protocol=tcp accept"

删除和添加一样,永久添加不会立即执行,临时添加重启之后就会消失,所以还是建议使用这种方式进行删除

相关推荐

  1. Linux防火墙————Firewalld

    2024-04-10 16:16:03       14 阅读
  2. RHEL/CentOSfirewalld防火墙服务配置

    2024-04-10 16:16:03       43 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-04-10 16:16:03       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-10 16:16:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-10 16:16:03       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-10 16:16:03       20 阅读

热门阅读

  1. NLopt

    2024-04-10 16:16:03       16 阅读
  2. 使用opencv + ffmpeg 开发视频播放器Demo

    2024-04-10 16:16:03       18 阅读
  3. k8s-pod设置执行优先级

    2024-04-10 16:16:03       16 阅读
  4. 在Pod设置limit 的情况下,如何让JDK容器适配

    2024-04-10 16:16:03       20 阅读
  5. “AI程序员上岗:垂类大模型应用蓬勃发展“

    2024-04-10 16:16:03       17 阅读
  6. hdc常用命令大全

    2024-04-10 16:16:03       17 阅读
  7. 分库,分表,分区,分片

    2024-04-10 16:16:03       16 阅读
  8. 【无标题】Ant Design of Vue + vue2.0 + 纯前端下载word

    2024-04-10 16:16:03       19 阅读
  9. Golang基础-11

    2024-04-10 16:16:03       18 阅读
  10. 2.Go的基本语法-指针、结构体、Map

    2024-04-10 16:16:03       16 阅读