我们有一个Linux服务器(CentOS 6.3),其中所有端口似乎都可以从内部访问(当从服务器尝试时),但是只有SSH可以从外部访问。 我想允许一些其他的端口,如1521(甲骨文),但我不能得到它的工作。
我尝试了以下内容:
iptables -A INPUT -m state --state NEW -p tcp --dport 1521 -j ACCEPT service iptables save service iptables restart
但是当我从另一台机器上执行“telnet 192.168.97.1 1521”的时候,我仍然得到“连接超时”,而我可以用相同的命令从服务器连接。
这是我在/ etc / sysconfig / iptables中所拥有的:
# Generated by iptables-save v1.4.7 on Fri Mar 15 12:13:41 2013 *nat :PREROUTING ACCEPT [6:1136] :POSTROUTING ACCEPT [14:878] :OUTPUT ACCEPT [15:986] -A POSTROUTING -o em1 -j MASQUERADE COMMIT # Completed on Fri Mar 15 12:13:41 2013 # Generated by iptables-save v1.4.7 on Fri Mar 15 12:13:41 2013 *filter :INPUT ACCEPT [0:0] :FORWARD ACCEPT [0:0] :OUTPUT ACCEPT [45:3812] -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 1521 -j ACCEPT -A FORWARD -j REJECT --reject-with icmp-host-prohibited COMMIT # Completed on Fri Mar 15 12:13:41 2013
(该行-A POSTROUTING -o em1 -j MASQUERADE在那里,因为以前我也尝试安装一个PPTP服务器,如下所述)
指令出现的顺序非常重要。 第一场比赛胜利。
所以你的问题是:
-A INPUT -j REJECT --reject-with icmp-host-prohibited -A INPUT -p tcp -m state --state NEW -m tcp --dport 1521 -j ACCEPT
因此,你永远不会允许交通1521端口。
要解决这个问题,只需将两条线颠倒过来。
您可以使用以下两个选项:(我假设您使用Red Hat或CentOS Linux)。
选项1:
在根shell中执行命令:
iptables -L --line-numbers
这将向您显示在每个链中使用行号应用的链条和规则。 例如,在我的一台服务器中,输出是:
iptables -L --line-numbers Chain INPUT (policy ACCEPT) num target prot opt source destination 1 RH-Firewall-1-INPUT all -- anywhere anywhere Chain FORWARD (policy ACCEPT) num target prot opt source destination 1 RH-Firewall-1-INPUT all -- anywhere anywhere Chain OUTPUT (policy ACCEPT) num target prot opt source destination Chain RH-Firewall-1-INPUT (2 references) num target prot opt source destination 1 ACCEPT all -- anywhere anywhere 2 ACCEPT icmp -- anywhere anywhere icmp any 3 ACCEPT esp -- anywhere anywhere 4 ACCEPT ah -- anywhere anywhere 5 ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED 6 ACCEPT udp -- anywhere anywhere state NEW udp dpt:snmp .... 14 DROP all -- anywhere anywhere
所以,为了build立你的规则,我会执行下面的命令:
iptables -I 7 INPUT -m state --state NEW -p tcp --dport 1521 -j ACCEPT service iptables save service iptables restart
使用-I 7而不是-AI命令iptables在第7行添加新规则,并推下所有其他规则。 请注意,在我的情况下,我可以使用7-14的任何行号。
选项2:
编辑文件/ etc / sysconfig / iptables ,find“DROP”之前的最后一个条目,并在那里添加规则,保存文件并重新加载iptables。