Linux Firewalld 基礎介紹

Linux Firewalld 基礎介紹

互聯網上提供了各種網絡服務,而防火牆可以設置各種規則來限制訪問,保護服務器。


概述

Linux的防火牆體系主要工作在網絡層,針對TCP/IP數據包實施過濾和限制,屬於典型的包過濾防火牆。

Linux系統的防火牆體系基於內核編碼實現,具有非常穩定的性能和極高的效率。

三種防火牆

netfilter

  • 指linux內核中實現包過濾防火牆的內部結構
  • 屬於內核態的防火牆功能體系

iptables

  • 指管理linux防火牆的命令程序
  • 屬於用戶態的防火牆管理體系

Firewalld

  • CentOS 7默認的防火牆管理工具,取代之前的iptables防火牆
  • 屬於用戶態
  • firewalld和iptables內部結構都指向netfilter這個強大的網絡過濾子系統,以實現包過濾防火牆功能
  • 支持動態更新、加入防火牆zone概念
  • 支持IPv4和IPv6地址
  • 字符管理工具firewall-cmd和圖形化管理工具firewall-config

區別

Linux Firewalld 基礎介紹

網絡區域

區域介紹

Linux Firewalld 基礎介紹

  • 每個區域都具有不同限制程度的規則
  • 可以使用一個或多個區域,但是任何一個活躍區域至少需要關聯源地址或接口
  • 默認情況下,public區域是默認區域,包含所有接口(網卡)

數據處理流程

檢查數據來源的源地址

  • 若源地址關聯到特定的區域,則執行該區域所指定的規則
  • 若源地址未關聯到特定的區域,則使用傳入網絡接口的區域並執行該區域所指定的規則
  • 若網絡接口未關聯到特定的區域,則使用默認區域並執行該區域所指定的規則

配置方法

運行時配置

  • 實時生效,並持續至Firewalld重新啟動或重新加載配置
  • 不中斷現有連接
  • 不能修改服務配置

永久配置

  • 不立即生效,除非Firewalld重新啟動或重新加載配置
  • 中斷現有連接
  • 可以修改服務配置

配置文件

Firewalld會優先使用/etc/firewalld/中的配置,如果不存在配置文件,則使用/usr/ib/firewalld/中的配置

  • /etc/firewalld/:用戶自定義配置文件,需要時可通過從/usr/ib/firewalld/中拷貝
  • /usr/ib/firewalld/:默認配置文件,不建議修改,若恢復至默認配置,可直接刪除/etc/firewalld/中的配置

圖形工具

[root@localhost ~]# firewall-config

命令行工具

[root@localhost ~]# firewall-cmd 命令
  • 以下是整理的一些命令
--get-default-zone
顯示網絡連接或接口的默認區域
--set-default-zone=<zone>
設置網絡連接或接口的默認區域
--get-active-zones
顯示已激活的所有區域
--get-zone-of-interface=<interface>
顯示指定接口綁定的區域
--zone=<zone> --add-interface=<interface>
為指定接口綁定區域
--zone=<zone> --change-interface=<interface>
為指定的區域更改綁定的網絡接口
--zone=<zone> --remove-interface=<interface>
為指定的區域刪除綁定的網絡接口
--query-interface=<interface>
查詢區域中是否包含某接口
--list-all-zones
顯示所有區域及其規則
[--zone=<zone>] --list-all
顯示所有指定區域的所有規則
Copy
[--zone=<zone>] --list-services
顯示指定區域內允許訪問的所有服務
[--zone=<zone>] --add-service=<service>
為指定區域設置允許訪問的某項服務
[--zone=<zone>] --remove-service=<service>
刪除指定區域已設置的允許訪問的某項服務
[--zone=<zone>] --query-service=<service>
查詢指定區域中是否啟用了某項服務

Copy
[--zone=<zone>] --list-ports
顯示指定區域內允許訪問的所有端口號
[--zone=<zone>] --add-port=<port>[-<port>]/<protocol> [--timeout=<seconds>]
啟用區域端口和協議組合,可選配置超時時間
[--zone=<zone>] --remove-port=<port>[-<port>]/<protocol>
禁用區域端口和協議組合
[--zone=<zone>] --query-port=<port>[-<port>]/<protocol>
查詢區域中是否啟用了端口和協議組合
Copy
[--zone=<zone>] --list-icmp-blocks
顯示指定區域內阻塞的所有ICMP類型
[--zone=<zone>] --add-icmp-block=<icmptype>
為指定區域設置阻塞的某項ICMP類型
[--zone=<zone>] --remove-icmp-block=<icmptype>
刪除指定區域已阻塞的某項ICMP類型
[--zone=<zone>] --query-icmp-block=<icmptype>
查詢指定區域的ICMP阻塞功能
/<icmptype>/<zone>/<icmptype>/<zone>/<icmptype>/<zone>/<zone>/<protocol>/<port>/<port>/<zone>/<protocol>/<port>/<port>/<zone>/<seconds>/<protocol>/<port>/<port>/<zone>/<zone>/<service>/<zone>/<service>/<zone>/<service>/<zone>/<zone>/<zone>/<interface>/<interface>/<zone>/<interface>/<zone>/<interface>/<zone>/<interface>/<zone>

