Ubuntu 22.04 iptables 命令不起作用

Ubuntu 22.04 iptables 命令不起作用

对于 netfilter 来说是全新的,目前正在运行一个使用三个接口 eth0/eth1/eth2 的应用程序,我的应用程序将在两台服务器上运行,它们都可以通过自己的接口(eth0/eth1/eth2)在它们之间进行通信

在ubuntu 18.04(内核版本4.*)中,我只是使用iptables命令来中断它们之间的通信。

在 22.04(内核版本 6.2.*)中,我使用相同的 iptable 命令来中断两个服务器之间的通信,但事情没有按预期工作(我的应用程序代码保持不变)——我的应用程序具有报告邻居服务器是否存在的机制是否可达——在 22.04 中应用了 iptables 规则,它仍然报告其他服务器可达(18.04 中不是这种情况)。

我可以看到关于如何在两个内核版本之间过滤网络流量(最近的版本中有更多工具)发生了很多变化。

我删除了 ufw 只是为了避免与 nftables 发生冲突,一个观察结果是,当我应用该规则时,我的应用程序短暂地报告邻居服务器无法访问,然后突然它将变为可访问,有些东西覆盖了规则,我不确定。

现在在这里寻求帮助,看看我错过了什么......

-A INPUT -s x.x.x.x/32 -d y.y.y.y/32 -i eth2 -j DROP
-A INPUT -s x.x.x.y/32 -d y.y.y.x/32 -i eth1 -j DROP
-A INPUT -s x.x.y.y/32 -d y.y.x.x/32 -i eth0 -j DROP

-A OUTPUT -s y.y.y.y/32 -d x.x.x.x/32 -o eth2 -j DROP
-A OUTPUT -s y.y.y.x/32 -d x.x.x.y/32 -o eth1 -j DROP
-A OUTPUT -s y.y.x.x/32 -d x.x.y.y/32 -o eth0 -j DROP

注意:我的所有规则都放在链中,以确保它们优先于其他任何规则

Chain INPUT (policy DROP)
target     prot opt source               destination
DROP       all  --  xxxx  yyyy
DROP       all  --  zzzz  AAAA
DROP       all  --  BBBB  CCCC

答案1

在 Ubuntu 22.04 上nftables默认使用。

Ubuntu 22.04 LTS 的安全性有哪些新变化?

nftables 作为默认防火墙后端

Linux 上的防火墙由两个组件组成:Linux 内核中的防火墙机制以及用于从用户空间配置防火墙的工具。 Linux 内核传统上支持两种不同的防火墙策略子系统 - iptables / xtables 和较新的 nftables。在创建和部署防火墙规则时,nftables 在性能和灵活性方面都带来了显着的优势,特别是对于双栈 IPv4/IPv6 系统。传统的 iptables 用户空间管理工具现在配置 nftables 内核后端,同时还提供新的 nft 用户空间工具,以允许创建传统 iptables 范例不支持的更灵活的规则。

您可以将iptables规则翻译为nftthroughiptables-translateipt6ables-translate。例如:

iptables-translate -A INPUT -s x.x.x.x/32 -d y.y.y.y/32 -i eth2 -j DROP

有关更多详细信息,请参阅联机帮助页

恢复到 iptables。

使用以下命令查看和更改优先级:

update-alternatives --config iptables 

示例输出:

  Selection    Path                       Priority   Status
------------------------------------------------------------
* 0            /usr/sbin/iptables-nft      20        auto mode
  1            /usr/sbin/iptables-legacy   10        manual mode

将最高优先级设置为iptables

sudo update-alternatives --install /usr/bin/iptables iptables /usr/sbin/iptables-nft 30

设置为默认:

sudo update-alternatives  --set iptables /usr/sbin/iptables-legacy

禁用 UFW 和 nft:

sudo systemctl disable --now ufw 
sudo systemctl disable --now nftables
sudo apt install iptables-persistent
sudo systemctl enable --now iptables

相关内容