1.简介

 

Linux服务器的功能是非常强大的。不仅可以作为正常的服务器、路由器,也可以作为防火墙使用。在RHEL7之前的系统中,防火墙一般指的就是iptables,防火墙的功能是通过iptables调用//lib/modules/2.6.32-358.el6.x86_64/kernel/net/netfilter来实现的。但是在RHEL7中,有三个能实现防火墙功能的“防火墙iptablesfirewallebtables

 

RHEL7系统中,Redhat建议使用的是firewall这个防火墙,其它两个建议关闭。因为三个防火墙之间相互冲突,并且在安装系统时默认都是开启状态的。

 

例:关闭并锁定iptablesebtables防火墙

[root@Desktop30 ~]# systemctl stop  iptabes

[root@Desktop30 ~]# systemctl disable  iptabes

[root@Desktop30 ~]# systemctl mask  iptabes  

ln -s '/dev/null' '/etc/systemd/system/iptabes.service'

 

[root@Desktop30 ~]# systemctl stop  ebtables

[root@Desktop30 ~]# systemctl disable  ebtables     

[root@Desktop30 ~]# systemctl mask  ebtables       

ln -s '/dev/null'  '/etc/systemd/system/ebtables.service'

----------------------------------------------------查看状态-----------------------------------------------------

[root@Desktop30 ~]# systemctl status  iptables

iptables.service - IPv4 firewall with  iptables

    Loaded: loaded (/usr/lib/systemd/system/iptables.service; disabled)

    Active: inactive (dead)

 

[root@Desktop30 ~]# systemctl status  ebtables

ebtables.service

    Loaded: masked (/dev/null)

    Active: inactive (dead)

 

May 12 11:15:34 freeit.example.com  systemd[1]: Stopped Ethernet Bridge Filte....

Hint: Some lines were ellipsized, use -l  to show in full.

其它两个防火墙关闭之后,下面,我们就开始介绍firewall的强大功能。

 

2.Firewall图形化

 

2.1.图形化介绍

 

1:打开图形化界面

输入命令firewall-config,打开图形化,如下:

wKiom1VUFOXy5sLgAAJHOvh9plE675.jpg

 

  • configuration

此处有两个状态,如下:

wKioL1VUFn3ivuEkAAIsOK7q5lw365.jpg

  1. Runtime:默认状态。运行时,为当前状态,此状态下的firewall设置只是当前生效,下    次重启失效;

  2. Permanent:下次启动状态,此状态下的配置下次重启生效,为永久状态,设置不对当前生效。

 

注:

  • Permanent下设置的状态虽然下次重启生效,但是可以设置让其加载到当前状态生效,如下:

wKiom1VUFpqTlRq4AAIY5VCFvM0259.jpg

点击菜单【Options---选择【Reload Firewall】即可。

  • 在实验环境及真实生产环境中,建议使用此状态设置firewall,不要使用Runtime

 

  • Zone

区域,下包含多种策略:blockdmzdropexternalhomeinternalpublictrustedwork。缺省采用public策略。可以针对不同的情况设置不同的策略。举个简单例子:实际生活中,我们使用一台电脑上班、在家上网,而一些公司要求严格,上班期间不能聊QQ、看视频等,但是休息期间可以,而使用iptables的话,就可能会出现上班添加策略,休息期间删除策略的情况。但是,使用firewall却可以针对不同的情况使用不同的策略,上班可以使用work策略,下班可以使用nowork策略。这样就可以简单、自动化。针对这些策略,我们只介绍以下一个:

  1. block:所有策略拒绝,有提示。相当于iptables中的REJECT选项;

  2. drop:所有策略拒绝,无提示。相当于iptables中的DROP选项;

  3. trusted:默认都允许,相当于iptables中的ACCEPT选项;

  4. public:缺省使用策略,规则只允许servicesdhcpc6-clientssh

  5. u  策略选项

