在VPS上面配置了某分布式服务,死活跑不起来,该配置的都配置了,真是见鬼了。日志里面很多:
No route to host
但是,我能ping通,为了排除是程序自身原因,只好使用telnet命令测试是否能够连通。
yum update
yum -y install telnet
telnet x.x.x.x 1111
输出结果:
Trying x.x.x.x...
telnet: connect to address x.x.x.x: No route to host
解决方案:
以下命令已经执行了,端口已经放行了啊,为什么?
iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 1111 -j ACCEPT
网上爬来爬去,终于知道原因了。
错误的:
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [4:512]
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A INPUT -p tcp -m state --state NEW -m tcp --dport 1111 -j ACCEPT
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT
正确的:
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [4:512]
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 1111 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT
结论(全干货,因为真不懂iptables):
端口放行条目,请放在下列条目之前,然后修改后,重启防火墙,一切OK了。
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited