RHEL Linux 7的防火墙firewalld学习总结,


RHEL Linux 7的防火墙firewalld学习总结

 

本文介绍一下RHEL 7下的防火墙配置工具firewalld。 在RHEL 7之前,我们一般使用iptables防火墙管理工具,其实,iptables与firewalld都不是真正的防火墙,它们都只是用来定义防火墙策略的防火墙管理工具而已,或者说,它们只是一种服务或程序。iptables服务会把配置好的防火墙策略交由内核层面的netfilter网络过滤器来处理,而firewalld服务则是把配置好的防火墙策略交由内核层面的nftables包过滤框架来处理。

 

firewalld(Dynamic Firewall Manager of Linux systems,Linux系统的动态防火墙管理器),在RHLE 7下是默认的防火墙配置管理工具,它拥有基于CLI(命令行界面 firewalld-cmd)和基于GUI(图形用户界面 firewalld-config)的两种管理方式。相较于传统的防火墙管理配置工具,firewalld支持动态更新技术并加入了区域(zone)的概念。简单来说,区域就是firewalld预先准备了几套防火墙策略集合(策略模板),用户可以根据生产场景的不同而选择合适的策略集合,从而实现防火墙策略之间的快速切换。它具备对IP V4和IP V6防火墙设置的支持。

 

防火墙中的一切都与一个或者多个区域相关联,下面对各个区进行说明:

 

Zone                                    Description 
-----------------------------------------------------
drop (immutable)             Deny all incoming connections, outgoing ones are accepted. 
block (immutable)            Deny all incoming connections, with ICMP host prohibited messages issued. 
trusted (immutable)          Allow all network connections 
public                       Public areas, do not trust other computers
external                     For computers with masquerading enabled, protecting a local network 
dmz                          For computers publicly accessible with restricted access.  
work                         For trusted work areas 
home                         For trusted home network connections 
internal                     For internal network, restrict incoming connections

 

drop(丢弃)

任何流入网络的包都被丢弃,不作出任何响应,只允许流出的网络连接。即使开放了某些服务(比如http),这些服务的数据也是不允许通过的。

 

block(限制)

任何进入的网络连接都被拒绝,并返回IPv4的icmp-host-prohibited报文或者IPv6的icmp6-adm-prohibited 报文。只允许由该系统初始化的网络连接。

 

trusted(信任)

可接受所有的网络连接。允许所有网络连接,即使没有开放任何服务,那么使用此zone的流量照样通过(一路绿灯)

 

public(公共)

在公共区域内使用,不能相信网络内的其他计算机不会对您的计算机造成危害,只能接收经过选取的连接。

 

external(外部)

特别是为路由器启用了伪装功能的外部网。您不能信任来自网络的其他计算,不能相信它们不会对您的计算机造成危害,只能接收经过选择的连接。

 

dmz(非军事区)

用以允许隔离区(dmz)中的电脑有限地被外界网络访问,只允许选中的服务通过。

 

work(工作)

用于工作区。您可以基本相信网络内的其他电脑不会危害您的电脑。只允许选中的服务通过。

 

home(家庭)

用于家庭网络。您可以基本信任网络内的其他计算机不会危害您的计算机。,只允许选中的服务通过。

 

internal(内部)

用于内部网络。您可以基本上信任网络内的其他计算机不会威胁您的计算机。只允许选中的服务通过。

 

 

 

 

 

检查防火墙工具

 

如果你要判断RHEL 7使用的是iptables还是firewalld,那么可以通过命令来判断

 

有iptables的情况:

 

# systemctl status iptables.service
● iptables.service
   Loaded: not-found (Reason: No such file or directory)
   Active: inactive (dead)

 

没有iptables的情况

 

 

# systemctl status iptables.service

Unit iptables.service could not be found.

 

 

当然,最简单的方法是使用下面命令来判别

 

# systemctl is-active firewalld
active
# systemctl is-active iptables
inactive
# systemctl is-active ip6tables
inactive
# systemctl is-active ebtables
inactive

 

 

 

查看防火墙状态

 

    # systemctl status firewalld

    # firewall-cmd --state

 

