nftables 详细参数和配置
nftables
是 Linux 内核的包过滤框架,提供了更灵活和高效的机制来编写和管理防火墙规则。下面将详细介绍 nftables
的配置参数和示例。
基本语法和结构
使用 nft
命令来管理规则。基本语法如下:
nft add rule <table> <chain> <rule-specification>
表(Table)
表是规则的容器,可以包含多个链。表可以定义在以下命名空间之一:
ip
:IPv4 包ip6
:IPv6 包arp
:ARP 包inet
:IPv4 和 IPv6 包bridge
:桥接包netdev
:网络设备包
示例:
nft add table inet filter
链(Chain)
链是规则的容器,每个链可以指定一个挂钩类型(hook),如输入(input)、转发(forward)、输出(output)等。链还可以指定一个优先级。
示例:
nft add chain inet filter input { type filter hook input priority 0 \; }
规则(Rule)
规则是具体的包过滤条件和动作。规则可以包含匹配条件(如源地址、目的地址、协议等)和动作(如接受、拒绝、记录日志等)。
示例:
nft add rule inet filter input ip saddr 192.168.1.0/24 counter accept
详细参数介绍
链参数
type <type>
:指定链的类型,可以是filter
、nat
、route
等。hook <hook>
:指定链的挂钩类型,可以是prerouting
、input
、forward
、output
、postrouting
。priority <value>
:指定链的优先级,数值越小优先级越高。policy <policy>
:指定链的默认策略,可以是accept
、drop
等。
示例:
nft add chain inet filter input { type filter hook input priority 0 \; policy accept \; }
规则参数
ip saddr <address>
:匹配源 IP 地址。ip daddr <address>
:匹配目的 IP 地址。ip protocol <protocol>
:匹配 IP 协议,如tcp
、udp
等。tcp sport <port>
:匹配 TCP 源端口。tcp dport <port>
:匹配 TCP 目的端口。udp sport <port>
:匹配 UDP 源端口。udp dport <port>
:匹配 UDP 目的端口。counter
:计数匹配的包。log
:记录日志。accept
:接受包。drop
:丢弃包。reject
:拒绝包,并发送 ICMP 错误消息。mark <value>
:标记包。queue
:将包发送到用户空间进行处理。
示例:
nft add rule inet filter input ip saddr 192.168.1.0/24 counter accept
nft add rule inet filter input tcp dport 22 counter drop
NAT 参数
snat <address>
:源地址转换。dnat <address>
:目的地址转换。masquerade
:地址伪装。
示例:
nft add table ip nat
nft add chain ip nat prerouting { type nat hook prerouting priority -100 \; }
nft add chain ip nat postrouting { type nat hook postrouting priority 100 \; }
nft add rule ip nat prerouting ip daddr 192.168.1.100 dnat to 10.0.0.100
nft add rule ip nat postrouting ip saddr 10.0.0.0/24 masquerade
使用链优先级来确保处理顺序
nftables
使用链的 priority
选项来确保规则在网络栈的不同阶段优先处理。每个链可以指定一个优先级,数值越小优先级越高。
默认情况下,nftables
链的优先级设置为 0。可以通过将优先级设置为负数(例如 -10)来确保 nftables
链优先于 iptables
处理数据包。
完整示例
以下是一个完整的 nftables
配置示例,展示了如何设置一个简单的防火墙规则集:
#!/usr/sbin/nft -f
# 创建表
table inet filter {
# 创建链
chain input {
type filter hook input priority -10; policy drop;
# 允许环回接口
iif lo accept
# 允许已建立和相关的连接
ct state established,related accept
# 允许ICMP包
ip protocol icmp accept
# 允许SSH连接
tcp dport 22 accept
# 记录并拒绝其他包
log prefix "nftables input drop: " drop
}
chain forward {
type filter hook forward priority -10; policy drop;
}
chain output {
type filter hook output priority -10; policy accept;
}
}
table ip nat {
chain prerouting {
type nat hook prerouting priority -100;
# 示例DNAT规则
ip daddr 192.168.1.100 dnat to 10.0.0.100
}
chain postrouting {
type nat hook postrouting priority 100;
# 示例SNAT规则
ip saddr 10.0.0.0/24 masquerade
}
}
处理顺序
在上面的示例中,filter
表中的 input
链、forward
链和 output
链的优先级都设置为 -10,这确保了这些链会在默认优先级为 0 的链之前处理数据包。同时,nat
表中的 prerouting
链的优先级设置为 -100,确保它在非常早的阶段处理数据包,而 postrouting
链的优先级设置为 100,确保它在后期处理数据包。
Route 链类型
route
链类型主要用于修改数据包的路由,常见的用途包括改变数据包的下一跳地址或接口。它可以挂载在以下钩子点(hooks)上:
input
forward
output
postrouting
示例:route 链类型
创建表和链
首先,创建一个包含 route
链类型的表和链。
bash复制代码#!/usr/sbin/nft -f
table inet mytable {
chain preroute {
type route hook prerouting priority -150; policy accept;
}
chain route_output {
type route hook output priority -150; policy accept;
}
}
添加规则
接下来,在 route
链中添加规则,以修改数据包的路由。
bash复制代码nft add table inet mytable
# 创建 preroute 链
nft add chain inet mytable preroute { type route hook prerouting priority -150 \; }
# 创建 route_output 链
nft add chain inet mytable route_output { type route hook output priority -150 \; }
# 在 preroute 链中添加规则,将所有目的地址为 192.168.1.100 的数据包重路由到 192.168.1.1
nft add rule inet mytable preroute ip daddr 192.168.1.100 ip nexthop 192.168.1.1
# 在 route_output 链中添加规则,将所有源地址为 10.0.0.1 的数据包通过接口 eth0 发送
nft add rule inet mytable route_output ip saddr 10.0.0.1 oif eth0
解释
- 创建表和链:
- 创建了一个名为
mytable
的表。 - 在表中创建了两个链:
preroute
和route_output
。 preroute
链用于在数据包进入路由之前修改其路由。route_output
链用于在数据包离开主机时修改其路由。
- 创建了一个名为
- 添加规则:
- 在
preroute
链中添加了一条规则,将所有目的地址为192.168.1.100
的数据包重路由到192.168.1.1
。 - 在
route_output
链中添加了一条规则,将所有源地址为10.0.0.1
的数据包通过接口eth0
发送。
- 在
检查和验证
配置完成后,可以使用以下命令检查和验证防火墙规则:
检查 nftables
规则
sudo nft list ruleset
检查 iptables
规则
sudo iptables -L
测试连接
使用 ping
、curl
或其他网络工具测试连接,确保规则按预期工作。
总结
通过详细了解 nftables
的规则参数和配置,可以灵活地控制网络流量,并确保系统的安全性。通过设置链的优先级,可以确保 nftables
规则在 iptables
规则之前处理数据包,从而实现更精细的流量控制和管理。