前言:在Internet中,企業通過架設各種應用系統來為使用者提供各種網絡服務,如Web網站、電子郵件系統、FTP伺服器、資料庫系統等。為了能夠保護這些伺服器,過濾企業不需要的通路甚至是惡意的入侵,我們便需要防火牆的功能。
文章目錄
- 一、Firewalld概述
-
- 1.Firewalld簡介
- 二、Firewalld和iptables的關系
-
- 1.什麼是iptables
-
-
- 1.1 Firewalld和iptables的差別
-
- 2. 四表五鍊
-
-
- 2.1 具體的四表
- 2.2 五鍊
- 2.3 四表五鍊之間的關系
-
- 3.iptables文法格式
- 三、Firewalld網絡區域
-
- 1. 區域介紹
- 2. Firewalld資料處理流程
- 四、Firewalld防火牆的配置方法
-
- 1. 運作時配置
- 2. 永久配置
- 五、Firewall-config圖形工具
-
-
-
- 5.1 /etc/firewalld中的配置檔案
- 5.2 運作時配置/永久配置
- 5.3 Firewall-cmd指令行工具
- 5.4 重新加載防火牆
- 5.5 關聯網卡到指定區域
- 5.6 修改預設區域
- 5.7 連接配接狀态
-
-
- ‘區域’頁籤
- ‘服務’頁籤
-
-
-
- 六、Firewalld防火牆維護指令
-
-
-
- 6.1 防火牆程序操作
- 6.2 防火牆管理操作
- 6.3 查詢Firewalld狀态
- 6.4 重新加載Firewall的配置
-
-
- 七、查詢預定義資訊指令
-
-
-
- 7.1 擷取預定義資訊
- 7.2 顯示網絡連接配接或接口的預設區域
- 7.3 設定網絡連接配接或接口的預設區域為internal
- 7.4 顯示已激活的所有區域
-
-
- 八、Firewalld區域操作指令
-
-
-
- 8.1 顯示ens33接口綁定的區域
- 8.2 為ens33接口綁定work區域
- 8.3 為work區域更改綁定的網絡接口ens33
- 8.4 為work區域删除綁定的網絡接口ens33
- 8.5 查詢work區域中是否包含接口ens33
- 8.6 顯示區域及其規則
-
-
- 九、Firewalld端口操作指令
-
-
-
- 9.1 顯示internal區域内允許通路的所有服務
- 9.2 為public區域設定允許通路SMTP服務
- 9.3 删除internal區域中的SSH服務
- 9.4 查詢internal區域中是否啟用了SSH服務
-
-
- 十、Firewalld端口操作指令
-
-
-
- 10.1 顯示internal區域内允許通路的所有端口号
- 10.2 啟用internal區域22端口的TCP協定組合
- 10.3 禁用internal區域22端口的TCP協定組合
- 10.4 查詢internal區域中是否啟用了22端口和TCP協定組合
-
-
- 十一、Firewalld阻塞ICMP操作指令
-
-
-
- 11.1 顯示work區域内阻塞的所有ICMP類型
- 11.2 為work區域設定阻塞echo-reply類型的ICMP
- 11.3 删除work區域已阻塞的echo-reply類型的ICMP
- 11.4 查詢work區域的echo-request類型的ICMP是否阻塞
-
-
- 十二、Firewalld防火牆案例
-
-
-
- 12.1 實驗環境
- 12.2 需求描述
- 12.3 綁定區域
- 12.4 配置服務(work區域配置)
- 12.5 配置服務(internal區域配置)
- 12.6 配置端口
- 12.7 配置ICMP阻塞(配置work區域)
- 12.8 配置ICMP阻塞(配置internal區域)
- 12.9 配置ICMP阻塞(配置public區域)
- 12.10 重新加載配置
- 12.11 驗證配置
-
-
一、Firewalld概述
1.Firewalld簡介
1)支援網絡區域所定義的網絡連結以及接口安全等級的動态防火牆管理工具
2)支援IPv4、IPv6防火牆設定以及以太網橋
3)支援服務或應用程式直接添加防火牆規則接口
4)擁有兩種配置配置模式
——運作時配置
——永久配置
二、Firewalld和iptables的關系
1.什麼是iptables
iptables是Linux的防火牆管理工具而已,真正實作防火牆功能的是Netfilter,我們配置了iptables規則後Netfilter通過這些規則來進行防火牆過濾等操作
Netfilter子產品:
它是主要的工作子產品,位于核心中,在網絡層的五個位置(也就是防火牆四表五鍊)注冊了一些函數,用來抓取資料包;把資料包的資訊拿出來比對各個鍊位置在對應表中的規則:比對之後,進行相應的處理ACCEPT、DROP等等。
1.1 Firewalld和iptables的差別