[root@mylnx ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
   Active: active (running) since Wed 2018-03-07 11:46:34 HKT; 1 day 4h ago
 Main PID: 124126 (firewalld)
   CGroup: /system.slice/firewalld.service
           └─124126 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid
 
Mar 07 11:46:34 mylnx systemd[1]: Starting firewalld - dynamic firewall daemon...
Mar 07 11:46:34 mylnx systemd[1]: Started firewalld - dynamic firewall daemon.
 
 
# firewall-cmd --state
running

 

 

启动防火墙服务:

 

 

# systemctl start firewalld

# systemctl start firewalld.service

 

 

重启防火墙服务:

 

# systemctl restart firewalld.service

 

 

# systemctl restart firewalld

 

 

关闭防火墙:

 

systemctl stop firewalld

 

 

[root@mylnx ~]# systemctl stop firewalld
[root@mylnx ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
   Active: inactive (dead) since Thu 2018-03-08 16:35:17 HKT; 6s ago
  Process: 124126 ExecStart=/usr/sbin/firewalld --nofork --nopid $FIREWALLD_ARGS (code=exited, status=0/SUCCESS)
 Main PID: 124126 (code=exited, status=0/SUCCESS)
 
Mar 07 11:46:34 mylnx systemd[1]: Starting firewalld - dynamic firewall daemon...
Mar 07 11:46:34 mylnx systemd[1]: Started firewalld - dynamic firewall daemon.
Mar 08 16:35:16 mylnx systemd[1]: Stopping firewalld - dynamic firewall daemon...
Mar 08 16:35:17 mylnx systemd[1]: Stopped firewalld - dynamic firewall daemon.
[root@mylnx ~]# 

 

 

禁用&启用防火墙:

 

开机时启用&禁用防火墙

 

# systemctl disable firewalld
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
Removed symlink /etc/systemd/system/basic.target.wants/firewalld.service.
# systemctl enable firewalld
Created symlink from /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service to /usr/lib/systemd/system/firewalld.service.
Created symlink from /etc/systemd/system/multi-user.target.wants/firewalld.service to /usr/lib/systemd/system/firewalld.service.

 

 

firewall-cmd帮助信息

 

# firewall-cmd --help

 

 

 

配置防火墙

 

 

firewall-cmd有临时设定(runtime)和永久(permanent)设定两种方式,临时设定reload后或重启服务器后会恢复原状态,而永久设定方式则不会。参数--permanent表示永久(permanent)设定。下面如果指定了参数--permanent表示永久设定。

 

 

 

1:列出当前有几个区域(zone)

 

 

[root@mylnx ~]# firewall-cmd --get-zones

block dmz drop external home internal public trusted work

[root@mylnx ~]#

 

 

2:  查看当前活动的区域(zone)

 

[root@mylnx ~]# firewall-cmd --get-active-zones

public

  interfaces: eth0

[root@mylnx ~]#

 

3: 查看当前默认的区域(zone)

 

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

public

 

 

4:设置当前默认的区域

 

[root@mylnx ~]# firewall-cmd --set-default-zone=public

Warning: ZONE_ALREADY_SET: public

success

 

5: 查看firewalld支持的服务。

 

 
[root@mylnx ~]# firewall-cmd --get-service   
RH-Satellite-6 amanda-client bacula bacula-client dhcp dhcpv6 dhcpv6-client dns freeipa-ldap freeipa-ldaps freeipa-replication ftp high-availability http https imaps ipp ipp-client ipsec iscsi-target kerberos kpasswd ldap ldaps libvirt libvirt-tls mdns mountd ms-wbt mysql nfs ntp openvpn pmcd pmproxy pmwebapi pmwebapis pop3s postgresql proxy-dhcp radius rpc-bind rsyncd samba samba-client smtp ssh telnet tftp tftp-client transmission-client vdsm vnc-server wbem-https

 

 

6:检查下一次重载后将激活的服务。

 

[root@mylnx ~]# firewall-cmd --get-service --permanent
 
RH-Satellite-6 amanda-client bacula bacula-client dhcp dhcpv6 dhcpv6-client dns freeipa-ldap freeipa-ldaps freeipa-replication ftp high-availability http https imaps ipp ipp-client ipsec iscsi-target kerberos kpasswd ldap ldaps libvirt libvirt-tls mdns mountd ms-wbt mysql nfs ntp openvpn pmcd pmproxy pmwebapi pmwebapis pop3s postgresql proxy-dhcp radius rpc-bind rsyncd samba samba-client smtp ssh telnet tftp tftp-client transmission-client vdsm vnc-server wbem-https

 

 

7: 列出区域开放的端口

 

[root@mylnx ~]# firewall-cmd --zone=public --list-ports

8123/tcp 8124/tcp 8217/tcp 80/tcp 443/tcp 3306/tcp 10050/tcp

 

 

8: 列出指定区域的设置信息

 

[root@mylnx ~]# firewall-cmd --zone=public --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens160
  sources: 
  services: dhcpv6-client ssh
  ports: 8123/tcp 8124/tcp 8217/tcp 80/tcp 443/tcp 3306/tcp 10050/tcp
  protocols: 
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules:

 

 

9:开放指定区域的特定端口号

 

 

例如,我们需要开放端口号8123

 

# firewall-cmd --zone=public --add-port=8123/tcp --permanent

success

# firewall-cmd --reload  #更新防火墙规则

 

--zone              #作用域

--add-port=8123/tcp     #添加端口,格式为:端口/通讯协议

--permanent         #永久生效,没有此参数重启后失效

 

注意:有没有--reload 选项的区别:

 

[root@mylnx ~]# firewall-cmd --zone=public --add-port=8124/tcp --permanent 
success
[root@mylnx ~]# firewall-cmd --zone=public --list-ports
8123/tcp
[root@mylnx ~]# firewall-cmd --reload
success
[root@mylnx ~]# firewall-cmd --zone=public --list-ports
8123/tcp 8124/tcp

 

另外,还需要注意--reload与--complete-reload的区别:

 

# firewall-cmd --reload

# firewall-cmd --complete-reload

 

两者的区别就是第一个无需断开连接,就是firewalld特性之一动态添加规则,第二个需要断开连接,类似重启服务

 

 

 

 

如何一次开放多个端口呢? 这里有两种方法,总结如下:

 

方法1:使用shell脚本,循环添加多个端口。

 

#!/bin/bash
for i in 22  21 1337 3306 31337
do
  firewall-cmd --zone=public --add-port=${i}/tcp
done

 

方法2:如果端口是区间段,那么可以使用下面命令:

 

# firewall-cmd --zone=public --add-port=6001-6020/tcp --permanent
success
# firewall-cmd --reload
success
# firewall-cmd --zone=public --list-ports
8123/tcp 8124/tcp 8217/tcp 80/tcp 443/tcp 3306/tcp 10050/tcp 6001-6020/tcp

 

 

 

 

10:删除开放的端口

 

 

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

 

 

 

移除一个区间段的端口号:

 

# firewall-cmd --zone=public --remove-port=6001-6020/tcp --permanent

success

 

# firewall-cmd  --reload

success

 

 

11: 查看有哪些服务可以打开。

 

 

# firewall-cmd --get-services

 

 

12: 开放某个服务

 

# firewall-cmd --add-service=zabbix-agent --permanent

success

 

# firewall-cmd --zone=public --add-service=zabbix-agent --permanent

success

 

 

 

13: 关闭某个服务

 

# firewall-cmd --remove-service=zabbix-agent --permanent

success

# firewall-cmd --reload

success

 

 

 

14:   查看当前开放了哪些服务

 

 

# firewall-cmd --list-services

dhcpv6-client ssh

 

# firewall-cmd --zone=public --list-services

dhcpv6-client ssh

 

 

查询某个服务是否开放

 

# firewall-cmd --query-service ftp

no

# firewall-cmd --query-service ssh

yes

 

15:  一些不常用的应急命令

 

firewall-cmd --panic-on         # 拒绝所有流量,远程连接会立即断开,只有本地能登陆 
firewall-cmd --panic-off        # 取消应急模式,但需要重启firewalld后才可以远程ssh 
firewall-cmd --query-panic         # 查看是否为应急模式

 

16: 设置某些IP允许访问某个服务

 

 

firewall-cmd --permanent --zone=public --add-rich-rule="rule family="ipv4" source address="192.168.0.4/24" service name="ssh" accept"
firewall-cmd --permanent --zone=public --remove-rich-rule="rule family="ipv4" source address="192.168.0.4/24" service name="ssh" accept"

 

 

 

 

 

 

 

firewall配置文件

 

这里存放了一些默认的文件。如默认区域和公用服务。避免修改它们,因为每次 firewall 软件包更新时都会覆盖这些文件

 

 

$ ls -lrt /usr/lib/firewalld
total 16
drwxr-xr-x. 2 root root  224 Feb 18 11:03 helpers
drwxr-xr-x. 2 root root   20 Feb 18 11:03 ipsets
drwxr-xr-x. 2 root root 4096 Feb 18 11:03 icmptypes
drwxr-xr-x. 2 root root 8192 Feb 18 11:03 services
drwxr-xr-x. 2 root root  163 Feb 18 11:03 zones

 

 

这里存放了用户自定义数据

 

$ ls -lrt /etc/firewalld
total 8
drwxr-x---. 2 root root   46 Feb  5 00:29 zones
drwxr-x---. 2 root root    6 Feb  5 00:29 services
-rw-r--r--. 1 root root  272 Feb  5 00:29 lockdown-whitelist.xml
drwxr-x---. 2 root root    6 Feb  5 00:29 ipsets
drwxr-x---. 2 root root    6 Feb  5 00:29 icmptypes
drwxr-x---. 2 root root    6 Feb  5 00:29 helpers
-rw-r--r--. 1 root root 2006 Feb  5 00:29 firewalld.conf

 

 

firewall命令非常丰富、灵活,限于篇幅和经验,不可能面面俱到,这里只是总结了一些比较常用、简单的命令。后续在工作中遇到了一些特殊需求,在不断学习和完善。

 

 

相关内容

    暂无相关文章