Linux 防火墙

Linux Firewalld Iptables shell

Posted by gomyck on July 20, 2023

Linux 防火墙使用案例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# 查看防火墙状态
$ firewall-cmd --zone=internal --query-target

$ firewall-cmd --zone=internal --list-all

$ firewall-cmd --list-all-zones

# rule <rule-options> [action <action-options>]
# <rule-options> 是规则的条件部分,用于指定匹配规则的条件,例如源 IP、目标 IP、端口、协议等。
# <action-options> 是规则的动作部分,用于指定规则匹配时所执行的动作,例如接受、拒绝、重定向等。

# <rule-options>
# family=<ipv4|ipv6>:指定规则的 IP 版本,可以是 IPv4 或 IPv6。
# source address=<address>:指定源 IP 地址。
# destination address=<address>:指定目标 IP 地址。
# source port=<port>:指定源端口。
# destination port=<port>:指定目标端口。
# protocol=<protocol>:指定协议,如 tcp、udp、icmp 等。

# <action-options>
# accept:接受匹配的流量。
# reject:拒绝匹配的流量。
# drop:丢弃匹配的流量。
# redirect to-port=<port>:将匹配的流量重定向到指定的端口。

$ firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="192.168.99.99" port port="5432" protocol="tcp" accept'
$ firewall-cmd --permanent --remove-rich-rule='rule family="ipv4" source address="192.168.99.99" port port="5432" protocol="tcp" accept'

# 添加信任区域
$ firewall-cmd --zone=trusted --permanent --add-source=172.17.0.1/16
#删除信任区域
$ firewall-cmd --zone=trusted --permanent --remove-source=172.17.0.1/16

# 添加端口
$ firewall-cmd --zone=public --permanent --add-port=80/tcp
# 删除端口
$ firewall-cmd --zone=public --permanent --remove-port=80/tcp

# 添加服务
$ firewall-cmd --zone=public --permanent --add-service=http
# 删除服务
$ firewall-cmd --zone=public --permanent --remove-service=http

# 当前运行规则永久生效
$ firewall-cmd --runtime-to-permanent
# 重启防火墙
$ firewall-cmd --reload

# 查看防火墙状态
$ firewall-cmd --state
# 查看防火墙版本
$ firewall-cmd --version