在 Linux 中配置 firewalld 规则
什么是 FirewallD
[yijiFirewalld zones[/yiji]
以下命令列出 FirewallD 提供的zones。运行以下命令以列出zones:
[root@server1 ~]# firewall-cmd --get-zones
block dmz drop external home internal public trusted work
Firewalld services
[root@server1 ~]# firewall-cmd --get-services
Firewalld的临时设置和永久设置
启用、禁用Firewalld
# 启用Firewalld
[root@server1 ~]# systemctl start firewalld
# 禁用Firewalld
[root@server1 ~]# systemctl stop firewalld
# 开机启动
[root@server1 ~]# systemctl enable firewlald
# 禁止开机启动
[root@server1 ~]# systemctl disable firewalld
[root@server1 ~]# systemctl status firewalld
或者
[root@server1 ~]# firewall-cmd --state
running
zone管理
[root@server1 ~]# firewall-cmd --get-default-zone
public
[root@server1 ~]# firewall-cmd --list-all
public (active)
target: default
icmp-block-inversion: no
interfaces: ens160
sources:
services: cockpit dhcpv6-client ntp ssh
ports: 2222/tcp
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
[root@server1 ~]# firewall-cmd --zone=home --change-interface=ens33
success
[root@server1 ~]# firewall-cmd --zone=home --list-all
home (active)
target: default
icmp-block-inversion: no
interfaces: ens33
sources:
services: cockpit dhcpv6-client mdns samba-client ssh
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
[root@server1 ~]# firewall-cmd --get-active-zones
home
interfaces: ens33
public
interfaces: ens160
[root@server1 ~]# firewall-cmd --set-default-zone=home
[root@server1 ~]# firewall-cmd --get-zone-of-interface=ens160
public
[root@server1 ~]# firewall-cmd --permanent --new-zone=test
success
[root@server1 ~]# firewall-cmd --reload
success
开放和关闭端口
[root@server1 ~]# firewall-cmd --list-ports
2222/tcp
[root@server1 ~]# firewall-cmd --permanent --add-port=8080/tcp
success
[root@server1 ~]# firewall-cmd --reload
success
[root@server1 ~]# firewall-cmd --remove-port=8080/tcp
success
[root@server1 ~]# firewall-cmd --list-ports
[root@server1 ~]# firewall-cmd --permanent --zone=home --add-port=80/tcp
success
[root@server1 ~]# firewall-cmd --reload
success
[root@server1 ~]# firewall-cmd --zone=home --remove-port=80/tcp
success
添加和移除服务类型
/usr/lib/firewalld/services
目录中。[root@server1 ~]# cat /usr/lib/firewalld/services/samba.xml
[root@server1 ~]# firewall-cmd --permanent --zone=home --add-service=samba
success
[root@server1 ~]# firewall-cmd --reload
success
[root@server1 ~]# firewall-cmd --info-service=samba
samba
ports: 137/udp 138/udp 139/tcp 445/tcp
protocols:
source-ports:
modules: netbios-ns
destination:
[root@server1 ~]# firewall-cmd --permanent --zone=home --add-service={http,https}
success
[root@server1 ~]# firewall-cmd --reload
success
设置端口转发
external
区域启用伪装。[root@server1 ~]# firewall-cmd --permanent --zone=external --add-masquerade
[root@server1 ~]# firewall-cmd --zone=external --query-masquerade
yes
[root@server1 ~]# firewall-cmd --permanent --zone=external --add-forward-port=port=80:proto=tcp:toport=8080
success
[root@server1 ~]# firewall-cmd --permanent --zone=external --add-forward-port=port=80:proto=tcp:toport=8080:toaddr=10.0.0.75
success
[root@server1 ~]# firewall-cmd --permanent --zone=home --add-source=192.168.1.0/24
success
富规则设置
man firewalld.richlanguage
并找到示例。富规则的一般规则结构如下:
rule
[source]
[destination]
service|port|protocol|icmp-block|icmp-type|masquerade|forward-port|source-port
[log]
[audit]
[accept|reject|drop|mark]
[root@server1 ~]# firewall-cmd --zone=public --add-rich-rule='rule family="ipv4" source address="192.168.0.0/24" accept'
success
[root@server1 ~]# firewall-cmd --zone=public --add-rich-rule='rule family="ipv4" source address="192.168.0.0/24" service name="ssh" log prefix="ssh" level="info" accept'
success
[root@server1 ~]# firewall-cmd --zone=public --add-rich-rule='rule family="ipv4" source address="192.168.10.0/24" port port=22 protocol=tcp reject'
success
--remove-rich-rule
选项,下面使用--list-rich-rules
列出富规则,然后删除掉富规则:[root@server1 ~]# firewall-cmd --zone=public --list-rich-rules
rule family="ipv4" source address="192.168.0.0/24" accept
rule family="ipv4" source address="192.168.0.0/24" service name="ssh" log prefix="ssh" level="info" accept
rule family="ipv4" source address="192.168.10.0/24" port port="22" protocol="tcp" reject
[root@server1 ~]# firewall-cmd --zone=public --remove-rich-rule='rule family="ipv4" source address="192.168.0.0/24" accept'
success
[root@server1 ~]# firewall-cmd --zone=public --remove-rich-rule='rule family="ipv4" source address="192.168.0.0/24" service name="ssh" log prefix="ssh" level="info" accept'
success
[root@server1 ~]# firewall-cmd --zone=public --remove-rich-rule='rule family="ipv4" source address="192.168.10.0/24" port port="22" protocol="tcp" reject'
success
Firewalld 的 Direct 规则
/etc/firewalld/direct.xml
文件中的规则并重新加载防火墙以激活这些规则。Direct规则主要由服务或应用程序用来添加特定的防火墙规则。[root@server1 ~]# firewall-cmd --permanent --direct --add-rule ipv4 filter INPUT 0 -p tcp --dport 8081 -j ACCEPT
success
[root@server1 ~]# firewall-cmd --reload
success
[root@server1 ~]# firewall-cmd --direct --get-all-rules
ipv4 filter INPUT 0 -p tcp --dport 8080 -j ACCEPT
ipv4 filter INPUT 0 -p tcp --dport 8081 -j ACCEPT
[root@server1 ~]# firewall-cmd --direct --get-all-rules
ipv4 filter INPUT 0 -p tcp --dport 8080 -j ACCEPT
ipv4 filter INPUT 0 -p tcp --dport 8081 -j ACCEPT
[root@server1 ~]# firewall-cmd --permanent --direct --remove-rule ipv4 filter INPUT 0 -p tcp --dport 8080 -j ACCEPT
success
[root@server1 ~]# firewall-cmd --reload
success
firewall-cmd --direct --remove-rules ipv4 [table] [chain]
[root@server1 ~]# firewall-cmd --permanent --direct --remove-rules ipv4 filter INPUT
success
[root@server1 ~]# firewall-cmd --reload
success
[root@server1 ~]# firewall-cmd --direct --get-all-rules
总 结
END
官方站点:www.linuxprobe.com
Linux命令大全:www.linuxcool.com
刘遄老师QQ:5604241
Linux技术交流群:2261840
(新群,火热加群中……)
想要学习Linux系统的读者可以点击"阅读原文"按钮来了解书籍《Linux就该这么学》,同时也非常适合专业的运维人员阅读,成为辅助您工作的高价值工具书!
微信扫码关注该文公众号作者
戳这里提交新闻线索和高质量文章给我们。
来源: qq
点击查看作者最近其他文章