Linux系統上的防火牆體系
系統服務:firewalld
管理工具:firewall-cmd
圖形管理工具firewall-confightml
預設安全區域
根據所在的網絡場所區分,預設保護規則集。
最經常使用的四個區域:
– public:僅容許訪問本機的sshd dhcp ping
– trusted:容許任何訪問
– block:拒絕任何來訪請求(明確拒絕迴應)
– drop:丟棄任何來訪的數據包(直接丟棄不給迴應,節省服務器資源)安全
防火牆的斷定規則: 匹配及中止
1.查看請求數據包中的源IP地址,並與本身全部區域規則依次比較,若是有該源IP地址的規則,則進服務器
入該區域。剩餘其它區域再也不比對,數據包進入該區域後具體的處理方法依據該區域的具體規則。
2.若是全部區域都沒有該源IP地址,則把數據包交給默認區域(public).網絡
firewalld的安裝ssh
[root@server1 ~]# rpm -q firewalld package firewalld is not installed [root@server1 ~]# yum -y install firewalld-0.4.4.4-14.el7.noarch [root@server1 ~]# systemctl start firewalld [root@server1 ~]# systemctl enable firewalld
防火牆默認區域修改curl
#查看默認區域 [root@server1 ~]# firewall-cmd --get-default-zone [root@client1 ~]# ping 192.168.4.100 #客戶端client1能夠Ping通 #把默認區別修改成block [root@server1 ~]# firewall-cmd --set-default-zone=block [root@server1 ~]# firewall-cmd --get-default-zone [root@server1 ~]#
[root@client1 ~]# ping 192.168.4.100 #client1不能ping通,但有迴應
PING 192.168.4.100 (192.168.4.100) 56(84) bytes of data.
From 192.168.4.100 icmp_seq=1 Destination Host Prohibited
From 192.168.4.100 icmp_seq=2 Destination Host Prohibitedtcp
#修改默認區域爲drop
[root@server1 ~]# firewall-cmd --set-default-zone=drop
[root@server1 ~]# firewall-cmd --get-default-zone
[root@server1 ~]# 工具
#客戶端測試時,不能夠通訊,沒有迴應
[root@client1 ~]# ping 192.168.4.100
PING 192.168.4.100 (192.168.4.100) 56(84) bytes of data.測試
在默認區域添加協議:url
#修改默認區域爲public [root@server1 ~]# firewall-cmd --set-default-zone=public [root@server1 ~]# firewall-cmd --get-default-zone #查看區域詳細規則 [root@server1 ~]# firewall-cmd --zone=public --list-all public (active) target: default icmp-block-inversion: no interfaces: eth0 eth1 eth2 eth3 sources: services: ssh dhcpv6-client ports: protocols: masquerade: no forward-ports: source-ports: icmp-blocks: rich rules: [root@server1 ~]# 在客戶端上訪問server1上的ftp,httpd服務 [root@client1 ~]# ftp 192.168.4.100 #ftp服務鏈接失敗 ftp: connect: No route to host #httpd服務鏈接失敗 [root@client1 ~]# curl http://192.168.4.100
curl: (7) Failed connect to 192.168.4.100:80; No route to host #在server1上添加規則,容許http,ftp [root@server1 html]# firewall-cmd --zone=public --add-service=http [root@server1 html]# firewall-cmd --zone=public --add-service=ftp [root@server1 html]# firewall-cmd --zone=public --list-all public (active) target: default icmp-block-inversion: no interfaces: eth0 eth1 eth2 eth3 sources: services: ssh dhcpv6-client http ftp ports: protocols: masquerade: no forward-ports: source-ports: icmp-blocks: rich rules: 客戶端再次測試 [root@client1 ~]# curl http://192.168.4.100
hello~ #http鏈接成功 [root@client1 ~]# ftp 192.168.4.100 #ftp鏈接成功 Connected to 192.168.4.100 (192.168.4.100). #以上修改策略能夠當即生效,但重啓系統或服務時會丟失 [root@server1 ~]# firewall-cmd --reload [root@server1 ~]# firewall-cmd --zone=public --list-all public (active) target: default icmp-block-inversion: no interfaces: eth0 eth1 eth2 eth3 sources: services: ssh dhcpv6-client ports: protocols: masquerade: no forward-ports: source-ports: icmp-blocks: rich rules: [root@server1 ~]#
防火牆永久策略 permanent
#經過permanent使用策略寫入到配置文件 [root@server1 ~]# firewall-cmd --permanent --zone=public --add-service=http [root@server1 ~]# firewall-cmd --permanent --zone=public --add-service=ftp #再次查看規則,由於策略被寫於到了配置文件,因此當前策略中看不到 [root@server1 ~]# firewall-cmd --zone=public --list-all public (active) target: default icmp-block-inversion: no interfaces: eth0 eth1 eth2 eth3 sources: services: ssh dhcpv6-client ports: protocols: masquerade: no forward-ports: source-ports: icmp-blocks: rich rules: [root@server1 ~]# firewall-cmd --reload #從新加載防火牆全部配置,使配置文件中的策略生效 [root@server1 ~]# firewall-cmd --zone=public --list-all public (active) target: default icmp-block-inversion: no interfaces: eth0 eth1 eth2 eth3 sources: services: ssh dhcpv6-client http ftp ports: protocols: masquerade: no forward-ports: source-ports: icmp-blocks: rich rules: [root@server1 ~]#
單獨拒絕虛擬機client1的訪問
[root@server1 ~]# firewall-cmd --zone=block --add-source=192.168.4.1 [root@server1 ~]# firewall-cmd --zone=block --list-all block (active) target: %%REJECT%% icmp-block-inversion: no interfaces: sources: 192.168.4.1 services: ports: protocols: masquerade: no forward-ports: source-ports: icmp-blocks: rich rules: [root@server1 ~]# [root@client1 ~]# ftp 192.168.4.100 #客戶端再次訪問時會被拒絕 ftp: connect: No route to host #此方法是臨時修改,當即生效,若是須要恢復 #firewall-cmd --reload
實現本機的端口映射
本地應用的端口重定向(端口1 --> 端口2)
從客戶機訪問 端口1 的請求,自動映射到本機 端口2
好比,訪問如下兩個地址能夠看到相同的頁面:
http://192.168.4.100:5423 ---> http://192.168.4.100:80
當client1訪問server1的5423,用防火牆把端口重定義爲80
[root@server1 ~]# firewall-cmd --permanent --zone=public --add-forward-port=port=5423:proto=tcp:toport=80 [root@server1 ~]# firewall-cmd --reload [root@server1 ~]# firewall-cmd --zone=public --list-all public (active) target: default icmp-block-inversion: no interfaces: eth0 eth1 eth2 eth3 sources: services: ssh dhcpv6-client http ftp ports: protocols: masquerade: no forward-ports: port=5423:proto=tcp:toport=80:toaddr= source-ports: icmp-blocks: rich rules: [root@server1 ~]# #在客戶端訪問5423端口時會被防火牆映射到80 [root@client1 ~]# curl http://192.168.4.100:5423 [root@client1 ~]#