sudo dnf install firewalld
firewall-offline-cmd 修改防火墙配置,添加的配置是永久性的,重启后也会保留;firewall-cmd 修改防火墙配置,添加的配置默认是临时性的,使用 --permanent 参数可将规则永久保存到防火墙配置文件中,并执行 firewall-cmd --reload 生效。--list-all 可以查看默认区域及其配置:sudo firewall-offline-cmd --list-all
publictarget: defaulticmp-block-inversion: nointerfaces:sources:services: dhcpv6-client mdns sshports:protocols:forward: yesmasquerade: noforward-ports:source-ports:icmp-blocks:rich rules:
public,允许的服务有 dhcpv6-client mdns ssh。需要注意,此处允许的服务相当于启用该服务的默认端口,例如允许 ssh 服务,则启用 22 端口,如果当前机器使用的不是默认端口,需要单独配置端口规则。sudo firewall-offline-cmd --zone=public --add-port=<port>/tcp
# 启动服务sudo systemctl start firewalld.service# 设置开机默认启动服务sudo systemctl enable firewalld.service
sudo firewall-cmd --state
running 表示服务正常运行。# 开机禁用服务sudo systemctl disable firewalld.service# 关闭服务sudo systemctl stop firewalld.service
sudo firewall-cmd --get-zones
block dmz drop external home internal nm-shared public trusted work
home 或 work 区域,外部网络可以使用 public 或 external 区域。public,在进行配置时可以使用 --zone= 指定区域,不指定则指向默认区域:sudo firewall-cmd --get-default-zone
public
public 区域允许 http/https 服务,首先查看 firewalld 预定义服务列表,是否包含所需服务:sudo firewall-cmd --get-services | grep http
... http http3 https ...
public 区域,由于 public 是默认区域,因此也可以不指定 --zone 参数;使用 --permanent 参数设置为永久规则,防止防火墙重启时配置丢失:sudo firewall-cmd --zone=public --add-service=http --permanentsudo firewall-cmd --zone=public --add-service=http3 --permanentsudo firewall-cmd --zone=public --add-service=https --permanent
public 区域所有打开的端口,此时并没有输出,因为在 firewalld 中,通过服务打开的都是默认端口,不会通过 --list-ports 显示:sudo firewall-cmd --zone=public --list-ports
sudo firewall-cmd --zone=public --add-port=8080/tcp --permanent
sudo firewall-cmd --reload
sudo firewall-cmd --query-port=8080/tcp
yes 表示端口已打开。sudo firewall-cmd --zone=public --remove-port=8080/tcp --permanent
/etc/firewalld/ 目录复制回原始位置,然后重启 firewalld 服务即可。sudo systemctl restart firewalld
链 | filter | nat | mangle | raw | security |
PREROUTING | - | ✓ | ✓ | ✓ | - |
INPUT | ✓ | ✓ | ✓ | - | ✓ |
FORWARD | ✓ | - | ✓ | - | ✓ |
OUTPUT | ✓ | ✓ | ✓ | ✓ | ✓ |
POSTROUTING | - | ✓ | ✓ | - | - |
iptables-extensions(8)。sudo dnf install iptables
--line-numbers 参数显示行号。iptables 默认表为 filter,包含 INPUT、FORWARD 和 OUTPUT 三个链。sudo iptables --list --line-numbers
Chain INPUT (policy ACCEPT)num target prot opt source destinationChain FORWARD (policy ACCEPT)num target prot opt source destinationChain OUTPUT (policy ACCEPT)num target prot opt source destination
# 启动服务sudo systemctl start iptables.service# 开机自启服务sudo systemctl enable iptables.service
# 开机禁用服务sudo systemctl disable iptables.service# 关闭服务sudo systemctl stop iptables.service
-A, --append 在规则最后追加,或者使用 -I, --insert 指定插入规则的顺序(number),如果不指定默认添加到规则列表的首行。sudo iptables -I INPUT -p tcp --dport 22 -j ACCEPT
-p 表示数据包使用 tcp 协议,--dport 表示目的端口为本机的 22 端口,-j 表示执行动作为 ACCEPT。sudo iptables -A INPUT -i lo -j ACCEPT
-i lo:匹配条件,表示仅处理通过本地回环接口(loopback interface)lo 的数据包。回环接口通常用于本地系统内的通信。sudo iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-m 指明使用 state 匹配模块。这个模块根据数据包的连接状态来匹配数据包。--state ESTABLISHED,RELATED:匹配条件,表示仅处理已建立(ESTABLISHED)或与已建立连接相关(RELATED)的数据包。已建立的连接表示双方已经进行了数据交换。相关连接通常是指与已建立连接有关的新连接,例如 FTP 数据连接或 ICMP 错误报告。sudo iptables -A INPUT -j DROP
-P 直接设置每条链的策略,策略值必须为 ACCEPT 或者 DROP,例如刚刚通过 -A 设置的策略也可以通过 -P 设置:sudo iptables -P INPUT DROP
sudo iptables -A INPUT -s 192.168.1.100 -j ACCEPT
sudo iptables -A INPUT -s 10.0.0.50 -j DROP
sudo iptables -A INPUT -s 192.168.1.0/24 -p tcp --dport 80 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPTsudo iptables -A INPUT -p tcp --dport 443 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 3306 -j DROP
sudo iptables -A INPUT -p tcp -m multiport --dports 8080,8443 -j ACCEPT
sudo iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -o eth0 -j SNAT --to-source 203.0.113.1
sudo iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -o eth0 -j MASQUERADE
sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j DNAT --to-destination 192.168.1.100:8080
sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-ports 8080
echo 1 | sudo tee /proc/sys/net/ipv4/ip_forward
/etc/sysctl.conf:net.ipv4.ip_forward = 1
sudo sysctl -p 使其生效。sudo iptables --list --line-numbers
Chain INPUT (policy ACCEPT)num target prot opt source destination1 ACCEPT tcp -- anywhere anywhere tcp dpt:ssh2 ACCEPT all -- anywhere anywhere3 ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED4 DROP all -- anywhere anywhereChain FORWARD (policy ACCEPT)num target prot opt source destinationChain OUTPUT (policy ACCEPT)num target prot opt source destination
sudo iptables -D INPUT 4
iptables-save 命令将规则输出到文件:sudo iptables-save > /etc/sysconfig/iptables
iptables-restore 命令从文件中加载规则:sudo iptables-restore < /etc/sysconfig/iptables
iptables --help、iptables(8)。sudo dnf install nftables
# 启动服务sudo systemctl start nftables.service# 设置开机默认启动服务sudo systemctl enable nftables.service
# 开机禁用服务sudo systemctl disable nftables.service# 关闭服务sudo systemctl stop nftables.service
sudo nft list ruleset
filter 的新表,作用是过滤数据包,指定地址族为 inet,处理 IPv4/IPv6 相关的数据包:sudo nft add table inet filter
add 后面可以接 table、chain、rule,分别表示添加表、添加链、添加规则。filter 表创建名为 input 的基本链,基本链是来自网络栈数据包的入口点:sudo nft add chain inet filter input { type filter hook input priority 0 \\; policy accept \\; }
\\ 用来转义,避免 Shell 将分号解释为命令结束。sudo nft list chain inet filter input
table inet filter {chain input {type filter hook input priority filter; policy accept;}}
dport 可以接常见服务名称,也可以接具体的端口号:sudo nft add rule inet filter input tcp dport ssh accept
sudo nft list ruleset
table inet filter {chain input {type filter hook input priority filter; policy accept;tcp dport ssh accept}}
sudo nft add rule inet filter input tcp dport http acceptsudo nft add rule inet filter input tcp dport https accept
sudo nft add rule inet filter input ct state established,related accept
sudo nft add rule inet filter input ip protocol icmp acceptsudo nft add rule inet filter input ip6 nexthdr ipv6-icmp accept
sudo nft add rule inet filter input iifname "lo" accept
sudo nft add rule inet filter input drop
drop 规则应放在允许规则之后,否则所有流量都将被拒绝。sudo nft add table inet nat
sudo nft add chain inet nat prerouting { type nat hook prerouting priority -100 \\; policy accept \\; }
sudo nft add chain inet nat postrouting { type nat hook postrouting priority 100 \\; policy accept \\; }
sudo nft add rule inet nat postrouting oifname "eth0" masquerade
sudo nft add rule inet nat prerouting tcp dport 80 dnat to 192.168.1.100:8080
-a 参数查看规则的 handle(句柄号),用于删除规则时指定:sudo nft -a list ruleset
table inet filter {chain input {type filter hook input priority filter; policy accept;tcp dport ssh accept # handle 2tcp dport http accept # handle 3tcp dport https accept # handle 4drop # handle 5}}
flush ruleset)后,链的默认策略将放行所有流量,存在安全风险。请您在操作前先确认规则内容,并保留必要的放行规则。sudo nft delete rule inet filter input handle 5
sudo nft flush chain inet filter input
sudo nft flush table inet filter
sudo nft flush ruleset
sudo nft delete chain inet filter input
sudo nft delete table inet filter
/etc/sysconfig/nftables.conf 或 /etc/nftables.conf。sudo nft list ruleset | sudo tee /etc/nftables.conf
sudo nft -f /etc/nftables.conf
sudo systemctl restart nftables.service
/etc/nftables.conf):#!/usr/sbin/nft -fflush rulesettable inet filter {chain input {type filter hook input priority 0; policy drop;# 允许本地回环接口iifname "lo" accept# 允许已建立连接的流量ct state established,related accept# 允许 SSHtcp dport ssh accept# 允许 HTTP 和 HTTPStcp dport { http, https } accept# 允许 ICMP (ping)ip protocol icmp acceptip6 nexthdr ipv6-icmp accept# 拒绝其他所有流量drop}chain forward {type filter hook forward priority 0; policy drop;}chain output {type filter hook output priority 0; policy accept;}}
nft --help、nft(8)。特性 | iptables | nftables |
语法 | 每条规则单独命令,语法较复杂 | 语法更简洁,支持脚本化配置 |
表/链 | 预定义表和链,灵活性有限 | 可自由创建表和链,灵活性更高 |
地址族 | ipv4、ipv6、arp、bridge 分别使用不同命令 | 统一使用 inet 地址族处理 IPv4/IPv6 |
规则更新 | 每条规则更新需替换整个规则集 | 支持增量更新,性能更高 |
配置文件 | /etc/sysconfig/iptables | /etc/nftables.conf |
NAT | 支持,需指定 nat 表 | 支持,语法更简洁 |
状态跟踪 | -m state --state | ct state(connection tracking) |
集合 | 不支持 | 支持命名集合,简化规则管理 |
iptables-translate 和 ip6tables-translate 工具,可以将 iptables 规则自动转换为 nftables 格式。sudo iptables-translate -A INPUT -p tcp --dport 22 -j ACCEPT
add rule inet filter input tcp dport 22 counter accept
sudo ip6tables-translate -A INPUT -p tcp --dport 22 -j ACCEPT
add rule inet filter input tcp dport 22 counter accept
sudo iptables-save | sudo iptables-restore-translate
iptables-save 输出的所有规则批量转换为 nftables 格式,方便整体迁移。文档反馈