Home | 簡體中文 | 繁體中文 | 雜文 | 知乎專欄 | Github | OSChina 博客 | 雲社區 | 雲棲社區 | Facebook | Linkedin | 視頻教程 | 打賞(Donations) | About
知乎專欄多維度架構 微信號 netkiller-ebook | QQ群:128659835 請註明“讀者”

40.5. CentOS 7 Firewalld

http://www.firewalld.org

40.5.1. 如果你不習慣使用firewalld想用回Iptables

安裝iptables

		
# yum install iptables-services

# vim /etc/sysconfig/iptables
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT
		
		

預設firewall作為防火牆的設置

		
#禁止firewall開機啟動

# systemctl disable firewalld.service

#設置防火牆開機啟動

# systemctl enable iptables.service 

#停止firewall 

# systemctl stop firewalld.service 

#重啟防火牆使配置生效

# systemctl restart iptables.service		
		
		

40.5.2. 安裝 firewalld

安裝firewalld

		
yum install firewalld	
		
		

firewall-config 圖形界面

		
yum install firewall-config			
		
		

啟動/停止/啟用/禁用

		
# systemctl start firewalld
# systemctl stop firewalld
# systemctl enable firewalld
# systemctl disable firewalld
# systemctl restart firewalld
		
		

查看運行狀態

		
[root@localhost ~]# systemctl status firewalld 
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
   Active: active (running) since Tue 2019-06-04 11:47:00 CST; 5h 16min ago
     Docs: man:firewalld(1)
 Main PID: 2928 (firewalld)
   CGroup: /system.slice/firewalld.service
           └─2928 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid

Jun 04 11:47:00 localhost.localdomain systemd[1]: Starting firewalld - dynamic firewall daemon...
Jun 04 11:47:00 localhost.localdomain systemd[1]: Started firewalld - dynamic firewall daemon.
		
		
		
查看服務是否開機啟動:systemctl is-enabled firewalld.service
查看已啟動的服務列表:systemctl list-unit-files|grep enabled
查看啟動失敗的服務列表:systemctl --failed		
		
		

40.5.3. firewalld 配置檔案

40.5.3.1. 規則配置檔案
			
[root@localhost ~]# 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"/>
  <port protocol="tcp" port="80"/>
</zone>			
			
			
40.5.3.2. 服務配置檔案
			
[root@localhost ~]# ls -1 /usr/lib/firewalld/services/
amanda-client.xml
amanda-k5-client.xml
bacula-client.xml
bacula.xml
bgp.xml
bitcoin-rpc.xml
bitcoin-testnet-rpc.xml
bitcoin-testnet.xml
bitcoin.xml
ceph-mon.xml
ceph.xml
cfengine.xml
condor-collector.xml
ctdb.xml
dhcpv6-client.xml
dhcpv6.xml
dhcp.xml
dns.xml
docker-registry.xml
docker-swarm.xml
dropbox-lansync.xml
elasticsearch.xml
freeipa-ldaps.xml
freeipa-ldap.xml
freeipa-replication.xml
freeipa-trust.xml
ftp.xml
ganglia-client.xml
ganglia-master.xml
git.xml
gre.xml
high-availability.xml
https.xml
http.xml
imaps.xml
imap.xml
ipp-client.xml
ipp.xml
ipsec.xml
ircs.xml
irc.xml
iscsi-target.xml
jenkins.xml
kadmin.xml
kerberos.xml
kibana.xml
klogin.xml
kpasswd.xml
kprop.xml
kshell.xml
ldaps.xml
ldap.xml
libvirt-tls.xml
libvirt.xml
managesieve.xml
mdns.xml
minidlna.xml
mongodb.xml
mosh.xml
mountd.xml
mssql.xml
ms-wbt.xml
murmur.xml
mysql.xml
nfs3.xml
nfs.xml
nmea-0183.xml
nrpe.xml
ntp.xml
openvpn.xml
ovirt-imageio.xml
ovirt-storageconsole.xml
ovirt-vmconsole.xml
pmcd.xml
pmproxy.xml
pmwebapis.xml
pmwebapi.xml
pop3s.xml
pop3.xml
postgresql.xml
privoxy.xml
proxy-dhcp.xml
ptp.xml
pulseaudio.xml
puppetmaster.xml
quassel.xml
radius.xml
redis.xml
RH-Satellite-6.xml
rpc-bind.xml
rsh.xml
rsyncd.xml
samba-client.xml
samba.xml
sane.xml
sips.xml
sip.xml
smtp-submission.xml
smtps.xml
smtp.xml
snmptrap.xml
snmp.xml
spideroak-lansync.xml
squid.xml
ssh.xml
syncthing-gui.xml
syncthing.xml
synergy.xml
syslog-tls.xml
syslog.xml
telnet.xml
tftp-client.xml
tftp.xml
tinc.xml
tor-socks.xml
transmission-client.xml
upnp-client.xml
vdsm.xml
vnc-server.xml
wbem-https.xml
xmpp-bosh.xml
xmpp-client.xml
xmpp-local.xml
xmpp-server.xml
zabbix-agent.xml
zabbix-server.xml			
			
			
40.5.3.3. 區域配置檔案
			
