天天看點

Linux系統管理初步(五)系統防火牆控制程式firewalld一、firewalld中的9個zone二、firewalld的應用

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

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

允許所有網絡連接配接。

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

systemctl list-units --all --type=service |egrep 'firewalld|ip6tables|iptables'
Linux系統管理初步(五)系統防火牆控制程式firewalld一、firewalld中的9個zone二、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

指令行操作

1、檢視新加接口預設的zone

Linux系統管理初步(五)系統防火牆控制程式firewalld一、firewalld中的9個zone二、firewalld的應用

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

Linux系統管理初步(五)系統防火牆控制程式firewalld一、firewalld中的9個zone二、firewalld的應用

3、檢視接口所在的zone

firewall-cmd --get-zone-of-interface=ens33
Linux系統管理初步(五)系統防火牆控制程式firewalld一、firewalld中的9個zone二、firewalld的應用

4、給指定的網卡設定zone

firewall-cmd --zone=dmz --d-interface=ens33
Linux系統管理初步(五)系統防火牆控制程式firewalld一、firewalld中的9個zone二、firewalld的應用

5、更改某個網卡的zone

firewall-cmd --zone=public --change-interface=ens33
Linux系統管理初步(五)系統防火牆控制程式firewalld一、firewalld中的9個zone二、firewalld的應用
firewall-cmd --get-active-zones
Linux系統管理初步(五)系統防火牆控制程式firewalld一、firewalld中的9個zone二、firewalld的應用

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

firewall-cmd --get-services
Linux系統管理初步(五)系統防火牆控制程式firewalld一、firewalld中的9個zone二、firewalld的應用

2、列出預設zone的service

firewall-cmd --list-service
Linux系統管理初步(五)系統防火牆控制程式firewalld一、firewalld中的9個zone二、firewalld的應用

3、列出特定zone的service

firewall-cmd --zone=trusted --list-service
Linux系統管理初步(五)系統防火牆控制程式firewalld一、firewalld中的9個zone二、firewalld的應用

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

firewall-cmd --zone=work --add-service=ftp
Linux系統管理初步(五)系統防火牆控制程式firewalld一、firewalld中的9個zone二、firewalld的應用

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

firewall-cmd --zone=work --remove-service=ftp
Linux系統管理初步(五)系統防火牆控制程式firewalld一、firewalld中的9個zone二、firewalld的應用

5、将服務的改動寫入配置檔案重新開機後也生效

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

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一、firewalld中的9個zone二、firewalld的應用
Linux系統管理初步(五)系統防火牆控制程式firewalld一、firewalld中的9個zone二、firewalld的應用

下面我們通過一個例子說明如何用模闆控制zone下的service

需求:将FTP service的預設端口改為1121,并且在work zone下放行

首先我們将ftp的模闆拷貝到控制檔案目錄中

cp /usr/lib/firewalld/services/ftp.xml /etc/firewalld/services
Linux系統管理初步(五)系統防火牆控制程式firewalld一、firewalld中的9個zone二、firewalld的應用

然後把預設的21端口改為1121端口

sed -i 's/21/1121/g' ftp.xml
Linux系統管理初步(五)系統防火牆控制程式firewalld一、firewalld中的9個zone二、firewalld的應用

然後把work zone的模闆拷貝到控制檔案目錄下

cp -v /usr/lib/firewalld/zones/work.xml /etc/firewalld/zones #建議加個-v參數,友善檢查操作正誤
Linux系統管理初步(五)系統防火牆控制程式firewalld一、firewalld中的9個zone二、firewalld的應用

修改控制目錄下的work.xml 把ftp服務加入白名單

(玩個花活^^)

sed -i '7i \ \ <service name="ftp"/>' work.xml
Linux系統管理初步(五)系統防火牆控制程式firewalld一、firewalld中的9個zone二、firewalld的應用

重載配置檔案讓添加的規則生效

firewall-cmd --reload #重新加載firewalld配置檔案 firewall-cmd --zone=work --list-services #檢查配置結果
Linux系統管理初步(五)系統防火牆控制程式firewalld一、firewalld中的9個zone二、firewalld的應用

 本文轉自 whytl 51CTO部落格,原文連結:http://blog.51cto.com/11934539/2065873

繼續閱讀