Centos7中默認將原來的防火牆iptables升級為了firewalld,firewalld跟iptables比起來至少有兩大好處:
1、firewalld可以動態修改單條規則,而不需要像iptables那樣,在修改了規則后必須得全部刷新才可以生效;
2、firewalld在使用上要比iptables人性化很多,即使不明白“五張表五條鏈”或者對TCP/IP協議也不理解,也可以實現大部分功能。
原理
firewalld自身並不具備防火牆的功能,而是和iptables一樣需要通過內核的netfilter來實現。
l 區域(zone)是一組規則,它們決定了允許哪些流量通過。Firewalld默認內置了 9 個區域(zone),按從最不信任到最受信任的順序為:
drop:最低信任級別。所有傳入的連接都將被丟棄而不會回復,並且只能進行傳出連接。
block:與上述類似,但不是簡單地刪除連接,而是使用icmp-host-prohibitedor和 icmp6-adm-prohibited 消息拒絕傳入的請求。
public:表示不信任的公共網絡。可能會視情況允許選擇的傳入連接。默認情況下,此區域為激活狀態。
external:如果你使用防火牆作為網關,則為外部網絡。將其配置為 NAT 轉發,以便你的內部網絡保持私有但可訪問。
internal:external 區域的另一側,用於網關的內部。這些計算機值得信賴,並且可以使用一些其他服務。
dmz:用於 DMZ (DeMilitarized Zone) 中的計算機(將無法訪問網絡其余部分的隔離計算機),僅允許某些傳入連接。
work:用於工作機。信任網絡中的大多數計算機。可能還允許其他一些服務。
home:家庭環境。通常,這意味着您信任其他大多數計算機,並且將接受其他一些服務。
trusted:信任網絡中的所有計算機。可用選項中最開放的,應謹慎使用。
l Zone包含這些可配置項:
target:默認行為,有四個可選值:default、ACCEPT、%%REJECT%%、DROP。默認為default
ACCEPT 會接受所有流入的符合規則的數據包。
REJECT 除了被明確寫好允許的規則,會拒絕所有流入的數據包, 並給發起連接的機器回復被拒絕的消息。
DROP 除了被明確寫好允許的規則,會拒絕所有流入的數據包, 不會給發起連接的機器回復任何消息。
service:表示一個服務。服務常常需要打開很多端口,甚至還有特定的一些源和目的 IP 甚至一些協議的訪問,如果把這些同時都定義在 zone 中,會造成最后的 zone 配置文件很大,提取成 services 相當於是解耦的概念,有利於后期的維護。
port:端口
interface:接口,可以理解為網卡
source:源地址,可以是ip地址也可以是ip地址段
icmp-block:icmp報文阻塞,可以按照icmp類型進行設置
masquerade:ip地址偽裝,也就是按照源網卡地址進行NAT轉發
forward-port:端口轉發
rule:自定義規則
l 對於一個接受到的請求具體使用哪個zone,firewalld是通過三種方法來判斷的:
1、source,也就是源地址
2、interface,接收請求的網卡
3、firewalld.conf中配置的默認zone
這三個的優先級按順序依次降低,也就是說如果按照source可以找到就不會再按interface去查找,如果前兩個都找不到才會使用第三個。
需要注意:同一個源地址只能對於一個zone。一個網絡連接只能被一個 zone 處理,但一個 zone 可以用於多個網絡連接。
安裝
$ sudo yum install firewalld
$ sudo systemctl start firewalld
$ sudo systemctl enable firewalld
$ sudo firewall-cmd --state
配置解釋
- firewalld的配置文件以xml格式為主(主配置文件firewalld.conf例外),他們有兩個存儲位置
1、/etc/firewalld/
2、/usr/lib/firewalld/
使用時的規則是這樣的:當需要一個文件時firewalld會首先到第一個目錄中去查找,如果可以找到,那么就直接使用,否則會繼續到第二個目錄中查找。
- firewalld的配置文件結構非常簡單,主要有兩個文件和三個目錄:
文件:firewalld.conf、lockdown-whitelist.xml
目錄:zones、services、icmptypes
另外,如果使用到direct,還會有一個direct.xml文件。
- firewalld.conf:firewalld的主配置文件,是鍵值對的格式,只有五個配置項
DefaultZone:默認使用的zone,默認值為public;
MinimalMark: 標記的最小值,linux內核會對每個進入的數據包都進行標記;
CleanupOnExit:表示當退出firewalld后是否清除防火牆規則,默認值為yes;
Lockdown: 這個選項跟D-BUS接口操作firewalld有關,firewalld可以讓別的程序通過D-BUS接口直接操作,當Lockdown設置為yes的時候就可以通過lockdown-whitelist.xml文件來限制都有哪些程序可以對其進行操作,而當設置為no的時候就沒有限制了,默認值為 no;
IPv6_rpfilter:其功能類似於rp_filter,只不過是針對ipv6版的,其作用是判斷所接受到的包是否是偽造的,檢查方式主要是通過路由表中的路由條目實現的。默認值為yes。
- lockdown-whitelist.xml:當Lockdown為yes的時候用來限制可以通過D-BUS接口操作firewalld的程序
- direct.xml:通過這個文件可以直接使用防火牆的過濾規則,對從原來的iptables到firewalld的遷移提供了一條綠色通道
- zones:保存zone配置文件
<?xml version="1.0" encoding="utf-8"?>
<zone target="%%REJECT%%">
<short>Block</short>
<description>Unsolicited incoming network packets are rejected. Incoming packets that are related to outgoing network connections are accepted. Outgoing network connections are allowed.</description>
<service name="ssh"/>
<service name="dhcpv6-client"/>
</zone>
target 表示當發來的數據包,沒有匹配到任何的規則(比如端口號,服務等),采取的默認行為。
short 表示對 zone 的一個簡短描述名字。
description 表示對 zone 的一段詳細描述
- services:保存service配置文件
- icmptypes:保存和icmp類型相關的配置文件
白名單配置操作
配置白名單
- 收集白名單IP地址或網段。逐一加入 trusted 區域:
$ sudo firewall-cmd --permanent --zone=trusted --add-source=173.245.48.0/20
……
$ sudo firewall-cmd --permanent --zone=trusted --add-source=131.0.72.55
- 使 trusted 區域設置生效,使用命令如下:
$ sudo firewall-cmd --reload
- 確認 trusted 區域是否設置正確,使用命令如下:
$ sudo firewall-cmd --zone=trusted --list-all
返回:
trusted (active)
target: ACCEPT
icmp-block-inversion: no
interfaces:
sources: 173.245.48.0/20 …… 131.0.72.0/22
services:
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
切換默認區域從 public 到 drop
- 把常用關鍵服務加入排除列表
$ sudo firewall-cmd --permanent --zone=drop --add-service=ssh
$ sudo firewall-cmd --permanent --zone=drop --add-service=dhcpv6-client
- 將默認區域設置為drop
$ sudo firewall-cmd --set-default-zone=drop
- 再將默認網卡(比如eth0)分配給 drop 區域,使用命令如下:
$ sudo firewall-cmd --permanent --zone=drop --change-interface=eth0
- 使白名單最終生效,使用命令如下:
$ sudo firewall-cmd --reload
常見命令參考
$ sudo firewall-cmd --list-all
- 配置zone
firewall-cmd --set-default-zone=zone
配置默認zone。也可以通過firewalld.conf配置文件的DefaultZone配置項來配置。
firewall-cmd --get-default-zone
獲取默認zone的值。
firewall-cmd --get-active-zones
查看當前所有起作用的zone
firewall-cmd --get-zone-of-interface=interface
firewall-cmd --get-zone-of-source=source[/mask]
根據source或者interface查詢所對應的zone。可以檢查設置是否正確。
- 配置source
firewall-cmd [--permanent] [--zone=zone] --list-sources
用於列出指定zone的所有綁定的source地址
firewall-cmd [--permanent] [--zone=zone] --query-source=source[/mask]
用於查詢指定zone是否跟指定source地址進行了綁定
firewall-cmd [--permanent] [--zone=zone] --add-source=source[/mask]
用於將一個source地址綁定到指定的zone(只可綁定一次,第二次綁定到不同的zone會報錯)
firewall-cmd [--zone=zone] --change-source=source[/mask]
用於改變source地址所綁定的zone,如果原來沒有綁定則進行綁定,這樣就跟--add-source的作用一樣了
firewall-cmd [--permanent] [--zone=zone] --remove-source=source[/mask]
用於刪除source地址跟zone的綁定
--permanent表示是否存儲到配置文件中(如果存儲到配置文件中這不會立即生效)
--zone用於指定所要設置的zone,如果不指定則使用默認zone。
- 配置interface
interface有兩個可以配置的位置:1、zone所對應的xml配置文件2、網卡配置文件(也就是ifcfg-*文件)。
firewall-cmd [--permanent] [--zone=zone] --list-interfaces
firewall-cmd [--permanent] [--zone=zone] --add-interface=interface
firewall-cmd [--zone=zone] --change-interface=interface
firewall-cmd [--permanent] [--zone=zone] --query-interface=interface
firewall-cmd [--permanent] [--zone=zone] --remove-interface=interface
還可以在網卡配置文件中進行配置。比如可以在ifcfg-em1文件中添加下面的配置:
ZONE=public
這行配置就相當於下面的命令
[root@excelib.com ~]# firewall-cmd --zone=public --change-interface=em1
這樣來自em1的連接就會使用public這個zone進行管理(如果source匹配了其他的zone除外)。
- 配置target
firewall-cmd --permanent [--zone=zone] --get-target
firewall-cmd --permanent [--zone=zone] --set-target=target
- 配置service
firewall-cmd [--permanent] [--zone=zone] --list-services
firewall-cmd [--permanent] [--zone=zone] --add-service=service [--timeout=seconds]
--timeout的含義是這樣的:添加一個服務,但是不是一直生效而是生效一段時間,過期之后自動刪除。這個選項非常有用,比如我們想暫時開放一個端口進行一些特殊的操作(比如遠程調試),等處理完成后再關閉,不過有時候我們處理完之后就忘記關閉了,而現在的--timeout選項就可以幫我們很好地解決這個問題。
firewall-cmd [--permanent] [--zone=zone] --remove-service=service
firewall-cmd [--permanent] [--zone=zone] --query-service=service
- Port命令
firewall-cmd [--permanent] [--zone=zone] --list-ports
firewall-cmd [--permanent] [--zone=zone] --add-port=portid[-portid]/protocol [--timeout=seconds]
--timeout的含義是這樣的:添加一個服務,但是不是一直生效而是生效一段時間,過期之后自動刪除。這個選項非常有用,比如我們想暫時開放一個端口進行一些特殊的操作(比如遠程調試),等處理完成后再關閉,不過有時候我們處理完之后就忘記關閉了,而現在的--timeout選項就可以幫我們很好地解決這個問題。
firewall-cmd [--permanent] [--zone=zone] --remove-port=portid[-portid]/protocol
firewall-cmd [--permanent] [--zone=zone] --query-port=portid[-portid]/protocol
- icmp-block命令
firewall-cmd [--permanent] [--zone=zone] --list-icmp-blocks
firewall-cmd [--permanent] [--zone=zone] --add-icmp-block=icmptype [--timeout=seconds]
firewall-cmd [--permanent] [--zone=zone] --remove-icmp-block=icmptype
firewall-cmd [--permanent] [--zone=zone] --query-icmp-block=icmptype
- masquerade命令
ip地址偽裝,NAT轉發中的一種。
firewall-cmd [--permanent] [--zone=zone] --add-masquerade [--timeout=seconds]
firewall-cmd [--permanent] [--zone=zone] --remove-masquerade
firewall-cmd [--permanent] [--zone=zone] --query-masquerade
- forward-port命令
端口轉發,支持范圍轉發,比如可以將80到85端口的所有請求都轉發到8080端口
firewall-cmd [--permanent] [--zone=zone] --list-forward-ports
firewall-cmd [--permanent] [--zone=zone] --add-forward-port=port=portid[-portid]:proto=protocol[:toport=portid[-portid]][:toaddr=address[/mask]][--timeout=seconds]
firewall-cmd [--permanent] [--zone=zone] --remove-forward-port=port=portid[-portid]:proto=protocol[:toport=portid[-portid]][:toaddr=address[/mask]]
firewall-cmd [--permanent] [--zone=zone] --query-forward-port=port=portid[-portid]:proto=protocol[:toport=portid[-portid]][:toaddr=address[/mask]]
具體步驟:
4 [2020.12.29 09:31:11]:firewall-cmd --state
6 [2020.12.29 09:31:44]:systemctl start firewalld
7 [2020.12.29 09:31:52]:firewall-cmd --state
8 [2020.12.29 09:32:01]:firewall-cmd --list-all-zone
9 [2020.12.29 09:32:31]:firewall-cmd --permanent --zone=trusted --add-source=10.29.141.188
10 [2020.12.29 09:32:51]:firewall-cmd --permanent --zone=trusted --add-source=10.29.41.163
11 [2020.12.29 09:33:03]:firewall-cmd --permanent --zone=trusted --add-source=10.7.27.5
12 [2020.12.29 09:33:06]:firewall-cmd --permanent --zone=trusted --add-source=10.26.196.234
12 [2020.12.29 09:33:06]:firewall-cmd --permanent --zone=trusted --add-source=10.26.197.234
13 [2020.12.29 09:33:26]:firewall-cmd --reload
14 [2020.12.29 09:33:37]:firewall-cmd --zone=trusted --list-all
15 [2020.12.29 09:33:49]:firewall-cmd --permanent --zone=drop --add-service=ssh
16 [2020.12.29 09:34:00]:firewall-cmd --permanent --zone=drop --add-service=dhcpv6-client
17 [2020.12.29 09:34:11]:firewall-cmd --set-default-zone=drop
18 [2020.12.29 09:34:22]:firewall-cmd --permanent --zone=drop --change-interface=eth0
19 [2020.12.29 09:34:32]:firewall-cmd --reload
20 [2020.12.29 09:34:41]:firewall-cmd --list-all-zone
開放端口:
firewall-cmd --zone=trusted --add-port=3000/tcp --permanent
firewall-cmd --zone=drop --add-port=3000/tcp --permanent
firewall-cmd --reload
firewall-cmd --zone=drop --remove-port=3000/tcp --permanent
Sudo命令執行權限被鎖住:(https://ywnz.com/linux/pamtally/)
pam_tally2 -r -u 用戶名
新增白名單:
sudo firewall-cmd --permanent --zone=trusted --add-source=10.1.x.x
開端口
firewall-cmd --zone=public --add-port=8080/tcp --permanent
移除
sudo firewall-cmd --permanent --zone=trusted --remove-source=10.1.x.x
sudo systemctl stop firewalld
端口開放
sudo firewall-cmd --zone=public --add-port=18770-19090/tcp --permanent
查看開放的端口
sudo firewall-cmd --zone=public --list-ports
指定開放給哪個ip端口
sudo firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="10.1.x.x" port protocol="tcp" port="18769" accept"