[root@localhost ~]# ls -1 /usr/lib/firewalld/zones/
block.xml
dmz.xml
drop.xml
external.xml
home.xml
internal.xml
public.xml
trusted.xml
work.xml			
			
			

40.5.4. firewall-cmd

40.5.4.1. 查看版本號
			
[root@localhost ~]# firewall-cmd --version
0.5.3			
			
			
40.5.4.2. 查看幫助

查看幫助: firewall-cmd --help

			
[root@localhost ~]# firewall-cmd --help			
			
			
40.5.4.3. 顯示狀態

顯示狀態: firewall-cmd --state

			
[root@localhost ~]# firewall-cmd --state
running			
			
			
40.5.4.4. 重新載入防火牆規則
			
firewall-cmd --reload			
			
			
40.5.4.5. 持久化

將當前防火牆的規則永久保存

			
[root@localhost ~]# firewall-cmd --runtime-to-permanent
success			
			
			
40.5.4.6. 檢查配置正確性
			
[root@localhost ~]# firewall-cmd --check-config 
success			
			
			
40.5.4.7. 日誌選項
			
--get-log-denied         # 獲取記錄被拒絶的日誌;
--set-log-denied=<value> # 設置記錄被拒絶的日誌,只能為 'all','unicast','broadcast','multicast','off' 其中的一個;			
			
			
40.5.4.8. 拒絶所有包
			
拒絶所有包:firewall-cmd --panic-on
取消拒絶狀態: firewall-cmd --panic-off
查看是否拒絶: firewall-cmd --query-panic			
			
			
40.5.4.9. 直接模式
			
firewall-cmd --direct -add-rule ipv4 filter INPUT 0 -p tcp --dport 9000 -j ACCEPT
firewall-cmd --reload			
			
			

40.5.5. 區域

Firewall 能將不同的網絡連接歸類到不同的信任級別,Zone 提供了以下幾個級別

		
drop: 		丟棄所有進入的包,而不給出任何響應
block: 		拒絶所有外部發起的連接,允許內部發起的連接
public: 	允許指定的進入連接
internal: 	範圍針對所有互聯網用戶
external: 	對偽裝的進入連接,一般用於路由轉發
dmz: 		允許受限制的進入連接
work: 		允許受信任的計算機被限制的進入連接,類似 workgroup
home: 		類似 homegroup
trusted: 	信任所有連接		
		
		
40.5.5.1. 查看區域
			
[root@localhost ~]# firewall-cmd --get-zones
block dmz drop external home internal public trusted work			
			
			
40.5.5.2. 查看預設區域
			
[root@localhost ~]# firewall-cmd --get-default-zone
public			
			
			
40.5.5.3. 設置預設區域
			
firewall-cmd --set-default-zone=inside			
			
			
40.5.5.4. 查看區域對應的網絡介面
			
[root@localhost ~]# firewall-cmd --get-active-zones
public
  interfaces: enp0s3			
			
			
40.5.5.5. 查看指定區域的所有配置
			
