nftables #
nftables 的规则通过Table、Chain、Rule进行组织,支持过滤、NAT、路由等多种功能。
nftables 的配置文件位于 /etc/nftables.conf
。
iptables 和 nftables #
iptables 和 nftables 都是 Linux 内核提供的网络包过滤框架,用于实现防火墙、NAT、流量控制等功能。 nftables 是 iptables 的继任者,旨在提供更高效、更灵活的配置方式。
nftables 用法 #
bash
# 启用服务
systemctl enable --now nftables.service
# 查看当前规则
nft list ruleset
# 丢弃来自192.168.1.100的流量
nft add rule ip filter INPUT ip saddr 192.168.1.100 counter drop
# 允许连接22端口
nft add rule ip filter INPUT tcp dport 22 ct state new,established counter accept
# 允许来自192.168.1.100访问80端口的流量
nft add rule ip filter input ip saddr 192.168.1.100 tcp dport 80 accept
# 允许192.168.1.0/24连接22端口
nft add rule ip filter INPUT ip saddr 192.168.1.0/24 tcp dport 22 ct state new,established counter accept
# 允许经过eth0接口连接3306端口
nft add rule ip filter INPUT iifname eth0 tcp dport 3306 ct state new,established counter accept
# 允许连接80/443端口
nft add rule ip filter INPUT ip protocol tcp tcp dport {80,443} ct state new,established counter accept