侧边栏壁纸
博主头像
奇楠木语博主等级

行动起来,活在当下

  • 累计撰写 138 篇文章
  • 累计创建 94 个标签
  • 累计收到 1 条评论

目 录CONTENT

文章目录

debian10 使用 nftables 替换 iptables

Administrator
2019-07-21 / 0 评论 / 0 点赞 / 83 阅读 / 5114 字

Debian 10 Buster 终于发布了 , 第一时间升级后发现默认防火墙变了 , 那就学习一下新版防火墙怎么用吧 . 以下是参考的一些资料 ,

nftables

具体的区别自己去 netfilter 官网查看吧 .
简单说就是扩展性更好 , 速度稍慢 .

不想用 nftables/iptables 怎么办?

版本切换

The default starting with Debian Buster:(默认 nftables)

update-alternatives --set iptables /usr/sbin/iptables-nft
update-alternatives --set ip6tables /usr/sbin/ip6tables-nft
update-alternatives --set arptables /usr/sbin/arptables-nft
update-alternatives --set ebtables /usr/sbin/ebtables-nft

Switching to the legacy version:(切换到 iptables)

update-alternatives --set iptables /usr/sbin/iptables-legacy
update-alternatives --set ip6tables /usr/sbin/ip6tables-legacy
update-alternatives --set arptables /usr/sbin/arptables-legacy
update-alternatives --set ebtables /usr/sbin/ebtables-legacy

ntables 默认配置文件位置 : /etc/nftables.conf

原来的规则怎么办?

转换/翻译 iptables -> nftables

  • 直接翻译
iptables-translate -A INPUT -p tcp --dport 22 -m conntrack --ctstate NEW -j ACCEPT
# nft add rule ip filter INPUT tcp dport 22 ct state new counter accept
  • 文件翻译

#后为注释内容

iptables-save > rules.v4    #导出 iptables 到文件 rules.v4
iptables-restore-translate -f rules.v4 >rules.nft    #翻译 rules.v4 到文件 rules.nft
nft -f rules.nft    #nftables 启用新规则
nft list ruleset    #查看当前规则列表

基本模板

安装后默认会有几个表 , 里边是空的没有规则 . 我都是清空后用原来转换过得规则直接导入就好了 .
ssh 22 端口记得先不要启用 , 免得机器失联了还要开 vnc .

  1. 创建一个表包括几个重要的链
nft add table inet filter
nft add chain inet filter input { type filter hook input priority 0 \; policy drop \; }
nft add chain inet filter forward { type filter hook forward priority 0 \; policy drop \; }
nft add chain inet filter output { type filter hook output priority 0 \; policy accept \; }
nft add chain inet filter TCP
nft add chain inet filter UDP
  1. 编写几条重要的规则
nft add rule inet filter output accept
nft add rule inet filter input ct state related,established accept
nft add rule inet filter input iif lo accept
nft add rule inet filter input ct state invalid drop
nft add rule inet filter input ip protocol icmp icmp type echo-request ct state new accept
nft add rule inet filter input ip protocol udp ct state new jump UDP
nft add rule inet filter input ip protocol tcp tcp flags \& \(fin\|syn\|rst\|ack\) == syn ct state new jump TCP
nft add rule inet filter input ip protocol udp reject
nft add rule inet filter input ip protocol tcp reject with tcp reset
nft add rule inet filter input counter reject with icmp type prot-unreachable

若需要监听80或443端口(其它端口参照这两个例子即可),可以添加一下规则

nft add rule inet filter TCP tcp dport 80 accept
nft add rule inet filter TCP tcp dport 443 accept
  1. nat 表
nft add table nat
nft add chain nat prerouting {type nat hook prerouting priority 0 \;}
nft add chain nat postrouting {type nat hook postrouting priority 100 \;}
nft add rule nat postrouting masquerade

管理语法

补充提示

debian10 切换后 nft flush ruleset 清空规则 , 然后自行编写规则即可 .
待编写完成后直接 nft list ruleset -nnn 查看原端口规则展示 , 确认无误后运行

nft list ruleset -nnn > /etc/nftables.conf
systemd enable nftables && systemd restart nftables && systemd status nftables

这样就能开机启动防火墙了 .

其它

具体可以看这位大佬的博客 : [https://wiki.shileizcc.com/confluence/display/firewall/nftables](https://wiki.shileizcc.com/
confluence/display/firewall/nftables)
如果你有 iptables 运行/编写经验 , nftables 不在话下 , 上手很快 .

update log:
2019-07-24 首次发布.
2019-08-26 增加 debian 开机启动方法 .

0

评论区