firewall-cmd

狀態操作

  • 停止、啟動
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# systemctl start firewalld
  • 禁止/允許開機啟動
[root@localhost ~]# systemctl disable firewalld
[root@localhost ~]# systemctl enable firewalld
  • 查看狀態
[root@localhost ~]# systemctl status firewalld
[root@localhost ~]# firewall-cmd --state
running

獲取預定義信息

預定義信息主要包括三種:可用的區域、可用的服務以及可用的ICMP阻塞類型

  • 顯示預定義的區域
[root@localhost ~]# firewall-cmd --get-zones 
block dmz drop external home internal public trusted work
  • 顯示預定義的服務
--get-default-zone
顯示網絡連接或接口的默認區域
--set-default-zone=<zone>
設置網絡連接或接口的默認區域
--get-active-zones
顯示已激活的所有區域
--get-zone-of-interface=<interface>
顯示指定接口綁定的區域

--zone=<zone> --add-interface=<interface>
為指定接口綁定區域
--zone=<zone> --change-interface=<interface>
為指定的區域更改綁定的網絡接口
--zone=<zone> --remove-interface=<interface>
為指定的區域刪除綁定的網絡接口
--query-interface=<interface>
查詢區域中是否包含某接口
--list-all-zones
顯示所有區域及其規則
[--zone=<zone>] --list-all
顯示所有指定區域的所有規則
/<zone>/<interface>/<interface>/<zone>/<interface>/<zone>/<interface>/<zone>/<interface>/<zone>
  • 顯示預定義的icmp阻塞類型
Copy
[root@localhost ~]# firewall-cmd --get-icmptypes
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
  • 各種阻塞類型的含義分別如下所示:

destination-unreachable:目的地址不可達。

echo-reply:應答回應(pong)。

parameter-problem:參數問題。

redirect:重新定向。

router-advertisement:路由器通告。

router-solicitation:路由器徵尋。

source-quench:源端抑制。

time-exceeded:超時。

timestamp-reply:時間戳應答回應。

timestamp-request:時間戳請求。

區域管理#

使用firewall-cmd命令可以實現獲取和管理區域,為指定區域綁定網絡接口等功能。

--get-default-zone
顯示網絡連接或接口的默認區域
--set-default-zone=<zone>
設置網絡連接或接口的默認區域
--get-active-zones
顯示已激活的所有區域
--get-zone-of-interface=<interface>
顯示指定接口綁定的區域
--zone=<zone> --add-interface=<interface>
為指定接口綁定區域
--zone=<zone> --change-interface=<interface>
為指定的區域更改綁定的網絡接口
--zone=<zone> --remove-interface=<interface>
為指定的區域刪除綁定的網絡接口
--list-all-zones
顯示所有區域及其規則
[--zone=<zone>] --list-all
顯示所有指定區域的所有規則,省略--zone=<zone>時表示僅對默認區域操作

/<zone>/<zone>/<interface>/<zone>/<interface>/<zone>/<interface>/<zone>/<interface>/<zone>
  • 顯示當前系統中的默認區域
[root@localhost ~]# firewall-cmd --get-default-zone 
public
  • 顯示默認區域的所有規則
[root@localhost ~]# firewall-cmd --list-all
public (active)
target: default
icmp-block-inversion: no
interfaces: ens33
sources:
services: ssh dhcpv6-client
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
  • 顯示網絡接口ens33對應的區域
[root@localhost ~]# firewall-cmd --get-zone-of-interface=ens33 
public
  • 將網絡接口ens33對應區域改為internal區域
[root@localhost ~]# firewall-cmd --zone=internal --change-interface=ens33 
The interface is under control of NetworkManager, setting zone to 'internal'.
success
[root@localhost ~]# firewall-cmd --get-zone-of-interface=ens33

internal
  • 顯示internal區域下有哪些網絡接口
[root@localhost ~]# firewall-cmd --zone=internal --list-interfaces 
ens33
  • 顯示所有已激活的區域
[root@localhost ~]# firewall-cmd --get-active-zones 
internal
interfaces: ens33

服務管理#

為了方便管理,firewalld預先定義了很多服務,存放在/usr/lib/firewalld/services/目錄中,服務通過單個的XML配置文件來指定。

