Linux防火牆(firewalld篇)

firewalld(centos7中的防火牆) 是 iptables 的前端控制器(iptables的封裝),用於實現持久的網絡流量規則。它提供命令行和圖形界面,在大多數 Linux 發行版的倉庫中都有。與直接控制 iptables 相比,使用 FirewallD 有兩個主要區別:

FirewallD 使用區域和服務而不是鏈式規則。
它動態管理規則集,允許更新規則而不破壞現有會話和連接。

(注centos7向後兼容iptables)

centos7上使用iptables工具

systemctl stop firewalld    #停止firewalld服務
systemctl disable firewalld #禁止firewalld服務開機啓動
yum install -y iptables-services    #系統默認不自帶iptables工具
systemctl enable iptables   #讓它開機啓動
systemctl start iptables    #啓動iptables服務

centos7上使用firewalld工具

systemctl stop iptables     #停止該服務
systemctl disable iptables  #開機禁止啓動
systemctl start firewalld   #啓動該服務
systemctl enable firewalld  #開機自啓
systemctl stop firewalld    #停止該服務
systemctl disable firewalld #開機禁止啓動

(注:如果你在centos7上安裝了python3且將/usr/bin/python3軟鏈接爲python時, 啓動firewalld會失敗,解決方法:)

ls /usr/bin/ |grep firewall
#firewall-cmd
#firewall-config
#firewall-offline-cmd

ls ls /usr/sbin/firewalld
#/usr/sbin/firewalld

使用vim分別打開這幾個文件,將首行的/usr/bin/python改爲/usr/bin/python2.7

firewalld有兩個基本概念,分別是zone和service。每一個zone裏面有不同iptables規則,默認一共九個zone,而centos7默認的zone爲public。

“區域”(zone)是針對給定位置或場景(例如家庭、公共、受信任等)可能具有的各種信任級別的預構建規則集。不同的區域允許不同的網絡服務和入站流量類型,而拒絕其他任何流量。 首次啓用 firewalld後,public 將是默認區域。

區域也可以用於不同的網絡接口。例如,要分離內部網絡和互聯網的接口,你可以在 internal 區域上允許 DHCP,但在external 區域僅允許 HTTP 和 SSH。未明確設置爲特定區域的任何接口將添加到默認區域。

firewall-cmd --get-zones    #獲取系統中的所有zone
#block dmz drop external home internal public trusted work

firewall-cmd --get-default-zone #獲取系統中的默認zone
#public

firewall-cmd --set-default-zone=public  #設定默認的zone

firewall-cmd --get-zone-of-interface=ens33  #查看指定網卡的zone
#public

firewall-cmd --zone=public --add-interface=lo   #給指定網卡設置zone
#success

firewall-cmd --zone=dmz  --change-interface=lo  #針對網卡更改zone
#success

firewall-cmd --zone=dmz --remove-interface=lo   #針對網卡刪除zone
#success

firewall-cmd --get-zone-of-interface=lo #查看指定網卡的zone
#no zone

firewall-cmd --get-active-zones #查看系統的所有網卡所在的zone
#public
#  interfaces: ens33

每個zone裏面都使用了不同的service,而service就是針對一個服務(端口)做的iptables規則。

firewall-cmd --get-services #查看系統中所有的services
-------------------------------------------------
RH-Satellite-6 amanda-client amanda-k5-client bacula bacula-client bitcoin bitcoin-rpc bitcoin-testnet bitcoin-testnet-rpc ceph ceph-mon cfengine condor-collector ctdb dhcp dhcpv6 dhcpv6-client dns docker-registry dropbox-lansync elasticsearch freeipa-ldap freeipa-ldaps freeipa-replication freeipa-trust ftp ganglia-client ganglia-master high-availability http https imap imaps ipp ipp-client ipsec iscsi-target kadmin kerberos kibana klogin kpasswd kshell ldap ldaps libvirt libvirt-tls managesieve mdns mosh mountd ms-wbt mssql mysql nfs nrpe ntp openvpn ovirt-imageio ovirt-storageconsole ovirt-vmconsole pmcd pmproxy pmwebapi pmwebapis pop3 pop3s postgresql privoxy proxy-dhcp ptp pulseaudio puppetmaster quassel radius rpc-bind rsh rsyncd samba samba-client sane sip sips smtp smtp-submission smtps snmp snmptrap spideroak-lansync squid ssh synergy syslog syslog-tls telnet tftp tftp-client tinc tor-socks transmission-client vdsm vnc-server wbem-https xmpp-bosh xmpp-client xmpp-local xmpp-server
-------------------------------------------------
這些service都是由一個個配置文件定義的,
配置文件的模板在/usr/lib/firewalld/services/目錄下,
真正生效的配置在/etc/firewalld/services/目錄下

