如何通过 iptables 允许传出连接?

sil*_*npi 21 linux redhat iptables connection

我有两台服务器。第一个程序需要在端口 2194 上与第二个程序通信。

我知道它不起作用,因为当我这样做时:

root@server1 [~]# telnet myserver2.com 2194
Trying 123.123.123.98...
telnet: connect to address 123.123.123.98: Connection timed out
telnet: Unable to connect to remote host: Connection timed out
Run Code Online (Sandbox Code Playgroud)
server1# iptables -L -n

Chain INPUT (policy DROP)
...
...

Chain FORWARD (policy DROP)
target     prot opt source               destination

Chain OUTPUT (policy DROP)
...

Chain LOCALINPUT (1 references)
target     prot opt source               destination
...

Chain LOCALOUTPUT (1 references)
target     prot opt source               destination
...

Chain LOGDROPIN (1 references)
target     prot opt source               destination
DROP       all  --  0.0.0.0/0            0.0.0.0/0

Chain LOGDROPOUT (1 references)
target     prot opt source               destination
DROP       all  --  0.0.0.0/0            0.0.0.0/0
Run Code Online (Sandbox Code Playgroud)

Mas*_*imo 26

要允许在 TCP 端口 2194 上从 server1 到 server2 的传出连接,请在 server1 上使用:

iptables -A OUTPUT -p tcp -d <server2ip> --dport 2194 -j ACCEPT
Run Code Online (Sandbox Code Playgroud)

要允许 TCP 端口 2194 上从 server1 到 server2 的传入连接,请在 server2 上使用:

iptables -A INPUT -p tcp -s <server1ip> --dport 2194 -j ACCEPT
Run Code Online (Sandbox Code Playgroud)

  • 对于那些可能将其读作 L 的人来说,它的减号 I 就像在“i”中一样 (3认同)
  • 尝试使用“-I”而不是“-A”;这使新规则*高于*可能已经存在的所有其他规则。 (2认同)

Dan*_*tta 7

只是一些提示

您正在运行的服务是否仅在本地主机上侦听?跑

netstat -ltn
Run Code Online (Sandbox Code Playgroud)

如果你看到一条这样的线,0.0.0.0:2194那么你就可以了。如果你看到127.0.0.1:2194(或则仅在本地连接监听:::2194::1:2194IPv6地址,分别显示为tcp6线)。

当前的 iptables 规则是什么?

iptables -L
Run Code Online (Sandbox Code Playgroud)

政策是否为 DROP/REJECT(如果不是,则对于所有连锁店都应该如此)?您需要的端口有特定的规则吗?

如果是防火墙问题,那么要么修改违规规则,要么添加类似的规则

iptables -A INPUT -p tcp --dport 2194 -j ACCEPT 
Run Code Online (Sandbox Code Playgroud)

应该可以解决问题(未经测试)

=== 编辑 ===

测试网络问题的一个好工具是tcpdump. 在尝试连接时在两台服务器上运行它并查看数据包的去向。例如在服务器 1 上运行:

tcpdump -i eth0 -n host server2.com
Run Code Online (Sandbox Code Playgroud)

并在服务器 2 上运行:

tcpdump -i eth0 -n host server1.com
Run Code Online (Sandbox Code Playgroud)

然后尝试连接。您应该会在屏幕上看到从源和目标转储的所有 TCP 数据包。有了这些信息,您应该能够查明问题出在哪里。