這些配置文件則按以下格式命名:service-name.xml,每個文件對應一項具體的網絡服務,如ssh服務等。

與之對應的配置文件中記錄了各項服務所使用的tcp/udp端口。在最新版本的firewalld中默認已經定義了70多種服務供我們使用,對於每個網絡區域,均可以配置允許訪問的服務。

當默認提供的服務不適用或者需要自定義某項服務的端口時,我們需要將service配置文件放置在/etc/firewalld/services/目錄中。

service配置具有以下優點。

  • 通過服務名字來管理規則更加人性化。
  • 通過服務來組織端口分組的模式更加高效,如果一個服務使用了若干個網絡端口,則服務的配置文件就相當於提供了到這些端口的規則管理的批量操作快捷方式。
[--zone=<zone>] --list-services
顯示指定區域內允許訪問的所有服務
[--zone=<zone>] --add-service=<service>
為指定區域設置允許訪問的某項服務
[--zone=<zone>] --remove-service=<service>
刪除指定區域已設置的允許訪問的某項服務
[--zone=<zone>] --list-ports
顯示指定區域內允許訪問的所有端口號
[--zone=<zone>] --add-port=<portid>[-<portid>]/<protocol>
為指定區域設置允許訪問的某個/某段端口號(包括協議名)
[--zone=<zone>] --remove-port=<portid>[-<portid>]/<protocol>
刪除指定區域已設置的允許訪問的端口號(包括協議名)
[--zone=<zone>] --list-icmp-blocks
顯示指定區域內拒絕訪問的所有 ICMP 類型
[--zone=<zone>] --add-icmp-block=<icmptype>
為指定區域設置拒絕訪問的某項 ICMP 類型
[--zone=<zone>] --remove-icmp-block=<icmptype>

刪除指定區域已設置的拒絕訪問的某項 ICMP 類型,省略--zone=<zone>時表示對默認區域操作
/<zone>/<icmptype>/<zone>/<icmptype>/<zone>/<zone>/<protocol>/<portid>/<portid>/<zone>/<protocol>/<portid>/<portid>/<zone>/<zone>/<service>/<zone>/<service>/<zone>/<zone>
  • 顯示默認區域允許訪問的所有服務
[root@localhost ~]# firewall-cmd --list-services 
ssh dhcpv6-client
  • 設置默認區域允許訪問http服務
[root@localhost ~]# firewall-cmd --add-service=http 
success
  • 設置默認區域允許訪問https服務
[root@localhost ~]# firewall-cmd --add-service=https 
success
  • 顯示默認區域允許訪問的所有服務
[root@localhost ~]# firewall-cmd --list-services 
ssh dhcpv6-client http https
  • 顯示internal區域內允許訪問的所有服務
[root@localhost ~]# firewall-cmd --zone=internal --list-services  

ssh mdns samba-client dhcpv6-client
  • 設置internal區域允許訪問mysql服務
[root@localhost ~]# firewall-cmd --zone=internal --add-service=mysql 
success
  • 設置internal區域不允許訪問samba-client服務
[root@localhost ~]# firewall-cmd --zone=internal --remove-service=samba-client 
success
  • 顯示internal區域內允許訪問的所有服務
[root@localhost ~]# firewall-cmd --zone=internal --list-services 
ssh mdns dhcpv6-client mysql

端口管理#

在進行服務配置時,預定義的網絡服務可以使用服務名配置,服務所涉及的端口就會自動打開。

但是,對於非預定義的服務只能手動為指定的區域添加端口。

  • 在internal區域打開443/TCP端口
[root@localhost ~]# firewall-cmd --zone=internal --add-port=443/tcp
success
  • 在internal區域禁止443/TCP端口訪問
[root@localhost ~]# firewall-cmd --zone=internal --remove-port=443/tcp
success

兩種配置模式#

前面提到firewall-cmd命令工具有兩種配置模式:

  • 運行時模式Runtime mode表示當前內存中運行的防火牆配置,在系統或firewalld服務重啟、停止時配置將失效。
  • 永久模式Permanent mode表示重啟防火牆或重新加載防火牆時的規則配置,是永久存儲在配置文件中的。

firewall-cmd命令工具與配置模式相關的選項有三個。

  • --reload:重新加載防火牆規則並保持狀態信息,即將永久配置應用為運行時配置。
  • --permanent:帶有此選項的命令用於設置永久性規則,這些規則只有在重新啟動firewalld或重新加載防火牆規則時才會生效;若不帶有此選項,表示用於設置運行時規則。
  • --runtime-to-permanent:將當前的運行時配置寫入規則配置文件中,使之成為永久性。
[root@localhost ~]# firewall-cmd --runtime-to-permanent
success

轉載自:https://www.cnblogs.com/llife/p/11695912.html

"


分享到:


相關文章: