全球主机交流论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

CeraNetworks网络延迟测速工具IP归属甄别会员请立即修改密码
查看: 20055|回复: 10

[经验] 【Linux 防火墙】nftables 简单上手

[复制链接]
发表于 2022-5-23 01:36:44 | 显示全部楼层 |阅读模式
Debian 11 已自带 nftables,为 iptables 原团队研发的新一代 netfilter 解释器/命令行,性能更高效,命令行更直观,建议代替 iptables 使用。

官方Wiki:https://wiki.nftables.org

简单记录一下我习惯启用基本 nftables 的步骤:

1. mkdir /etc/nftables
2. vim /etc/nftables/default.nft

#!/usr/sbin/nft -f

flush ruleset

table ip default {

  chain input {

    type filter hook input priority 0; policy drop;

    iif lo accept
    ct state established, related accept

    # ping
    icmp type echo-request limit rate 500/second accept

    # SSH
    tcp dport 22 accept

    # Nginx
    #tcp dport { 80, 443 } accept
  }

  chain forward {

    type filter hook forward priority 0; policy drop;

    ct status dnat accept
  }
}

table ip6 default {

  chain input {

    type filter hook input priority 0; policy drop;

    iif lo accept
    ct state established, related accept

    icmpv6 type { nd-nei**or-solicit, nd-router-advert, nd-nei**or-advert } accept

    # ping
    icmpv6 type echo-request limit rate 500/second accept
  }

  chain forward {

    type filter hook forward priority 0; policy drop;

    ct status dnat accept
  }
}

3. vim /etc/nftables.conf

include "/etc/nftables/default.nft"

4. systemctl start nftables
5. systemctl enable nftables
6. systemctl restart nftables
7. 如有报错 systemctl status nftables 检查错误报告

8. 端口转发示例

table ip default {

  chain input {

    type filter hook input priority 0; policy drop;

    iif lo accept
    ct state established, related accept

    # ping
    icmp type echo-request limit rate 500/second accept

    # SSH
    tcp dport 22 accept
  }

  chain forward {

    type filter hook forward priority 0; policy drop;

    ct status dnat accept
  }

  chain prerouting {

    type nat hook prerouting priority -100; policy accept;

    iif eth0 tcp dport 443 dnat to ip 目的地:port 端口
  }

  chain postrouting {

    type nat hook postrouting priority 100; policy accept;

    masquerade
  }
}

完。
发表于 2022-5-23 09:48:22 | 显示全部楼层
ufw语法更直观
发表于 2022-5-23 01:39:56 来自手机 | 显示全部楼层
nftables天下第一!好用且强大,主机资讯帮顶。
发表于 2022-5-23 07:07:59 | 显示全部楼层
nft天下第一了?
发表于 2022-5-23 09:53:27 | 显示全部楼层
之前 iptables 不是白学了,之前写的 iptables 脚本不是不能用了。

debian buster不能启动docker守护进程(dockerd)的解决办法
iptables v1.8.2 (nf_tables): Chain already exists 解决办法
故障原因是Docker用iptables初始化NAT网络,而Debian buster使用 nftables 而不是 iptables,导致dockerd不能正常完成NAT初始化,出错退出。
处理方法是调用update-alternatives强制Debian用iptables而不是nftables。

sudo update-alternatives --set iptables /usr/sbin/iptables-legacy
# for ipv6
sudo update-alternatives --set ip6tables /usr/sbin/ip6tables-legacy
发表于 2022-5-23 10:01:46 | 显示全部楼层
我还是喜欢用firewall-cmd
发表于 2022-5-23 10:13:51 | 显示全部楼层
sRGB 发表于 2022-5-23 09:53
之前 iptables 不是白学了,之前写的 iptables 脚本不是不能用了。

debian buster不能启动docker守护进程 ...

爱用啥就装啥呗,又不是iptables不更新也不兼容了
发表于 2022-5-23 10:15:16 | 显示全部楼层
51Yo 发表于 2022-5-23 10:01
我还是喜欢用firewall-cmd

nftables可以firewalld强大多了
发表于 2022-5-23 10:15:21 | 显示全部楼层
不错 晚上回去了解下
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|全球主机交流论坛

GMT+8, 2026-5-14 21:39 , Processed in 0.074030 second(s), 11 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表