此选项值介绍以下几种

  • Services:针对服务的策略设置。缺省的public策略只允许dhcpc6-clientssh两个服务。其他策略此项缺省都拒绝。

wKioL1VUGEGRItwoAAD_ggztDpU676.jpg

wKiom1VUFsfyZuCxAADuHx5Hju8641.jpg

  • Ports:端口选项。为自定义项,缺省没有任何选项,要自己添加允许被访问的端口或端口范围;

wKiom1VUFv_Bd2ApAAH_bgll20A225.jpg

wKioL1VUGHqDuEgHAACg6h_3dQM566.jpg

  • Masquerading:地址伪装,仅支持IPV4,打开此选项将开启IPV4地址转发功能;

wKioL1VUGLGzqHdyAAIINRqUTJU136.jpg

wKiom1VUFzeyMEeeAAC11tDMs5Y025.jpg

  • Port Forwarding:端口转发,缺省无任何设置,同样为自定义选项。仅转发Ipv4

wKiom1VUF1iRn9ExAAIiK2u2pus149.jpg

wKioL1VUGNPAp-0zAAD1urqQen0166.jpg

  • ICMP Filtericmp过滤,支持以下设置(缺省无设置,即拒绝所有)

wKioL1VUGPvhkDHPAAJbCaUa4JE974.jpg

wKiom1VUF4GSJWcqAAEypg0nv8c818.jpg

  • Rich Rules:富规则,可以做比services更加颗粒化的设置,缺省无任何设置,自定义项;

wKiom1VUF6ryVsUvAAHouzPB1LM973.jpg

wKioL1VUGSXhx-QUAACepzrLaoY843.jpg

  • Interface:接口选项,缺省无设置,需自定义;

wKioL1VUGVKBc0V7AAIK-wa1Cmo911.jpg

wKioL1VUGVOAwOPBAACjydUYyHo077.jpg

  • Sources:源,可以添加源地址。缺省无设置,需自定义;

wKiom1VUGAuxGumjAAHcYtUIdow723.jpg

wKioL1VUGYXDFiRuAAB16gXdUUE247.jpg

2.2.图形化设置

 

下面,针对以上的几个策略选项举例并做简单的设置。

首先把Runting(当前状态)状态的firewall配置调用到Permanent状态,并点击【options-->Reload Firewall】。

 

2.2.1.services

1:安装http服务,创建索引,真机访问http

[root@Desktop30 ~]# yum -y install  httpd   

……

[root@Desktop30 ~]# cd /var/www/html/

[root@Desktop30 html]# touch index.html

[root@Desktop30 html]# echo Hello! how  are you! >index.html 

[root@Desktop30 html]# cat index.html

Hello!  how are you!

[root@Desktop30 html]# systemctl restart  httpd

[root@Desktop30 html]# systemctl enable  httpd

ln -s  '/usr/lib/systemd/system/httpd.service' '/etc/systemd/system/multi-user.target.wants/httpd.service'

 

本机访问

wKioL1VUGaqTDBLvAACB2tKuC7k408.jpg

如上:可以访问。

 

桥接的真机访问查看:

wKioL1VUGcqymmg6AADL4cJ-Q2A855.jpg

如上:由于firewall策略原因,访问失败。

查看缺省策略

wKiom1VUGGejS1KfAAIlEX_kiys291.jpg

如上:firewall的缺省策略publicServices选项的http没有勾选,所以,默认拒绝所有访问本机http服务的主机。下面,勾选http服务,再次验证:


wKiom1VUGJ_A-piVAAHxgRagP1o395.jpg

注:

  • 勾选之后,不要忘记点击【options--ReloadFirewalld】,把在Permanent的设置调用到当前状态;

  • Firewall的设置与iptables不同,Firewall设置完后不用保存,但是上个操作一定要做。

 

设置完后再次验证

wKioL1VUGhiy0uWfAACfwxe8fDY209.jpg


如上:访问成功。

 

2.2.2.Rich Rules

