firewalld規則寫法


需要注意的是Firewalld中的區域與接口

一個網卡僅能綁定一個區域。比如: eth0-->A區域

但一個區域可以綁定多個網卡。比如: B區域-->eth0、eth1、eth2

可以根據來源的地址設定不同的規則。比如:所有人能訪問80端口,但只有公司的IP才允許訪問22端口

trusted 允許所有的數據包流入與流出 白名單

public 拒絕流入的流量,除非與流出的流量相關;而如果流量與ssh、dhcpv6-client服務相關,則允許流量

drop 拒絕流入的流量,除非與流出的流量相關 黑名單

 

1.獲取當前默認的區域

[root@m01 ~]# firewall-cmd --get-default-zone

public

2.查看當前活動的區域

[root@m01 ~]# firewall-cmd --get-active-zones

public

interfaces: eth0 eth1

3.查看firewalld規則明細

[root@m01 ~]# firewall-cmd --list-all

public (active)

target: default

icmp-block-inversion: no

interfaces: eth0 eth1

sources:

services: ssh dhcpv6-client

ports:

protocols:

masquerade: no

forward-ports:

source-ports:

icmp-blocks:

rich rules:

3.使用firewalld多個區域規則結合,調整默認public區域拒絕所有流量,但如果來源IP是10.0.0.1/24則允許。

[root@m01 ~]# firewall-cmd --remove-service=ssh

[root@m01 ~]# firewall-cmd --add-source=10.0.0.1/32 --zone=trusted

[root@m01 ~]# firewall-cmd --get-active-zone

public

interfaces: eth0 eth1

trusted

sources: 10.0.0.1/32

runtime運行時: 修改規則馬上生效,但如果重啟服務則馬上失效,測試建議。

permanent持久配置: 修改規則后需要reload重載服務才會生效,生產建議。

 

firewalld放行端口

[root@m01 ~]# firewall-cmd --add-port=80/tcp

[root@m01 ~]# firewall-cmd --add-port={8081/tcp,8082/tcp}

[root@m01 ~]# firewall-cmd --remove-port={8081/tcp,8082/tcp}

firewalld放行自帶服務

[root@m01 ~]# firewall-cmd --add-service={http,https}

[root@m01 ~]# #firewall-cmd --add-port=6379/tcp

[root@m01 ~]# firewall-cmd --add-service=redis

firewalld放行自定義的服務 qqqq--->1111

[root@m01 ~]# cd /usr/lib/firewalld/services/

[root@m01 services]# cp http.xml qqqq.xml

[root@m01 services]# vim qqqq.xml 80--->1111

[root@m01 services]# systemctl restart firewalld

[root@m01 services]# firewall-cmd --add-service=qqqq

 

firewalld端口轉發 --> 四層負載均衡

firewall-cmd --permanent --zone=<區域> --add-forward-port=port=<源端口號>:proto=<協議>:toport=<目標端口號>:toaddr=<目標IP地址>

[root@m01 ~]# firewall-cmd --add-forward-port=port=5555:proto=tcp:toport=22:toaddr=172.16.1.7

[root@m01 ~]# firewall-cmd --add-masquerade #IP地址偽裝

[root@m01 ~]# firewall-cmd --permanent --add-forward-port=port=6666:proto=tcp:toport=3306:toaddr=172.16.1.51

 

firewalld富規則

rule

[source]

[destination]

service|port|protocol|icmp-block|masquerade|forward-port

[log]

[audit]

[accept|reject|drop]

rule [family="ipv4|ipv6"]

source address="address[/mask]" [invert="True"]

service name="service name"

port port="port value" protocol="tcp|udp"

forward-port port="port value" protocol="tcp|udp" to-port="port value" to-addr="address"

accept | reject | drop

1.允許10.0.0.0/24網段能夠訪問http服務,僅允許172.16.1.7/32主能訪問ssh服務

10網段能訪問http服務

172.16.1.7能訪問ssh服務

其他統統拒絕

[root@m01 ~]# firewall-cmd --remove-service=ssh

[root@m01 ~]# firewall-cmd --remove-service=dhcpv6-client

[root@m01 ~]# firewall-cmd --add-rich-rule='rule family=ipv4 source address=10.0.0.0/24 service name=http accept'

[root@m01 ~]# firewall-cmd --add-rich-rule='rule family=ipv4 source address=172.16.1.7/32 port port=22 protocol=tcp accept'

2.默認所有人就能連接ssh服務,但拒絕172.16.1.0/24網段.

[root@m01 ~]# firewall-cmd --add-rich-rule='rule family=ipv4 source address=172.16.1.0/24 service name=ssh drop'

3.允許所有人能訪問http,https服務,但只有10.0.0.1主機可以訪問ssh服務

[root@m01 ~]# firewall-cmd --remove-service=ssh

[root@m01 ~]# firewall-cmd --add-service={http,https}

[root@m01 ~]# firewall-cmd --add-rich-rule='rule family=ipv4 source address=10.0.0.1/32 service name=ssh accept'

4.當用戶來源IP地址是10.0.0.1主機,則將用戶請求的5555端口轉發至后端172.16.1.7的22端口

[root@m01 ~]# firewall-cmd --add-rich-rule='rule family="ipv4" source address="10.0.0.1/32" forward-port port=5555 protocol=tcp to-port=22 to-addr=172.16.1.7'

如果沒有添加--permanent參數則重啟firewalld會失效

富規則按先后順序匹配,按先匹配到的規則生效


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM