Chain ‘MASQUERADE‘ does not exist 报错解决

今天在执行 iptables 相关命令时遇到了 Chain 'MASQUERADE' does not exist 的报错,报错信息如下所示:

$ iptables -t nat -A POSTROUTING -s 192.168.122.0/24 ! -d 192.168.122.0/24 -j MASQUERADE
iptables v1.8.2 (nf_tables): Chain 'MASQUERADE' does not exist
Try `iptables -h' or 'iptables --help' for more information.

请教同事后才知道使用的 iptables 版本不对。

$ iptables -t nat -L
# Warning: iptables-legacy tables present, use iptables-legacy to see them
iptables: Operation not supported.

$ which iptables
/usr/sbin/iptables

$ ls -l /usr/sbin/iptables
lrwxrwxrwx 1 root root 26 2月 14 2019 /usr/sbin/iptables -> /etc/alternatives/iptables

在同事的提示下重新配置 iptables 的版本,命令如下所示。其中,update-alternatives 命令用于处理 linux 系统中软件版本的切换。

$ update-alternatives --config iptables
有 2 个候选项可用于替换 iptables (提供 /usr/sbin/iptables)。

选择         路径                 优先级    状态
------------------------------------------------------------
* 0 /usr/sbin/iptables-nft        20     自动模式
  1 /usr/sbin/iptables-legacy     10     手动模式
  2 /usr/sbin/iptables-nft        20     手动模式

要维持当前值[*]请按<回车键>,或者键入选择的编号:1
update-alternatives: 使用 /usr/sbin/iptables-legacy 来在手动模式中提供 /usr/sbin/iptables (iptables)

确认 iptables 的版本是否更新成功:

$ iptables -t nat -L
Chain PREROUTING (policy ACCEPT)
target prot opt source destination

Chain INPUT (policy ACCEPT)
target prot opt source destination

Chain OUTPUT (policy ACCEPT)
target prot opt source destination

Chain POSTROUTING (policy ACCEPT)
target prot opt source destination

重新执行之前报错的命令,确认执行成功:

$ iptables -t nat -A POSTROUTING -s 192.168.122.0/24 ! -d 192.168.122.0/24 -j MASQUERADE
$

猜你喜欢

转载自blog.csdn.net/choumin/article/details/111935589