Linux系統管理初步(五)系統防火牆控制程序firewalld

CentOS7中,系統自帶的netfilter操作程序由iptables變爲firewalld。firewall有zone和service兩個概念,網口或者說nmcli下的conection可加入某個zone那麼這個con來的數據就會遵循zone下的規則,而每個zone下又有一些service,這些service不受zone默認規則的限制。可以通過con、zone、service的搭配來具體對某一業務進行規劃,保證系統安全。

一、firewalld中的9個zone

firewalld中有9個zone,各個zone的說明如下
drop
Any incoming network packets are dropped; there is no reply. Only outgoing network connections are possible.
block
Any incoming network connections are rejected with an icmp-host-prohibited message for IPv4 and icmp6-adm-prohibited for IPv6. Only network connections initiated from within the system are possible.
public
For use in public areas. You do not trust the other computers on the network to not harm your computer. Only selected incoming connections are accepted.
external
For use on external networks with masquerading enabled, especially for routers. You do not trust the other computers on the network to not harm your computer. Only selected incoming connections are accepted.
dmz
For computers in your demilitarized zone that are publicly-accessible with limited access to your internal network. Only selected incoming connections are accepted.
work
For use in work areas. You mostly trust the other computers on networks to not harm your computer. Only selected incoming connections are accepted.
home
For use in home areas. You mostly trust the other computers on networks to not harm your computer. Only selected incoming connections are accepted.
internal
For use on internal networks. You mostly trust the other computers on the networks to not harm your computer. Only selected incoming connections are accepted.
trusted
All network connections are accepted.

譯文:
由firewalld 提供的區域按照從不信任到信任的順序排序。
丟棄 drop
任何流入網絡的包都被丟棄,不作出任何響應。只允許流出的網絡連接。
阻塞 block
任何進入的網絡連接都被拒絕,並返回 IPv4 的 icmp-host-prohibited 報文或者 IPv6 的 icmp6-adm-prohibited 報文。只允許由該系統初始化的網絡連接。
公開 public
用以可以公開的部分。你認爲網絡中其他的計算機不可信並且可能傷害你的計算機。只允許選中的連接接入。
外部 external
用在路由器等啓用僞裝的外部網絡。你認爲網絡中其他的計算機不可信並且可能傷害你的計算機。只允許選中的連接接入。
隔離區dmz
用以允許隔離區(dmz)中的電腦有限地被外界網絡訪問。只接受被選中的連接。
工作 work
用在工作網絡。你信任網絡中的大多數計算機不會影響你的計算機。只接受被選中的連接。
家庭 home
用在家庭網絡。你信任網絡中的大多數計算機不會影響你的計算機。只接受被選中的連接。
內部 internal
用在內部網絡。你信任網絡中的大多數計算機不會影響你的計算機。只接受被選中的連接。
受信任的 trusted
允許所有網絡連接。

二、firewalld的應用

操作前,我們看下當前系統中應用的是哪種防火牆程序。

systemctl list-units --all --type=service |egrep 'firewalld|ip6tables|iptables'

Linux系統管理初步(五)系統防火牆控制程序firewalld

看active那一列,active的就是在用的程序,如果你看到firewalld那一行是inactive的,那我們就用下列命令把他啓動

systemctl start firewalld #
systemctl enable firewalld #

如果iptables.service是active的那我們也要把他停用

systemctl stop iptables
systemctl disable iptables

都執行完後可以再重複第一條命令看下服務狀態是否跟截圖一致。

firewall命令有點像一個英語句子,好理解,但是輸入有點煩
比如說

firewall-cmd --get-default-zone
firewall-cmd --set-default-zone=work
firewall-cmd --get-zone-interface=ens33

命令行操作

(一)操作網卡與zone的關係

1、查看新加接口默認的zone

firewall-cmd --get-default-zone

Linux系統管理初步(五)系統防火牆控制程序firewalld

2、設定新接口加入時的默認zone

firewall-cmd --set-default-zone=work

Linux系統管理初步(五)系統防火牆控制程序firewalld
3、查看接口所在的zone

firewall-cmd --get-zone-of-interface=ens33

Linux系統管理初步(五)系統防火牆控制程序firewalld

4、給指定的網卡設置zone

firewall-cmd --zone=dmz --d-interface=ens33

Linux系統管理初步(五)系統防火牆控制程序firewalld

5、更改某個網卡的zone

firewall-cmd --zone=public --change-interface=ens33

Linux系統管理初步(五)系統防火牆控制程序firewalld

6、查看系統所有網卡所在的zone

firewall-cmd --get-active-zones

Linux系統管理初步(五)系統防火牆控制程序firewalld

(二)操作zone的service

1、列出系統中存在的所有網絡服務(比如http、shttp、ssh等等)

firewall-cmd --get-services

Linux系統管理初步(五)系統防火牆控制程序firewalld

2、列出默認zone的service

firewall-cmd --list-service

Linux系統管理初步(五)系統防火牆控制程序firewalld

3、列出特定zone的service

firewall-cmd --zone=trusted --list-service

Linux系統管理初步(五)系統防火牆控制程序firewalld

4、將某個服務加入特定的zone

firewall-cmd --zone=work --add-service=ftp

Linux系統管理初步(五)系統防火牆控制程序firewalld

相對的將某個service移出某個zone

firewall-cmd --zone=work --remove-service=ftp

Linux系統管理初步(五)系統防火牆控制程序firewalld

5、將服務的改動寫入配置文件重啓後也生效

firewall-cmd --zone=work --add-service=ftp --permanent #就是在末尾加個permanent參數

(三)、通過更改配置文件實現service的控制

firewalld的配置文件保存在,同時程序爲我們預備了zone控制的模板與service的模板,其中zone模板保存在/usr/lib/firewalld/zones/下,而service的模板保存在/usr/lib/firewalld/services/目錄下,都是一些*.xml的文件。

模板路徑 控制文件路徑
zone /usr/lib/firewalld/zones/ /etc/firewalld/zones
service /usr/lib/firewalld/services/ /etc/firewalld/services

Linux系統管理初步(五)系統防火牆控制程序firewalld

Linux系統管理初步(五)系統防火牆控制程序firewalld

下面我們通過一個例子說明如何用模板控制zone下的service
需求:將FTP service的默認端口改爲1121,並且在work zone下放行

首先我們將ftp的模板拷貝到控制文件目錄中

cp /usr/lib/firewalld/services/ftp.xml /etc/firewalld/services

Linux系統管理初步(五)系統防火牆控制程序firewalld

然後把默認的21端口改爲1121端口

sed -i 's/21/1121/g' ftp.xml

Linux系統管理初步(五)系統防火牆控制程序firewalld

然後把work zone的模板拷貝到控制文件目錄下

cp -v /usr/lib/firewalld/zones/work.xml /etc/firewalld/zones #建議加個-v參數,方便檢查操作正誤

Linux系統管理初步(五)系統防火牆控制程序firewalld

修改控制目錄下的work.xml 把ftp服務加入白名單
(玩個花活^^)

sed -i '7i \ \ <service name="ftp"/>' work.xml

Linux系統管理初步(五)系統防火牆控制程序firewalld

重載配置文件讓添加的規則生效

firewall-cmd --reload #重新加載firewalld配置文件
firewall-cmd --zone=work --list-services #檢查配置結果

Linux系統管理初步(五)系統防火牆控制程序firewalld

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章