【问题标题】:iptables blocking local connection to mongodbiptables 阻止本地连接到 mongodb
【发布时间】:2014-04-02 00:11:32
【问题描述】:

我有一个带有 mongodb (2.0.4) 的虚拟机 (Ubuntu 12.04.4 LTS),我想用 iptables 限制它只接受 SSH (in/out) 而没有别的。 这就是我的设置脚本设置规则的样子:

#!/bin/sh

# DROP everything
iptables -F
iptables -X
iptables -P FORWARD DROP
iptables -P INPUT DROP
iptables -P OUTPUT DROP

# input
iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -A INPUT -s 127.0.0.1 -j ACCEPT  # accept all ports for local conns

# output
iptables -A OUTPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -p tcp -m tcp --dport 22 -j ACCEPT  # ssh

但是激活这些规则后,我无法在本地连接到 mongodb。

ubuntu ~ $ mongo
MongoDB shell version: 2.0.4
connecting to: test
Fri Mar 28 09:40:40 Error: couldn't connect to server 127.0.0.1 shell/mongo.js:84
exception: connect failed

没有它们,它工作正常。部署 mongodb 时是否需要考虑任何特殊的防火墙案例?

我尝试安装 mysql,它非常适合本地连接。 SSH 按预期工作(可以从外部和内部连接)。

iptables 规则设置后如下所示:

ubuntu ~ $ sudo iptables -nvL
Chain INPUT (policy DROP 8 packets, 1015 bytes)
 pkts bytes target     prot opt in     out     source               destination         
  449  108K ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
    0     0 ACCEPT     all  --  *      *       127.0.0.1            0.0.0.0/0           
    0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:22
    0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:80
   32  2048 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:443

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

Chain OUTPUT (policy DROP 27 packets, 6712 bytes)
 pkts bytes target     prot opt in     out     source               destination         
  379  175K ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
    0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:22

【问题讨论】:

  • 我注意到,如果我为端口 27017 添加一个 OUTPUT 规则,它就可以工作。问题是为什么?我不想在不了解原因或将其暴露在外的情况下打开它

标签: mongodb iptables


【解决方案1】:

环回 (127.0.0.1) 也必须接受出站流量。

添加它使其工作:

iptables -A OUTPUT -o lo -j ACCEPT

【讨论】:

    【解决方案2】:

    你可能想试试,换行

    iptables -A INPUT -s 127.0.0.1 -j ACCEPT
    

    iptables -A INPUT -i lo -j ACCEPT
    

    【讨论】:

    • 我试过这个,不幸的是和以前一样的问题。这是应用规则时的样子:0 0 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0跨度>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多