Services选项尽管可以控制对本机服务的访问,但是,对象是针对所有,并没有针对单独的主机。如果想针对单独的主机,可以通过Rich Rules来实现。

 

2:只允许同网段的11主机访问本机的ssh服务,禁止其它主机的访问

wKioL1VUGnDTXdkNAAFI439VEZk309.jpg

去掉Public策略的services选项的ssh服务的勾选。然后选择Rich Rules

wKiom1VUGPaR6ZlEAACIHEfXZik403.jpg

点击【Add】添加策略

wKiom1VUGSiRNqOMAAEaTYR_x2E199.jpg

  • Family】选择【ipv4

  • Element】选择【serviceàssh

  • Action】选择【accept

  • source】填写允许的主机的地址

设置完成后点击【OK

wKioL1VUGqKSpmAPAAB630pM3EM401.jpg


点击【options--Reload Firewalld】,让设置当前生效。

 

验证:

  • 桥接的真实机使用putty访问

wKiom1VUGV_wBNKRAAFcvWZ74p8256.jpg

wKioL1VUGtqjRsNVAABiD8GKkDw761.jpg

访问失败。

  • 使用同网段的11主机访问

[root@freeit ~]# ssh -o  StrictHostKeyChecking=no root@192.168.10.10

Warning: Permanently added '192.168.10.10'  (ECDSA) to the list of known hosts.

root@192.168.10.10's password:

Last login: Tue May 12 12:53:14 2015

[root@Desktop30 ~]# ifconfig eth0 |grep  inet |cut -d " " -f10

192.168.10.10

fe80::20c:29ff:fe70:f064

 

访问成功。

 

2.2.3.Interface

 

3:当同网段的主机通过本机的eth0接口访问时,通过home设置的策略,允许访问ssh

wKiom1VUGZ3jsMt4AAHsmscIIko800.jpg

wKioL1VUGxqQtiBdAAImiNfiTpY478.jpg


wKiom1VUGaKDKloRAAIYRXRTalw524.jpg

然后加载到当前状态。

wKioL1VUGx7QFqoIAAH9Ew1CZvw998.jpg

访问测试:

分别用桥接的真实机和同网段的另一台机子实验。两台主机都能连接,则说明了是经由了home策略的验证。因为缺省的public策略,我们取消了servicesssh,只允许同网段的11主机可以连接。

桥接的真机通过ssh连接:

wKiom1VUGfKyYPWCAAF7l3NwjUs999.jpg

输入要连接的主机地址;

点击打开

wKioL1VUG26R3uZGAAF52oNvTXA969.jpg

wKioL1VUG27SdX1iAABxdzZSVSs189.jpg

连接成功。

 

同网段的11主机访问

[root@freeit ~]# ifconfig eth0 |grep inet  |cut -d " " -f10

192.168.10.11

fe80::20c:29ff:fe02:1ce7

[root@freeit ~]# ssh -o  StrictHostKeyChecking=no root@192.168.10.10

root@192.168.10.10's password:

Last login: Tue May 12 15:56:19 2015 from  192.168.10.1

[root@freeit ~]# ifconfig eth0 |grep inet  |cut -d " " -f10

192.168.10.10

fe80::20c:29ff:fe70:f064

连接成功。

 

2.2.4.sources

此选项可以让我们针对指定的源地址设置策略。

 

4:设置,让源为桥接的真机的http连接,通过home策略验证,能够正常访问,源为同网段的11主机通过缺省public验证,不能正常访问(此例需去除上个实验的配置,以免产生影响)

 

home策略设置

wKiom1VUGjvCrzUjAAJZMq7uUHY577.jpg

zone选择【home】策略

services项勾选【http

wKioL1VUG7ehL7W0AAIHxoSqRB4638.jpg

zone选择【home】策略

选择【sources

添加桥接的真机的地址

 

Public策略设置

wKioL1VUG-SA_DPgAAI89t1L_hg179.jpg

zone选择【public】策略

选择【services

去除【http】的勾选

wKioL1VUHADigq_mAAH-BOVZimw389.jpg

zone选择【public】策略

选择【sources

添加同网段的11主机

 

测试:

真机访问http

wKioL1VUHBegE_5zAAC_-ojsnMs967.jpg

同网段11主机访问http

wKiom1VUGsjAY1A7AAFuoH-aSEM533.jpg


3.Firewall 命令行

命令:

firewall-cmd

 

3.1.查看策略

1:查看命令支持参数

[root@freeit ~]# firewall-cmd --

--add-forward-port=                   --lockdown-off

--add-icmp-block=                     --lockdown-on

--add-interface=                      --panic-off

--add-lockdown-whitelist-command=     --panic-on

--add-lockdown-whitelist-context=     --permanent

--add-lockdown-whitelist-uid=         --query-forward-port=

--add-lockdown-whitelist-user=        --query-icmp-block=

--add-masquerade                      --query-interface=

--add-port=                           --query-lockdown

--add-rich-rule                        --query-lockdown-whitelist-command=

--add-service=                         --query-lockdown-whitelist-context=

--add-source=                         --query-lockdown-whitelist-uid=

--change-interface=                    --query-lockdown-whitelist-user=

--change-source=                      --query-masquerade

--change-zone=                        --query-panic

--complete-reload                     --query-port=

--direct                               --query-rich-rule

--get-active-zones                    --query-service=

--get-default-zone                    --query-source=

--get-icmptypes                       --reload

--get-services                        --remove-forward-port=

--get-zone-of-interface=              --remove-icmp-block=

--get-zones                            --remove-interface=

--help                                 --remove-lockdown-whitelist-command=

--list-all                            --remove-lockdown-whitelist-context=

--list-all-zones                       --remove-lockdown-whitelist-uid=

--list-forward-ports                   --remove-lockdown-whitelist-user=

--list-icmp-blocks                    --remove-masquerade

--list-interfaces                     --remove-port=

--list-lockdown-whitelist-commands    --remove-rich-rule

--list-lockdown-whitelist-contexts    --remove-service=

--list-lockdown-whitelist-uids        --remove-source=

--list-lockdown-whitelist-users       --set-default-zone=

--list-ports                          --state

--list-rich-rules                     --version

--list-services                       --zone=

--list-sources                       

 

2:查看缺省zone--public的缺省策略

[root@freeit ~]# firewall-cmd --list-all

public (default, active)

   interfaces: eth0

   sources: 192.168.10.11

//上个实验我们设置的策略,源为同网段11主机不能正常访问http服务。

   services: dhcpv6-client ssh

   ports:

   masquerade: no

   forward-ports:

   icmp-blocks:

   rich rules:

3:查看所有zone的缺省策略

[root@freeit ~]# firewall-cmd  --list-all-zones

block

   interfaces:

   sources:

   services:

   ports:

   masquerade: no

   forward-ports:

   icmp-blocks:

   rich rules:

 

dmz

   interfaces:

   sources:

   services: ssh

   ports:

   masquerade: no

   forward-ports:

   icmp-blocks:

   rich rules:

……

 

3.2.移除操作

4:移除Publicsources设置

 [root@freeit  ~]# firewall-cmd --remove-source=192.168.10.11 --permanent  

success

[root@freeit ~]# firewall-cmd --reload

success

[root@freeit ~]# firewall-cmd --list-all

public (default, active)

   interfaces: eth0

   sources:

   services: dhcpv6-client ssh

   ports:

   masquerade: no

   forward-ports:

   icmp-blocks:

   rich rules:

5:移除Publicservicesssh设置

[root@freeit ~]# firewall-cmd --remove-service=ssh  

success

[root@freeit ~]# firewall-cmd  --remove-service=ssh --permanent

success

[root@freeit ~]# firewall-cmd --reload

success

[root@freeit ~]# firewall-cmd --list-all

public (default, active)

   interfaces: eth0

   sources:

   services: dhcpv6-client