[root@localhost ~]# firewall-cmd --zone=public --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: enp0s3
  sources: 
  services: ssh dhcpv6-client
  ports: 
  protocols: 
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 			
			
			
40.5.5.6. 查看所有區域的配置信息
			
[root@localhost ~]# firewall-cmd --list-all-zones
block
  target: %%REJECT%%
  icmp-block-inversion: no
  interfaces: 
  sources: 
  services: 
  ports: 
  protocols: 
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 
	

dmz
  target: default
  icmp-block-inversion: no
  interfaces: 
  sources: 
  services: ssh
  ports: 
  protocols: 
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 
	

drop
  target: DROP
  icmp-block-inversion: no
  interfaces: 
  sources: 
  services: 
  ports: 
  protocols: 
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 
	

external
  target: default
  icmp-block-inversion: no
  interfaces: 
  sources: 
  services: ssh
  ports: 
  protocols: 
  masquerade: yes
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 
	

home
  target: default
  icmp-block-inversion: no
  interfaces: 
  sources: 
  services: ssh mdns samba-client dhcpv6-client
  ports: 
  protocols: 
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 
	

internal
  target: default
  icmp-block-inversion: no
  interfaces: 
  sources: 
  services: ssh mdns samba-client dhcpv6-client
  ports: 
  protocols: 
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 
	

public (active)
  target: default
  icmp-block-inversion: no
  interfaces: enp0s3
  sources: 
  services: ssh dhcpv6-client
  ports: 
  protocols: 
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 
	

trusted
  target: ACCEPT
  icmp-block-inversion: no
  interfaces: 
  sources: 
  services: 
  ports: 
  protocols: 
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 
	

work
  target: default
  icmp-block-inversion: no
  interfaces: 
  sources: 
  services: ssh dhcpv6-client
  ports: 
  protocols: 
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 
			
			
			
40.5.5.7. 刪除區域
			
[root@localhost ~]# firewall-cmd --delete-zone=your_zone --permanent			
			
			
40.5.5.8. 區域介面
40.5.5.8.1. 介面列表
			
[root@localhost ~]# firewall-cmd --list-interfaces
enp0s3		

[root@localhost ~]# firewall-cmd --list-interfaces
wlp5s0
			
				
40.5.5.8.2. 查詢介面所在區域
			
[root@localhost ~]# firewall-cmd --get-zone-of-interface=eth0
no zone
[root@localhost ~]# firewall-cmd --get-zone-of-interface=enp0s3
public			
			
				
40.5.5.8.3. 設置區域介面
			
firewall-cmd --set-default-zone=dmz
firewall-cmd --zone=dmz --add-interface=eth0			
			
				
40.5.5.9. 更在區域介面
			
firewall-cmd --permanent --zone=internal --change-interface=enp03s			
			
			

40.5.6. 連接埠操作

40.5.6.1. 開放連接埠

開放連接埠 firewall-cmd --zone=public --add-port=80/tcp --permanent

			
[root@localhost ~]# firewall-cmd --zone=public --add-port=80/tcp --permanent
success
			
			

--permanent永久生效,沒有此參數重啟後失效

40.5.6.2. 查看連接埠狀態

查看連接埠,使用 firewall-cmd --zone=public --query-port=80/tcp

			
[root@localhost ~]# firewall-cmd --zone=public --query-port=80/tcp
no			
			
			
40.5.6.3. 禁用連接埠

刪除連接埠

			
firewall-cmd --zone=public --remove-port=80/tcp --permanent
			
			

--permanent 表示永久生效

40.5.6.4. 指定連接埠協議
			
firewall-cmd --zome=public --add-port=5060-5059/udp --permanent			
			
			
40.5.6.5. 連接埠轉發

將 80 連接埠的流量轉發到 8080 連接埠

			
firewall-cmd --zone="public" --add-forward-port=port=80:proto=tcp:toport=8080			
			
			
40.5.6.6. IP 轉發

開啟IP偽裝

			
firewall-cmd --zone=public --add-masquerade
			
			

將 80 連接埠轉發到 172.16.0.10:8080 主機

			
firewall-cmd --zone="public" --add-forward-port=port=80:proto=tcp:toport=8080:toaddr=172.16.0.10
			
			

40.5.7. 服務

