无法使用iptables在Ubuntu上打开端口3306

我试图在我的Ubuntu 12.04服务器机器上打开端口3306(用于远程mysql连接),但对于我的生活无法得到该死的东西的工作!

这是我做的:

1)列出当前的防火墙规则:

$> sudo iptables -nL -v output: Chain INPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 225 16984 fail2ban-ssh tcp -- * * 0.0.0.0/0 0.0.0.0/0 multiport dports 22 220 69605 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0 0 0 REJECT all -- lo * 0.0.0.0/0 127.0.0.0/8 reject-with icmp-port-unreachable 486 54824 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED 1 60 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:80 19 988 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:443 1 52 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22 0 0 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0 icmptype 8 4 208 LOG all -- * * 0.0.0.0/0 0.0.0.0/0 limit: avg 5/min burst 5 LOG flags 0 level 7 prefix "iptables denied: " 4 208 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-port-unreachable Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 0 0 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-port-unreachable Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 735 182K ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 Chain fail2ban-ssh (1 references) pkts bytes target prot opt in out source destination 225 16984 RETURN all -- * * 0.0.0.0/0 0.0.0.0/0 

2)尝试从远程机器连接:

 $> mysql -u root -p -h xxxx output: timeout.... failed to connect 

3)尝试添加一个新的规则到iptables:

 iptables -A INPUT -i eth0 -p tcp -m tcp --dport 3306 -j ACCEPT 

4)确保添加新规则:

 $> sudo iptables -nL -v output: Chain INPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 359 25972 fail2ban-ssh tcp -- * * 0.0.0.0/0 0.0.0.0/0 multiport dports 22 251 78665 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0 0 0 REJECT all -- lo * 0.0.0.0/0 127.0.0.0/8 reject-with icmp-port-unreachable 628 64420 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED 1 60 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:80 19 988 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:443 1 52 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22 0 0 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0 icmptype 8 5 260 LOG all -- * * 0.0.0.0/0 0.0.0.0/0 limit: avg 5/min burst 5 LOG flags 0 level 7 prefix "iptables denied: " 5 260 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-port-unreachable 0 0 ACCEPT tcp -- eth0 * 0.0.0.0/0 0.0.0.0/0 tcp dpt:3306 Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 0 0 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-port-unreachable Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 919 213K ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 Chain fail2ban-ssh (1 references) pkts bytes target prot opt in out source destination 359 25972 RETURN all -- * * 0.0.0.0/0 0.0.0.0/0 

这似乎是这种情况(“链input”部分的最后一行)。

5)尝试再次从远程机器连接:

 $> mysql -u root -p -h xxxx output: timeout.... failed to connect 

这是再次失败。

6)尝试冲洗所有规则:

 $> sudo iptables -F 

7)这次我可以连接。

8)重新启动服务器,并尝试连接,失败。

我怀疑,由于新的规则被附加到最后,它将不起作用,因为在它之前似乎有一个“拒绝所有”的规则。 如果是这种情况,如何确保新规则以正确的顺序添加? 否则,我错过了什么?

请帮忙。

是的,你的问题与规则顺序有关。 最后一条规则不会有任何影响,因为它先于拒绝所有规则。

您需要删除最后一条规则或在其之前插入新规则。 不需要添加拒绝所有规则。 您只需要将INPUT链的默认策略更改为DROP即可拒绝任何不允许显式指定的stream量。

要在链中插入一个规则,使用-I选项而不是-A来追加。 你可以看到man iptables的更多细节。

“8)重新启动服务器并尝试连接,FAILURE”

这表明你的更新不是持久的。 你可以使用iptables-restore来解决这个问题。

1) sudo vi /etc/iptables.firewall.rules

2)插入mySql规则:

 # Allow MySQL connections from anywhere. -A INPUT -p tcp --dport 3306 -j ACCEPT 

3)保存文件并重新加载规则:

 sudo iptables-restore < /etc/iptables.firewall.rules 

4)激活新的规则

 sudo iptables -L