iptables:如何只允许一个 ip 通过特定端口?

Ano*_*345 29 security linux ubuntu iptables

如何在我的 ubuntu 服务器上,在 Iptables 中只允许特定端口上的一个 IP 地址?

谢谢

Cri*_*itu 56

一个班轮:

iptables -I INPUT \! --src 1.2.3.4 -m tcp -p tcp --dport 777 -j DROP  # if it's not 1.2.3.4, drop it
Run Code Online (Sandbox Code Playgroud)

更优雅的解决方案:

iptables -N xxx # create a new chain
iptables -A xxx --src 1.2.3.4 -j ACCEPT  # allow 1.2.3.4
iptables -A xxx --src 1.2.3.5 -j ACCEPT  # allow 1.2.3.5
iptables -A xxx --src 1.2.3.6 -j ACCEPT  # allow 1.2.3.6
iptables -A xxx -j DROP  # drop everyone else
iptables -I INPUT -m tcp -p tcp --dport 777 -j xxx  # use chain xxx for packets coming to TCP port 777
Run Code Online (Sandbox Code Playgroud)

  • 不要忘记您还可以在链中指定源,例如:`--src 1.2.3.4/30` (2认同)
  • @Matthias:`iptables -I xxx --src 7.8.9.10 -j ACCEPT` (2认同)