40.5.7.1. 查看可用的伺服器
			
[root@localhost ~]# firewall-cmd --get-services
RH-Satellite-6 amanda-client amanda-k5-client bacula bacula-client bgp bitcoin bitcoin-rpc bitcoin-testnet bitcoin-testnet-rpc ceph ceph-mon cfengine condor-collector ctdb dhcp dhcpv6 dhcpv6-client dns docker-registry docker-swarm dropbox-lansync elasticsearch freeipa-ldap freeipa-ldaps freeipa-replication freeipa-trust ftp ganglia-client ganglia-master git gre high-availability http https imap imaps ipp ipp-client ipsec irc ircs iscsi-target jenkins kadmin kerberos kibana klogin kpasswd kprop kshell ldap ldaps libvirt libvirt-tls managesieve mdns minidlna mongodb mosh mountd ms-wbt mssql murmur mysql nfs nfs3 nmea-0183 nrpe ntp openvpn ovirt-imageio ovirt-storageconsole ovirt-vmconsole pmcd pmproxy pmwebapi pmwebapis pop3 pop3s postgresql privoxy proxy-dhcp ptp pulseaudio puppetmaster quassel radius redis rpc-bind rsh rsyncd samba samba-client sane sip sips smtp smtp-submission smtps snmp snmptrap spideroak-lansync squid ssh syncthing syncthing-gui synergy syslog syslog-tls telnet tftp tftp-client tinc tor-socks transmission-client upnp-client vdsm vnc-server wbem-https xmpp-bosh xmpp-client xmpp-local xmpp-server zabbix-agent zabbix-server			
			
			
40.5.7.2. 啟用服務
			
[root@localhost ~]# firewall-cmd --zone=public --add-service=mysql --permanent
success			
			
			
40.5.7.3. 禁用服務
			
[root@localhost ~]# firewall-cmd --zone=public --remove-service=mysql --permanent
success			
			
			
40.5.7.4. 指定區域添加服務
			
firewall-cmd --zone=dmz --add-service=http --permanent
firewall-cmd --zone=dmz --add-service=https --permanent			
			
			
40.5.7.5. 查詢服務狀態
			
[root@localhost ~]# firewall-cmd --query-service mysql
no			
			
			
40.5.7.6. 查看持久化服務

查看重啟後所有 Zones 級別中被允許的服務,即永久放行的服務

			
firewall-cmd --get-service --permanent			
			
			

40.5.8. IP 偽裝

40.5.8.1. 開啟 IP 偽裝
			
firewall-cmd --zone=public --add-masquerade
			
			
40.5.8.2. 查看 IP 偽裝
			
firewall-cmd --zone=external --query-masquerade			
			
			
40.5.8.3. 關閉 IP 偽裝
			
firewall-cmd --zone=public --remove-masquerade			
			
			

40.5.9. 富規則

使用 --add-rich-rule, --list-rich-rules, --remove-rich-rule 命令來管理富規則

允許來自主機 192.168.0.14 的所有 IPv4 流量。

			

sudo firewall-cmd --zone=public --add-rich-rule 'rule family="ipv4" source address=192.168.0.14 accept'			
			
		

拒絶來自主機 192.168.1.10 到 22 連接埠的 IPv4 的 TCP 流量。

		
firewall-cmd --zone=public --add-rich-rule 'rule family="ipv4" source address="192.168.1.10" port port=22 protocol=tcp reject'		
		
		

允許來自主機 172.16.0.5 到 80 連接埠的 IPv4 的 TCP 流量,並將流量轉發到 6532 連接埠上。

		
firewall-cmd --zone=public --add-rich-rule 'rule family=ipv4 source address=172.16.0.5 forward-port port=80 protocol=tcp to-port=8080'		
		
		

將主機 172.16.0.2 上 80 連接埠的 IPv4 流量轉發到 8080 連接埠(需要在區域上激活 masquerade)

		
firewall-cmd --zone=public --add-rich-rule 'rule family=ipv4 forward-port port=80 protocol=tcp to-port=8080 to-addr=172.16.0.2'
		
		

列出你目前的豐富規則

		
firewall-cmd --list-rich-rules