【问题标题】:Unable to connect to any port other than 22 (ssh) AWS无法连接到 22 (ssh) AWS 以外的任何端口
【发布时间】:2016-04-17 11:51:05
【问题描述】:

我在 AWS 上有一个 ubuntu 实例,我在服务器上运行 mongod。 mongod 已将绑定 ip 设置为 0.0.0.0 并使用 netstat 进行验证。

sudo netstat -nltp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1055/sshd
tcp        0      0 0.0.0.0:27017           0.0.0.0:*               LISTEN      1049/mongod
tcp6       0      0 :::22                   :::*                    LISTEN      1055/sshd

我还在 AWS EC2 控制台上设置了安全组规则,以便允许来自我的公共 ip 在端口 27017 上的入站连接。我无法从我的笔记本电脑连接到 mongod (27017)。

telnet ec2-<ip-removed>.compute-1.amazonaws.com 27017                    1 ↵
Trying <ip-removed>...
telnet: connect to address <ip-removed>: Operation timed out

但是我从笔记本电脑连接到端口 22 上的 ssh 没有问题

telnet ec2-<ip-removed>.compute-1.amazonaws.com 22                    130 ↵
Trying <ip-removed>...
Connected to ec2-<ip-removed>.compute-1.amazonaws.com.

此设置在一周前运行良好,但突然我的笔记本电脑客户端拒绝连接到 mongo。我在端口 6800 上尝试了另一个服务,设置了入站规则并将绑定 ip 设置为 0.0.0.0/32,它也无法连接。我没有设置其他规则,实例上也没有 iptables 或防火墙。

我也尝试过重启服务器,但没有成功。

也可以从服务器本身连接到 mongo

ubuntu@aws$ telnet localhost 27017
Trying 127.0.0.1...
Connected to localhost.

【问题讨论】:

  • 为了记录你不需要做telnet ec2-&lt;ip-removed&gt;.compute-1.amazonaws.com你可以做telnet &lt;ip-removed&gt;。您的外部 IP 地址是否更改或与实例解除关联?
  • 感谢您的提示 :) 。不,我的 ip 没有改变,它是静态的。我也可以在 22 上通过 ssh 连接,但没有其他端口可以连接:(
  • 必须是防火墙问题...你可以通过iptables -L -vn检查iptables是否正在运行,如果它列出了任何规则,它正在运行。如果它正在运行,那么您可以使用/etc/init.d/iptables stop 停止它。并加倍确保您没有任何 AWS 安全策略。
  • 嗯,mongod 没有绑定到 IPv6 ip,但是 sshd 是,我想知道这是否是问题所在。
  • 访问成功了吗?

标签: linux mongodb amazon-web-services ssh amazon-ec2


【解决方案1】:

如 cmets 中所述,来自外部网络的端口访问被实例上运行的 iptables 阻止。

我对 iptables/AWS 访问策略的方法是始终使用“开放”AWS 访问策略并使用 iptables 锁定所有内容。这样一来,控制访问就更接近于实例,并且不会通过将访问策略相互叠加而使事情变得过于复杂。

另外,您的 mongod 实例甚至不应该位于 DMZ(可公开访问)中,您应该将 iptables 设置为仅允许从内部网络访问 mongod 实例。您可能有一个需要访问 mongod 的“应用程序服务器”,因此请使应用程序服务器可公开访问,而不是 mongod 实例。

这是在我的“docstore”实例之一上运行的 iptables 示例:

root@service-a-2.sn3.vpc3.example.com ~ # -> iptables -L -vn
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
  pkts bytes target     prot opt in     out     source               destination
  1419   15M ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0           /* 001 accept all to lo interface */
  27M  4531M ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0           /* 005 accept related established rules */ state RELATED,ESTABLISHED
  0        0 ACCEPT     icmp --  eth0   *       10.3.0.0/16          0.0.0.0/0           /* 015 accept internal icmp */ state NEW
 74     4440 ACCEPT     tcp  --  eth0   *       10.3.0.0/16          0.0.0.0/0           multiport dports 22 /* 777 accepts ssh */ state NEW
 420K    25M ACCEPT     tcp  --  eth0   *       10.3.0.0/16          0.0.0.0/0           multiport dports 27017 /* 777 allow mongo access */ state NEW
 462K    28M ACCEPT     tcp  --  eth0   *       10.3.0.0/16          0.0.0.0/0           multiport dports 3306 /* 777 allow mysql access */ state NEW
  656  28976 DROP       all  --  *      *       0.0.0.0/0            0.0.0.0/0           /* 999 drop all INPUT */

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
  pkts bytes target     prot opt in     out     source               destination

Chain OUTPUT (policy ACCEPT 27M packets, 9639M bytes)
  pkts bytes target     prot opt in     out     source               destination

请注意,我使用 CIDR(无类域间路由)块来识别内部网络,这样您就不必指定实际的 IP 地址。

【讨论】:

    【解决方案2】:

    默认情况下,MongoDB 只绑定到 localhost。您已更新其配置以启用外部访问。

    2.6 版新功能:从官方 .deb 和 .rpm 安装 mongod 默认情况下,包的 bind_ip 配置设置为 127.0.0.1。

    来源:https://docs.mongodb.org/manual/administration/configuration/#security-considerations

    这里有一个说明如何操作的教程:http://www.mkyong.com/mongodb/mongodb-allow-remote-access/

    【讨论】:

    • 0.0.0.0:27017 表示它已绑定到所有可用的 ip,因此如果他确实有绑定到该实例的弹性 ip,则 mongod 也应该绑定到它。
    猜你喜欢
    • 1970-01-01
    • 2022-01-01
    • 2020-08-10
    • 2017-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多