2. 四表五鍊
鍊就是位置:共有五個 進路由(PREROUTING)、進系統(INPUT)、轉發(FORWARD)、出系統(OUTPUT)、出路由(POSTROUTING);
表就是存儲的規則;資料包到了該鍊處,會去對應表中查詢設定的規則。然後決定是否放行、丢棄、轉發還是修改等等操作。
2.1 具體的四表
fileer表——過濾資料包
Nat表——用于網絡位址轉換(ip、端口)
Mangle表——修改資料包的服務類型、TTL、并且可以配置路由實作QOS
Raw表——決定資料包是否被狀态跟蹤機制處理
2.2 五鍊
INPUT鍊——進來的資料包應用此規則鍊中的政策
OUTPUT鍊——外出的資料包應用此規則鍊中的政策
FORWARD鍊——轉發資料包時應用此規則鍊中的政策
PREROUTING鍊——對資料包作路由選擇前應用此鍊中的規則(所有的資料包進來的時候都先由這個鍊處理)
POSTROUTING鍊——對資料包作路由選擇後應用此鍊中的規則(所有的資料包出來的時候都先由這個鍊處理)
2.3 四表五鍊之間的關系
3.iptables文法格式
iptables常用參數:
-p 設定預設政策:iptables
-p INPUT(DROP|ACCEPT)
-L 清空規則鍊
-A 在規則鍊的末尾加入新規則
-I num在規則鍊的頭部加入新規則
-D num删除某一條規則
-S 比對來源位址IP/MASK,加歎号“!”表示除這個IP外
-d 比對目标位址
-i 網卡名稱 比對從這塊網卡流入的出局
-o 網卡名稱 比對從這塊網卡流出的資料
-p 比對協定,如tcp,udp,icmp
--dport num 比對目标端口号
--sport num 比對來源端口号
三、Firewalld網絡區域
1. 區域介紹
——區域如同進入主機的安全門,每個區域都具有不同限制程度的規則
——可以使用一個或多個區域,但是任何一個活躍區域至少需要關聯源位址或接口
——預設情況下,public區域是預設區域,包含所有接口(網卡 )
2. Firewalld資料處理流程
——檢查資料來源的源位址
········若源位址關聯到特定的區域,則執行該區域所指定的規則
······· 若源位址未關聯到特定的區域,則使用傳入網絡接口的區域并執行該區域所指定的規則
······· 若網絡接口未關聯到特定的區域,則使用預設區域并執行該區域所指定的規則
四、Firewalld防火牆的配置方法
1. 運作時配置
——實時生效,并持續至Firewalld重新啟動或重新加載配置
——不中斷現有連接配接
——不能修改服務配置
2. 永久配置
——不立即生效,除非Firewalld重新啟動或重新加載配置
——中斷現有連接配接
——可以修改服務配置
五、Firewall-config圖形工具
5.1 /etc/firewalld中的配置檔案
——Firewalld會優先使用/etc/firewalld中的配置,如果不存在配置檔案,則使用/etc/firewalld/中的配置
——/etc/firewalld:使用者自定義配置檔案,需要時可通過/usr/lib/firewalld中拷貝
——/usr/lib/firewalld/:預設配置檔案,不建議修改,若恢複至預設配置,可直接删除/etc/firewalld中的配置
5.2 運作時配置/永久配置
5.3 Firewall-cmd指令行工具
5.4 重新加載防火牆
——更改永久配置并生效
5.5 關聯網卡到指定區域
5.6 修改預設區域
5.7 連接配接狀态
‘區域’頁籤
——‘服務’子頁籤
——‘端口’子頁籤
——‘協定’子頁籤
——‘源端口’子頁籤
——‘僞裝’子頁籤
——‘端口轉發’子頁籤
——‘ICMP過濾器’子頁籤
‘服務’頁籤
——‘子產品’子頁籤
——‘目标位址’子頁籤
六、Firewalld防火牆維護指令
6.1 防火牆程序操作
選項:start stop restart status
6.2 防火牆管理操作
—firewall-cmd指令
—— 支援全部防火牆特性
—— 對于狀态和查詢模式,指令隻傳回狀态,沒有其他輸出
—— --permanent參數:攜帶該參數表示永久配置,否則表示運作時配置對指定區域操作
6.3 查詢Firewalld狀态
[[email protected] ~]# 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 一 2019-12-09 19:29:38 CST; 3h 46min ago
Docs: man:firewalld(1)
Main PID: 780 (firewalld)
CGroup: /system.slice/firewalld.service
└─780 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid
12月 09 19:29:35 localhost.localdomain systemd[1]: Starting firewalld - dynamic fi....
12月 09 19:29:38 localhost.localdomain systemd[1]: Started firewalld - dynamic fir....
12月 09 19:29:41 localhost.localdomain firewalld[780]: WARNING: ICMP type 'beyond-....
12月 09 19:29:41 localhost.localdomain firewalld[780]: WARNING: beyond-scope: INVA....
12月 09 19:29:41 localhost.localdomain firewalld[780]: WARNING: ICMP type 'failed-....
12月 09 19:29:41 localhost.localdomain firewalld[780]: WARNING: failed-policy: INV....
12月 09 19:29:41 localhost.localdomain firewalld[780]: WARNING: ICMP type 'reject-....
12月 09 19:29:41 localhost.localdomain firewalld[780]: WARNING: reject-route: INVA....
Hint: Some lines were ellipsized, use -l to show in full.
[[email protected] ~]# firewall-cmd --state
running
6.4 重新加載Firewall的配置
—— firewall-cmd -reload
[[email protected] ~]# firewall-cmd --reload
success
—— firewall-cmd --complete -reload
狀态資訊将會丢失,多用于處理防火牆出現問題時
[[email protected] ~]# firewall-cmd --complete-reload
success
systemctl restart firewalld
[[email protected] ~]# systemctl restart firewalld
七、查詢預定義資訊指令
7.1 擷取預定義資訊
[[email protected] ~]# firewall-cmd --get-zones //檢視預定義的區域
block dmz drop external home internal public trusted work
[[email protected] ~]# firewall-cmd --get-services //檢視預定義的服務
RH-Satellite-6 amanda-client amanda-k5-client bacula bacula-client bitcoin bitcoin-rpc bitcoin-testnet bitcoin-testnet-
rpc ceph ceph-mon cfengine condor-collector ctdb dhcp dhcpv6 dhcpv6-client dns docker-registry dropbox-lansync
elasticsearch freeipa-ldap freeipa-ldaps freeipa-replication freeipa-trust ftp ganglia-client ganglia-master high-
availability http https imap imaps ipp ipp-client ipsec iscsi-target kadmin kerberos kibana klogin kpasswd kshell ldap
ldaps libvirt libvirt-tls managesieve mdns mosh mountd ms-wbt mssql mysql nfs nrpe ntp openvpn ovirt-imageio ovirt-
storageconsole ovirt-vmconsole pmcd pmproxy pmwebapi pmwebapis pop3 pop3s postgresql privoxy proxy-dhcp ptp pulseaudio
puppetmaster quassel radius rpc-bind rsh rsyncd samba samba-client sane sip sips smtp smtp-submission smtps snmp snmptrap
spideroak-lansync squid ssh synergy syslog syslog-tls telnet tftp tftp-client tinc tor-socks transmission-client vdsm
vnc-server wbem-https xmpp-bosh xmpp-client xmpp-local xmpp-server
[[email protected] ~]# firewall-cmd --get-icmptypes //檢視預定義的ICMP類型
address-unreachable bad-header communication-prohibited destination-unreachable echo-reply echo-request fragmentation-
needed host-precedence-violation host-prohibited host-redirect host-unknown host-unreachable ip-header-bad neighbour-
advertisement neighbour-solicitation network-prohibited network-redirect network-unknown network-unreachable no-route
packet-too-big parameter-problem port-unreachable precedence-cutoff protocol-unreachable redirect required-option-missing
router-advertisement router-solicitation source-quench source-route-failed time-exceeded timestamp-reply timestamp-
request tos-host-redirect tos-host-unreachable tos-network-redirect tos-network-unreachable ttl-zero-during-reassembly
ttl-zero-during-transit unknown-header-type unknown-option
7.2 顯示網絡連接配接或接口的預設區域
[[email protected] ~]# firewall-cmd --get-default-zone
public
[[email protected]
7.3 設定網絡連接配接或接口的預設區域為internal
[[email protected] ~]# firewall-cmd --set-default-zone=internal
success
7.4 顯示已激活的所有區域
[[email protected] ~]# firewall-cmd --get-active-zones
internal
interfaces: ens33
激活的條件:區域至少關聯一個接口或一個源位址/網段
八、Firewalld區域操作指令
8.1 顯示ens33接口綁定的區域
[[email protected] ~]# firewall-cmd --get-zone-of-interface=ens33
internal
8.2 為ens33接口綁定work區域
[[email protected] ~]# firewall-cmd --zone=work --add-interface=ens33
The interface is under control of NetworkManager, setting zone to 'work'.
success
8.3 為work區域更改綁定的網絡接口ens33
[[email protected] ~]# firewall-cmd --zone=work --change-interface=ens33
The interface is under control of NetworkManager and already bound to 'work'
The interface is under control of NetworkManager, setting zone to 'work'.
success
8.4 為work區域删除綁定的網絡接口ens33
[[email protected] ~]# firewall-cmd --zone=work --remove-interface=ens33
The interface is under control of NetworkManager, setting zone to default.
success
8.5 查詢work區域中是否包含接口ens33
[[email protected] ~]# firewall-cmd --zone=work --query-interface=ens33
no
8.6 顯示區域及其規則
——顯示所有區域及其規則
[[email protected] ~]# firewall-cmd --list-all-zones
block
target: %%REJECT%%
icmp-block-inversion: no
interfaces:
sources:
services:
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
dmz
target: default
icmp-block-inversion: no
interfaces:
sources:
services: ssh
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
drop
target: DROP
icmp-block-inversion: no
interfaces:
sources:
services:
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
external
target: default
icmp-block-inversion: no
interfaces:
sources:
services: ssh
ports:
protocols:
masquerade: yes
forward-ports:
source-ports:
icmp-blocks:
rich rules:
home
target: default
icmp-block-inversion: no
interfaces:
sources:
services: ssh mdns samba-client dhcpv6-client
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
internal (active)
target: default
icmp-block-inversion: no
interfaces: ens33
sources:
services: ssh mdns samba-client dhcpv6-client
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
public
target: default
icmp-block-inversion: no
interfaces:
sources:
services: ssh dhcpv6-client
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
trusted
target: ACCEPT
icmp-block-inversion: no
interfaces:
sources:
services:
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
work
target: default
icmp-block-inversion: no
interfaces:
sources:
services: ssh dhcpv6-client
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
—— 顯示internal區域的所有規則
[[email protected] ~]# firewall-cmd --zone=internal --list-all
internal (active)
target: default
icmp-block-inversion: no
interfaces: ens33
sources:
services: ssh mdns samba-client dhcpv6-client
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
——顯示預設區域的所有規則
[[email protected] ~]# firewall-cmd --list-all
internal (active)
target: default
icmp-block-inversion: no
interfaces: ens33
sources:
services: ssh mdns samba-client dhcpv6-client
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
九、Firewalld端口操作指令
9.1 顯示internal區域内允許通路的所有服務
[[email protected] ~]# firewall-cmd --zone=internal --list-services
ssh mdns samba-client dhcpv6-client
9.2 為public區域設定允許通路SMTP服務
[[email protected] ~]# firewall-cmd --zone=public --add-service=smtp
success
9.3 删除internal區域中的SSH服務
[[email protected] ~]# firewall-cmd --zone=internal --remove-service=ssh
success
9.4 查詢internal區域中是否啟用了SSH服務
[[email protected] ~]# firewall-cmd --zone=internal --query-service=ssh
no
十、Firewalld端口操作指令
10.1 顯示internal區域内允許通路的所有端口号
[[email protected] ~]# firewall-cmd --zone=internal --list-ports
10.2 啟用internal區域22端口的TCP協定組合
[[email protected] ~]# firewall-cmd --zone=internal --add-port=22/tcp --timeout=5m
success
–timeout=5m:表示5分鐘後删除該端口,多用于測試目的
10.3 禁用internal區域22端口的TCP協定組合
[[email protected] ~]# firewall-cmd --zone=internal --remove-port=22/tcp
Warning: NOT_ENABLED: '22:tcp' not in 'internal'
success
10.4 查詢internal區域中是否啟用了22端口和TCP協定組合
[[email protected] ~]# firewall-cmd --zone=internal --query-port=22/tcp
no
十一、Firewalld阻塞ICMP操作指令
11.1 顯示work區域内阻塞的所有ICMP類型
[[email protected] ~]# firewall-cmd --zone=work --list-icmp-blocks
11.2 為work區域設定阻塞echo-reply類型的ICMP
[[email protected] ~]# firewall-cmd --zone=work --add-icmp-block=echo-reply
success
11.3 删除work區域已阻塞的echo-reply類型的ICMP
[[email protected] ~]# firewall-cmd --zone=work --remove-icmp-block=echo-reply
success
11.4 查詢work區域的echo-request類型的ICMP是否阻塞
[[email protected] ~]# firewall-cmd --zone=work --query-icmp-block=echo-reply
no
十二、Firewalld防火牆案例
12.1 實驗環境
12.2 需求描述
——禁止主機ping伺服器
——隻允許192.168.9.130主機通路Apache服務
——隻允許192.168.9.132主機通路TCP/22端口
12.3 綁定區域
[[email protected] ~]# firewall-cmd --get-active-zone
public //預設情況下,隻有public區域處于激活狀态
interfaces: ens33
[[email protected] ~]# firewall-cmd --permanent --zone=work --add-source=192.168.9.130
success //将來源位址192.168.8.130和work區域關聯
[[email protected] ~]# firewall-cmd --permanent --zone=internal --add-source=192.168.9.132
success
[[email protected] ~]# firewall-cmd --reload
success //永久配置需要重新加載才能生效
[[email protected] ~]# firewall-cmd --get-active-zone
work
sources: 192.168.9.130
internal
sources: 192.168.9.132
public
interfaces: ens33
12.4 配置服務(work區域配置)
work區域配置:
[[email protected] ~]# firewall-cmd --zone=work --list-services
ssh dhcpv6-client
[[email protected] ~]# firewall-cmd --permanent --zone=work --remove-service=ssh
success
[[email protected] ~]# firewall-cmd --permanent --zone=work --remove-service=dhcpv6-client
success
[[email protected] ~]# firewall-cmd --permanent --zone=work --add-service=http
success
[[email protected] ~]# firewall-cmd --permanent --zone=work --list-services
http
12.5 配置服務(internal區域配置)
[[email protected] ~]# firewall-cmd --zone=internal --list-services
ssh mdns samba-client dhcpv6-client
[[email protected] ~]# firewall-cmd --permanent --zone=internal --remove-service=mdns
success
[[email protected] ~]# firewall-cmd --permanent --zone=internal --remove-service=dhcpv6-client
success
[[email protected] ~]# firewall-cmd --permanent --zone=internal --remove-service=ssh
success
[[email protected] ~]# firewall-cmd --permanent --zone=internal --remove-service=samba-client
success
[[email protected] ~]# firewall-cmd --permanent --zone=internal --list-services
12.6 配置端口
[[email protected] ~]# firewall-cmd --permanent --zone=internal --list-ports
[[email protected] ~]# firewall-cmd --permanent --zone=internal --add-port=22/tcp
success
[[email protected] ~]# firewall-cmd --permanent --zone=internal --list-ports
22/tcp
12.7 配置ICMP阻塞(配置work區域)
[[email protected] ~]# firewall-cmd --permanent --zone=work --list-icmp-blocks
[[email protected] ~]# firewall-cmd --permanent --zone=work --add-icmp-block=echo-request
success
[[email protected] ~]# firewall-cmd --permanent --zone=work --list-icmp-blocks
echo-request
12.8 配置ICMP阻塞(配置internal區域)
[[email protected] ~]# firewall-cmd --permanent --zone=internal --list-icmp-blocks
[[email protected] ~]# firewall-cmd --permanent --zone=internal --add-icmp-block=echo-request
success
[[email protected] ~]# firewall-cmd --permanent --zone=internal --list-icmp-blocks
echo-request
12.9 配置ICMP阻塞(配置public區域)
[[email protected] ~]# firewall-cmd --permanent --zone=public --list-icmp-blocks
[[email protected] ~]# firewall-cmd --permanent --zone=public --add-icmp-block=echo-request
success
[[email protected] ~]# firewall-cmd --permanent --zone=public --list-icmp-blocks
echo-request
12.10 重新加載配置
[[email protected] ~]# firewall-cmd --reload
success
12.11 驗證配置
隻允許192.168.9.130主機通路Apache服務
禁止主機ping伺服器
//192.168.9.131的客戶機
//192.168.9.131的客戶機
隻允許192.168.9.132主機通路TCP/22端口
[[email protected] ~]# ssh 192.168.9.130 '//IP位址為192.168.9.131的主機'
ssh: connect to host 192.168.9.130 port 22: No route to host
[[email protected] ~]# ssh 192.168.9.130 '//IP位址為192.168.9.132的主機'
The authenticity of host '192.168.9.130(192.168.9.130)' can't be established.
ECDSA key fingerprint is SHA256:UhiQeQeIoKaH1ogewTdbaZIldXmr3dxKoD0/RN0jhcU.
ECDSA key fingerprint is MD5:95:ac:b8:fe:9e:01:50:3c:56:c9:e3:aa:28:ee:1c:24.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.9.130' (ECDSA) to the list of known hosts.
[email protected] password:
Last failed login: Mon Dec 9 17:40:09 CST 2019 from 192.168.9.130 on ssh:notty
There was 1 failed login attempt since the last successful login.
Last login: Mon Dec 9 16:39:50 2019
[[email protected] ~]# exit
logout
Connection to 192.168.9.130 closed.