//默认允许所有的ssh连接本机被删除

   ports:

   masquerade: no

   forward-ports:

   icmp-blocks:

   rich rules:

 

注:

  • firewall-cmd设置策略时,要加上- -permanent参数,否则所有操作默认在Runtime状态生效,重启失效;

  • - -permanent状态下做完的操作要使用--reload参数使之当前生效,否则下次启动才生效。

 

3.3.查看及修改默认zone区域

6:查看默认zone策略及修改默认zone

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

Public

----------------------------------------------修改当前默认zone策略为home--------------------------------

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

home

注:

  • 修改默认zone策略,不需加- -permanent参数,且不用--reload,不仅为当前生效,下次重启同样生效,即使在Runtime状态下做(实验得出)

 

3.4.默认zone区域添加策略

7:为public添加策略,允许所有主机访问http访问

[root@freeit ~]# firewall-cmd  --add-service=http --permanent

success

[root@freeit ~]# firewall-cmd --reload

success

[root@freeit ~]# firewall-cmd --list-all

public (default, active)

   interfaces: eth0

   sources:

   services: dhcpv6-client http

   ports:

   masquerade: no

   forward-ports:

   icmp-blocks:

   rich rules:

 

8:添加富规则,值允许同网段的11主机访问本机的ssh服务

[root@freeit ~]# firewall-cmd  --add-rich-rule 'rule family=ipv4 source address=192.168.10.11 service  name=ssh accept'  --permanent 

success

[root@freeit ~]# firewall-cmd --reload

success

[root@freeit ~]# firewall-cmd --list-all

public (default, active)

   interfaces: eth0

   sources:

   services: dhcpv6-client http

   ports:

   masquerade: no

   forward-ports:

   icmp-blocks:

  rich rules:

        rule family="ipv4" source  address="192.168.10.11" service name="ssh" accept

 

 

3.5.非默认zone区域添加策略

 

经过上个实验我们知道,设置策略在不改变默认zone区域的情况下都是为默认的public这个zone设置的。那么,如果想为其他的zone设置策略应该怎么设置呢?从实验来看,如果想为其他zone设置策略,那么要先设置其他zone为默认zone,然后再为默认zone设置策略。

9:为home设置策略,仅允许主机192.168.10.11访问其http服务

[root@freeit ~]# firewall-cmd  --set-default-zone=home

success

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

Home

//首先设置home为缺省使用的zone

[root@freeit ~]# firewall-cmd  --add-rich-rule 'rule family=ipv4 source address=192.168.10.11 service  name=http accept'  --permanent

success

[root@freeit ~]# firewall-cmd --reload

Success

//home设置策略并加载到当前

 [root@freeit  ~]# firewall-cmd --list-all

home (default, active)

   interfaces: eth0

   sources: 192.168.10.1

   services: dhcpv6-client http ipp-client mdns samba-client ssh

   ports:

   masquerade: no

   forward-ports:

   icmp-blocks:

  rich rules:

        rule family="ipv4" source  address="192.168.10.11" service name="http" accept

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

success

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

Public

//改回缺省的public zone

 

3.6.其它参数

10:查看现在firewall的状态

[root@freeit ~]# firewall-cmd --stat

running

 

11:查看当前有效的zone

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

home

   sources: 192.168.10.1

public

   interfaces: eth0

 

12:检查当前有效的服务

[root@freeit ~]# firewall-cmd  --get-service

amanda-client bacula bacula-client dhcp  dhcpv6 dhcpv6-client dns ftp high-availability http https imaps ipp  ipp-client ipsec kerberos kpasswd ldap ldaps libvirt libvirt-tls mdns mountd  ms-wbt mysql nfs ntp open*** pmcd pmproxy pmwebapi pmwebapis pop3s postgresql  proxy-dhcp radius rpc-bind samba samba-client smtp ssh telnet tftp tftp-client  transmission-client vnc-server wbem-https