如何在一个 iptables 命令行中添加多个 IP?须藤 iptables -A INPUT -p tcp --dport 22 !-s 1.2.3.4 -j 删除

rm *_*-rf 4 linux iptables

更新:

我试过了sudo iptables -A INPUT -p tcp --dport 22 ! -s 1.2.3.4,11.22.33.44 -j DROP,不工作,它给了我一个错误

iptables v1.8.2 (nf_tables):!不允许有多个源或目标 IP 地址


例如,我的目标是只允许允许1.2.3.411.22.33.44连接我服务器的 ssh(端口 22)。

我更喜欢这种方法/命令而不是其他方法。

sudo iptables -A INPUT -p tcp --dport 22 ! -s 1.2.3.4 -j DROP

但我不知道如何11.22.33.44在此命令中添加另一个允许的 IP ,

你能给我一点帮助或提示吗?

谢谢。

Tha*_*Guy 5

您可以使用IP 集

根据您的发行版,您可能需要先安装该ipset实用程序。

#(For Debian and Debian-derived distros)
sudo apt install ipset-persistent
Run Code Online (Sandbox Code Playgroud)

然后创建一个具有友好名称的集合。

sudo ipset create ssh_friends iphash
Run Code Online (Sandbox Code Playgroud)

一旦你有了一个集合,你就可以向它添加 IP。

sudo ipset add ssh_friends 1.2.3.4
sudo ipset add ssh_friends 11.22.33.44
Run Code Online (Sandbox Code Playgroud)

现在您可以使用您在iptables规则中创建的集合而不是单个 IP。

sudo iptables -A INPUT -p tcp --destination-port 22 -m set ! --match-set ssh_friends src -j DROP
Run Code Online (Sandbox Code Playgroud)