ls /usr/lib/firewalld/services  #未全部列出
---------------------------------------------
amanda-client.xml        kadmin.xml                quassel.xml
amanda-k5-client.xml     kerberos.xml              radius.xml
bacula-client.xml        kibana.xml                RH-Satellite-6.xml
bacula.xml               klogin.xml                rpc-bind.xml
bitcoin-rpc.xml          kpasswd.xml               rsh.xml
bitcoin-testnet-rpc.xml  kshell.xml                rsyncd.xml
bitcoin-testnet.xml      ldaps.xml                 samba-client.xml
bitcoin.xml              ldap.xml                  samba.xml
ceph-mon.xml             libvirt-tls.xml           sane.xml
----------------------------------------------------------

ls /etc/firewalld/services  #默認爲空
firewall-cmd --list-services    #查看當前zone下有哪些services
#ssh dhcpv6-client

firewall-cmd --zone=public --list-services
#ssh dhcpv6-client  #查看指定zone下有哪些services

#對於每個zone來說,都有自己的配置文件,查看zone的配置文件
ls /usr/lib/firewalld/zones
#block.xml  drop.xml      home.xml      public.xml   work.xml
#dmz.xml    external.xml  internal.xml  trusted.xml

#查看public這個zone的配置文件
cat /etc/firewalld/zones/public.xml
---------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<zone>
  <short>Public</short>
  <description>For use in public areas. You do not trust the other computers on networks to not harm your computer. Only selected incoming connections are accepted.</description>
  <service name="ssh"/>
  <service name="dhcpv6-client"/>
</zone>
-----------------------------------------------------------


#在指定zone中增加一個service,僅僅在內存中生效,並沒有修改配置文件
firewall-cmd  --zone=public --add-service=http
#success

#在指定zone中增加一個service,且修改其配置文件,加--permanent
firewall-cmd  --zone=public --add-service=http --permanent
#success

配置文件位於兩個目錄中

  • /usr/lib/firewalld 下保存默認配置,如默認區域和公用服務。 因避免修改它們,因爲每次 firewall 軟件包更新時都會覆蓋這些文件。
  • /etc/firewalld 下保存系統配置文件。 這些文件將覆蓋默認配置。

舉例:假如服務器上配置了一個ftp服務,但端口 並非默認的21號,而是1212,並且需要在work zone 下放行ftp。

vi /usr/lib/firewalld/services/ftp.xml  #更改端口號爲1212
-------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<service>
  <short>FTP</short>
  <description>FTP is a protocol used for remote file transfer. If you plan to make your FTP server publicly available, enable this option. You need the vsftpd package installed for this option to be useful.</description>
  #<port protocol="tcp" port="21"/>
  <port protocol="tcp" port="1212"/>
  <module name="nf_conntrack_ftp"/>
</service>
---------------------------------------------------------

#cp 模板 真正生效的配置文件
cp /usr/lib/firewalld/zones/work.xml /etc/firewalld/zones/  

vi /etc/firewalld/zones/work.xml    #加入一行ftp相關的配置
---------------------------------------------------------
<zone>
  <short>Work</short>
  <description>For use in work areas. You mostly trust the other computers on networks to not harm your computer. Only selected incoming connections are accepted.</description>
  <service name="ssh"/>
  <service name="dhcpv6-client"/>
  #此處增加配置
  <service name="ftp"/>
</zone>
----------------------------------------------------------

firewall-cmd --reload   #重新加載

#驗證一下work zone裏面是否有ftp
firewall-cmd --zone=work --list-services
#ssh dhcpv6-client ftp
#檢查防火牆狀態。輸出應該是 running 或者 not running。
firewall-cmd --state

#要查看 firewalld 守護進程的狀態:
systemctl status firewalld

#重新加載 Firewalld 配置:
firewall-cmd --reload

firewalld 使用 XML 進行配置。除非是非常特殊的配置,你不必處理它們,而應該使用 firewall-cmd

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