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
容許全部網絡鏈接。ssh
操做前,咱們看下當前系統中應用的是哪一種防火牆程序。ide
systemctl list-units --all --type=service |egrep 'firewalld|ip6tables|iptables'命令行
看active那一列,active的就是在用的程序,若是你看到firewalld那一行是inactive的,那咱們就用下列命令把他啓動3d
systemctl start firewalld #
systemctl enable firewalld #router
若是iptables.service是active的那咱們也要把他停用xml
systemctl stop iptables
systemctl disable iptablesblog
都執行完後能夠再重複第一條命令看下服務狀態是否跟截圖一致。排序
firewall命令有點像一個英語句子,好理解,可是輸入有點煩
好比說
firewall-cmd --get-default-zone
firewall-cmd --set-default-zone=work
firewall-cmd --get-zone-interface=ens33
命令行操做
一、查看新加接口默認的zone
firewall-cmd --get-default-zone
二、設定新接口加入時的默認zone
firewall-cmd --set-default-zone=work
三、查看接口所在的zone
firewall-cmd --get-zone-of-interface=ens33
四、給指定的網卡設置zone
firewall-cmd --zone=dmz --d-interface=ens33
五、更改某個網卡的zone
firewall-cmd --zone=public --change-interface=ens33
firewall-cmd --get-active-zones
一、列出系統中存在的全部網絡服務(好比http、shttp、ssh等等)
firewall-cmd --get-services
二、列出默認zone的service
firewall-cmd --list-service
三、列出特定zone的service
firewall-cmd --zone=trusted --list-service
四、將某個服務加入特定的zone
firewall-cmd --zone=work --add-service=ftp
相對的將某個service移出某個zone
firewall-cmd --zone=work --remove-service=ftp
五、將服務的改動寫入配置文件重啓後也生效
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 |
下面咱們經過一個例子說明如何用模板控制zone下的service
需求:將FTP service的默認端口改成1121,而且在work zone下放行
首先咱們將ftp的模板拷貝到控制文件目錄中
cp /usr/lib/firewalld/services/ftp.xml /etc/firewalld/services
而後把默認的21端口改成1121端口
sed -i 's/21/1121/g' ftp.xml
而後把work zone的模板拷貝到控制文件目錄下
cp -v /usr/lib/firewalld/zones/work.xml /etc/firewalld/zones #建議加個-v參數,方便檢查操做正誤
修改控制目錄下的work.xml 把ftp服務加入白名單
(玩個花活^^)
sed -i '7i \ \ <service name="ftp"/>' work.xml
重載配置文件讓添加的規則生效
firewall-cmd --reload #從新加載firewalld配置文件
firewall-cmd --zone=work --list-services #檢查配置結果