王煜林
RHEL7中防火墻Firewalld典型應用與配置
王煜林
(廣東技術師范學院天河學院,廣東 廣州 510540)
本文研究了RHEL7中防火墻的變化,以前RHEL6用的是iptables、ip6tables與ebtables,現(xiàn)在在RHEL7中使用的是基于zone的firewalld防火墻。對于初學者來說,甚難理解,本文通過兩個典型應用,重點講解了firewalld的原理與應用。
防火墻;firewalld;zone
Firewalld是RHEL7下默認的防火墻,它在內(nèi)核的表現(xiàn)還是基于Netfilter,以前的iptables,ip6tables,ebtables都還可以使用,但是它與Firewalld相沖突。Firewalld主要是通過firewalld.service的systemd服務來進行管理,包括啟動、停止、重啟Firewalld。
為了阻止偶然地啟動iptables,ip6tables,ebtables服務,可以使用systemctl命令對其進行mask,這樣這些服務就不會啟動,并且在RHEL7系統(tǒng)啟動時也不會啟動這些服務。
Firewalld是基于zone的防火墻,每一個zone都有自身的規(guī)則來檢查進入的流量,它的工作原理如下:
(1)首先匹配的是源地址,當某一個zone設置了源地址,匹配源地址的數(shù)據(jù)包將會路由到此zone中。
(2)再匹配的是進入的接口,當一個數(shù)據(jù)包進入某個接口后,它將匹配這個接口對應的zone。
(3)否則,默認的zone將會匹配,網(wǎng)絡接口默認就是使用public zone。
系統(tǒng)初始化的zone有:trusted、internal、home、work、dmz、external、public、block、drop。它們的作用是:
Trusted:所有的流量都允許通過。
Block:禁用所有的流量,但是會返回ICМP的拒絕消息。
Drop:禁用所有的流量,不作出任何響應,只允許流出的網(wǎng)絡連接。
internal、home、work、dmz、external、public:用于不同的網(wǎng)絡,勾選的服務則可以通過防火墻。
管理firewalld有三種方式:(1)使用命令行工具firewallcmd;(2)使用圖形工具firewall-config;(3)直接修改配置文件,在/etc/firewalld目錄中,不推薦這種方法。
2.1 假設在內(nèi)網(wǎng)中建有一臺Web服務器,IP地址是10.0.8.99,端口是80。網(wǎng)段10.71.15.0/24中的主機,都可以訪問此web服務器。
具體的配置步驟如下:
(1)啟動防火墻Firewalld
[root@demo~]#systemctl restart firewalld.service
(2)查看當前防火墻的配置
[root@demo~]#firewall-cmd--list-all
public(default,active)
interfaces:eth0
sources:
services:dhcpv6-client ssh
從上面的顯示可以看出,當前默認的zone是public,public默認的接口是eth0,沒有設置sources,只有dhcpv6-client與ssh的數(shù)據(jù)才可以進入demo這臺主機。
(3)安裝并啟動httpd,創(chuàng)建網(wǎng)站的首頁
[root@demo~]#yum install httpd-y
[root@demo~]#systemctl restart httpd
[root@demo~]#echo“firewalld test”>/var/www/html/ index.html
(4)在本機與10.71.15.0/24網(wǎng)段進行測試
本機上測試:
[root@demo~]#curl http://10.0.8.99
firewalld test
在10.71.15.0/24網(wǎng)段進行測試,打開IE瀏覽器,訪問http://10.0.8.99,可以看出是打不開此網(wǎng)頁的。
(5)從10.71.15.0/24網(wǎng)段進入的流量分配到internal zone。
[root@demo~]#firewall-cmd--permanent--zone=internal--add-source=10.71.15.0/24
Success
(6)在internal zone中添加http服務
[root@demo~]#firewall-cmd--permanent--zone=internal--add-service=http
success
(7)讓防火墻的配置生效
[root@demo~]#firewall-cmd--reload
success
(8)在10.71.15.0/24網(wǎng)段中進行測試,輸入 http://10.0.8.99,是可以訪問此網(wǎng)站的。
[root@localhost~]#curl http://10.0.8.99
firewalld test
2.2 假設DМZ區(qū)有一臺Web服務器,IP地址是10.0.8.99,為了安全起見,它工作在8080端口,現(xiàn)要求用戶在訪問的時候,直接可以通過http://10.0.8.99進行訪問。
(1)配置httpd,使其工作在8080端口
[root@demo ~]# cat/etc/httpd/conf/httpd. conf|grep‘Listen 8080’
Listen 8080
[root@demo~]#systemctl restart httpd.service
[root@demo~]#semanage port-a-t http_port_t-p tcp 8080
本機上測試:
[root@demo~]#curl http://10.0.8.99:8080
firewalld test
在10.71.15.0/24網(wǎng)段進行測試,打開IE瀏覽器,訪問http://10.0.8.99,可以看出是打不開此網(wǎng)頁的。
(2)配置Firewalld
1)把源是10.71.15.0/24網(wǎng)段的流加入到dmz區(qū)域
[root@demo~]#firewall-cmd--permanent--zone=dmz--add-source=10.71.15.0/24
2)把http服務加入到dmz區(qū)域
[root@demo~]#firewall-cmd--permanent--zone=dmz--add-service=http
3)添加一條rich rule,把從10.71.15.0/24網(wǎng)段進入的流量到目標80端口轉(zhuǎn)換至8080
[root@demo~]#firewall-cmd--permanent--zone=dmz--add-rich-rule='rule family=ipv4 source address=10.71.15.0/24 forward-port port=80 protocol=tcp to-port=8080'
4)讓防火墻配置生效
[root@demo~]#firewall-cmd--reload
5)查看配置
[root@demo~]#firewall-cmd--list-all--zone=dmz
internal
interfaces:
sources:10.71.15.0/24
services:dhcpv6-client http ipp-client mdns samba-client ssh
rich rules:
rule family="ipv4"source address="10.71.15.0/24" forward-port port="80"protocol="tcp"to-port="8080"
(3)測試
在10.71.15.0/24網(wǎng)段的某臺主機中,打開IE瀏覽器,輸入http://10.0.8.99進入測試,由圖1可以看出,防火墻把80端口轉(zhuǎn)換到了8080端口。

圖1 通過80端口可以訪問web服務器的8080端口
防火墻的變化很大,對于剛接觸它的人來講,直接將其關閉,這樣非常不安全,其實,它在RHEL7中顯得更加靈活,可以適用于不同的場合,只要做一次配置,后面就可以直接使用,比如在家里使用,只要在防火墻的home zone中做相關的設置,在辦公室工作的時候,只要在防火墻的work zone中進行相關的設置即可。
[1]念青.Linux自由之風:啟動和關閉Linux系統(tǒng)[J].電腦愛好者,2012,(19).
[2]Linux World China 2000——為中國的企業(yè)用戶提供適合的Linux應用解決方案[J].電腦編程技巧與維護,2010,(08).
TheApplication and Configuration of Firewalld in RHEL7
Wang Yulin
(Tianhe College of Guangdong Polytechnic Normal University,Guangzhou 510540,Guangdong)
tract】 This paper study on the changes of firewall in RHEL7.RHEL6 uses iptables,ip6tables and ebtables,while RHEL7 uses firewalld based on zone,which is difficult to understand for beginners.This explains the theory and application of firewalld with two typical applications.
words】 firewall;firewalld;zone
王煜林,男,湖南衡陽人,碩士,網(wǎng)絡規(guī)劃師,研究方向:計算機系統(tǒng)集成。