Linux的防火牆體系主要工做在網絡層,針對TCP/IP數據包實時過濾和限制,屬於典型的包過濾防火牆。mysql
Linux防火牆是如何檢查數據流量的? sql
對於進入系統的數據包,首先檢查的就是其源地址:網絡
- 若源地址關聯到特定的區域,則執行該區域所制定的規則;
- 若源地址未關聯到特定的區域,則使用傳入網絡接口的區域並執行該區域所制定的規則。
- 若網絡接口未關聯到特定的區域,那麼就使用默認區域並執行該區域所制定的規則。
默認區域不是單獨的區域,而是指向系統上定義的某個其餘區域。默認狀況下,默認區域是public,可是也能夠更改默認區域。以上匹配規則,按照前後順序,第一個匹配的規則勝出,和網絡設備的ACL匹配規則差很少,俗稱匹配即停。ssh
firewalld的相關預約義區域說明以下:tcp
firewalld防火牆有兩種配置模式:ide
1 、運行時模式:表示當前內存中運行的防火牆配置,在系統或firewalld服務重啓、中止時將失效;工具
二、永久模式:表示重啓防火牆或從新加載防火牆時的規則配置,是永久存儲在配置文件中的。ui
firewall-cmd命令工具與配置模式相關的選項有三個:code
一、防火牆相關命令使用:router
[root@localhost ~]# systemctl start firewalld #啓動防火牆 [root@localhost ~]# systemctl enable firewalld #設置開機自啓動 [root@localhost /]# firewall-cmd --reload #重載防火牆 [root@localhost ~]# systemctl status firewalld #查看防火牆狀態 [root@localhost ~]# systemctl stop firewalld #中止防火牆 [root@localhost ~]# systemctl disable firewalld #設置防火牆開機不自啓動 [root@localhost ~]# firewall-cmd --get-zones #顯示預約義的區域 block dmz drop external home internal public trusted work [root@localhost ~]# firewall-cmd --get-service #顯示預約義的服務 [root@localhost /]# firewall-cmd --list-all --zone=dmz #查看指定區域的配置 dmz target: default icmp-block-inversion: no interfaces: sources: services: ssh ports: protocols: masquerade: no forward-ports: source-ports: icmp-blocks: rich rules: [root@localhost ~]# 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
firewall-cmd --get-icmptypes命令執行結果中部分阻塞類型的含義以下:
destination-unreachable:目的地址不可達;
echo-reply:應答迴應;
parameter-problem:參數問題;
redirect:從新定向;
router-advertisement:路由器通告;
router-solicitation:路由器徵尋;
source-quench:源端抑制;
time-exceeded:超時;
timestamp-reply:時間戳應答迴應;
timestamp-request:時間戳請求;
二、firewalld防火牆區域管理命令及示例:
[root@localhost /]# firewall-cmd --set-default-zone=dmz #設置默認區域爲DMZ區域 [root@localhost ~]# firewall-cmd --get-default-zone #顯示當前系統中的默認區域 [root@localhost ~]# firewall-cmd --list-all #顯示默認區域的全部規則 [root@localhost ~]# firewall-cmd --get-zone-of-interface=ens33 #顯示網絡接口ens33對應區域 [root@localhost ~]# firewall-cmd --get-active-zones #顯示全部激活區域 internal interfaces: ens33 #執行如下操做可將網絡接口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 --zone=internal --list-interfaces #查看internal區域的接口列表 ens33 [root@localhost ~]# firewall-cmd --get-zone-of-interface=ens33 #查看接口ens33對應的區域 internal
三、防火牆服務管理相關命令及示例:
部分服務管理的示例:
①、爲默認區域設置容許訪問的服務
[root@localhost services]# firewall-cmd --list-services #顯示默認區域內容許訪問的全部服務 ssh dhcpv6-client [root@localhost services]# firewall-cmd --add-service=http #設置默認區域容許訪問http服務 success [root@localhost services]# firewall-cmd --add-service=https #設置默認區域容許訪問https服務 success [root@localhost services]# firewall-cmd --list-services #顯示默認區域內容許訪問的全部服務 ssh dhcpv6-client http https
②、爲internal區域設置容許訪問的服務:
[root@localhost services]# firewall-cmd --zone=internal --add-service=mysql #設置internal區域容許訪問mysql服務 success [root@localhost services]# firewall-cmd --zone=internal --remove-service=samba-client #設置internal區域不容許訪問Samba-client服務 success [root@localhost services]# firewall-cmd --zone=internal --list-services #顯示internal區域內容許訪問的全部服務 ssh mdns dhcpv6-client mysql
③、端口管理:
在進行服務配置時,預約義的網絡服務可使用服務名配置,服務所涉及的端口就會自動打開。可是,對於非預約義的服務只能手動爲指定的區域添加端口。示例以下:
[root@localhost services]# firewall-cmd --zone=internal --add-port=443/tcp #在internal區域打開443/tcp端口 success [root@localhost services]# firewall-cmd --zone=internal --remove-port=443/tcp #在internal區域禁止443/tcp端口訪問 success
以上配置都爲臨時配置,若想將當前配置保存爲永久配置,可使用下面命令:
[root@localhost services]# firewall-cmd --runtime-to-permanent success
直接配置爲永久性規則,須帶--permanent選項,以下:
[root@localhost /]# firewall-cmd --add-icmp-block=echo-request --permanent #禁止ping success [root@localhost /]# firewall-cmd --zone=external --add-icmp-block=echo-request --permanent #配置external區域禁止ping success