一,firewalld對一個請求會適用哪個zone?
當接收到一個請求時,firewalld具體使用哪個zone?
firewalld是通過三個步驟來判斷的:
-
source,即:源地址
-
interface,即:接收請求的網卡
-
firewalld.conf中配置的默認zone
通常值為:DefaultZone=public
說明:三個步驟的優先級順序降低
即:如果通過source匹配到了一個zone,
則不會再使用interface,
如果通過interface匹配到了zone,
則不會再使用默認zone
說明:劉宏締的架構森林是一個專注架構的博客,地址:https://www.cnblogs.com/architectforest
對應的源碼可以訪問這里獲取: https://github.com/liuhongdi/
說明:作者:劉宏締 郵箱: 371125307@qq.com
二,zone的操作
1,列出當前激活的zone
[root@blog ~]# firewall-cmd --get-active-zones public interfaces: eth0 trusted sources: 121.122.123.105
2,列出缺省的zone
[root@blog ~]# firewall-cmd --get-default-zone
public
3,列出所有的zone
[root@blog ~]# firewall-cmd --get-zones
block dmz drop external home internal public trusted work
三,source和zone的綁定操作
1,得到一個source所屬的zone
[root@blog ~]# firewall-cmd --get-zone-of-source=121.122.123.105 trusted
2,綁定一個source到zone
例子:把121.122.123.118綁定到trusted這個zone
[root@blog ~]# firewall-cmd --permanent --zone=trusted --add-source=121.122.123.118 success [root@blog ~]# firewall-cmd --reload success
3,一個source能否綁定到了兩個zone?
當121.122.123.118已經被綁定到trusted這個zone后,
能否再被綁定到drop這個zone?
[root@blog ~]# firewall-cmd --permanent --zone=drop --add-source=121.122.123.118 Error: ZONE_CONFLICT: 121.122.123.118
報錯,一個source不能同時綁定到兩個zone
4,列出一個zone下綁定的source
[root@blog ~]# firewall-cmd --permanent --zone=trusted --list-sources 121.122.123.105
5,把一個source從zone下解除綁定
[root@blog ~]# firewall-cmd --permanent --zone=trusted --remove-source=121.122.123.118 success
6,查詢一個source是否和指定的zone做了綁定?
[root@blog firewalld]# firewall-cmd --permanent --zone=drop --query-source=121.122.123.118 yes
四,interface和zone的綁定操作
1,得到一個interface所屬的zone
[root@blog ~]# firewall-cmd --get-zone-of-interface=eth0
public
2,列出一個zone下綁定的interface
[root@blog firewalld]# firewall-cmd --zone=public --list-interfaces
eth0
3,列出所有的interface
[root@blog firewalld]# firewall-cmd --list-interfaces
eth0
4,查詢一個zone下是否綁定了指定的interace?
[root@blog firewalld]# firewall-cmd --zone=public --query-interface=eth0
yes
5,一個interface能否同時屬於多個zone?
[root@blog firewalld]# firewall-cmd --zone=trusted --add-interface=eth0 Error: ZONE_CONFLICT: 'eth0' already bound to a zone
6,從zone下移除interface
[root@blog firewalld]# firewall-cmd --permanent --zone=public --remove-interface=eth0
The interface is under control of NetworkManager and already bound to the default zone
The interface is under control of NetworkManager, setting zone to default.
success
說明:如果一個interface被NM綁定到了default zone,
則不能解綁,
五,如何修改默認的zone?
#--set-default-zone:設置缺省zone
[root@blog firewalld]# firewall-cmd --set-default-zone=public
success
說明:這個命令同時修改了配置文件
[root@blog firewalld]# grep DefaultZone /etc/firewalld/firewalld.conf DefaultZone=public
我們也可以手動修改配置文件
[root@blog firewalld]# vi /etc/firewalld/firewalld.conf
修改DefaultZone指令的值:
DefaultZone=public
然后重啟firewalld
[root@blog firewalld]# systemctl restart firewalld.service
六,查看一個zone的target
1,得到zone的target
[root@blog firewalld]# firewall-cmd --permanent --get-target --zone=public default [root@blog firewalld]# firewall-cmd --permanent --get-target --zone=trusted ACCEPT [root@blog firewalld]# firewall-cmd --permanent --get-target --zone=drop DROP
說明:用--list-all參數也可以把指定zone的信息都打印出來
2,target的值通常有4個:
default
, ACCEPT
, REJECT
和 DROP
ACCEPT
:除了明確禁止的規則,默認會接受所有流入的數據包。
REJECT
:除了明確允許的規則,默認會拒絕所有流入的數據包,
但會給發出連接請求的機器回復被拒絕的消息
DROP:
除了明確允許的規則,默認會拒絕所有流入的數據包,
不會給發起連接
請求的機器回復任何消息
default:沒有指定時,target的值是default:規則就是:每個沒有匹配上的包將會拒絕
(
If the target is not specified, every packet not matching any rule will be rejected.)
文檔地址:
https://firewalld.org/documentation/zone/options.html
七,查看firewalld的版本
[root@blog ~]# firewall-cmd --version 0.6.3
八,查看linux的版本
[root@blog ~]# cat /etc/redhat-release CentOS Linux release 8.0.1905 (Core)