gpt4 book ai didi

mongodb - iptables 阻止与 mongodb 的本地连接

转载 作者:可可西里 更新时间:2023-11-01 09:59:13 26 4
gpt4 key购买 nike

我有一个带有 mongodb (2.0.4) 的虚拟机 (Ubuntu 12.04.4 LTS),我想用 iptables 限制它只接受 SSH(输入/输出),而不接受其他任何东西。这就是我的设置脚本设置规则的方式:

#!/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

最佳答案

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

添加这个使其工作:

iptables -A OUTPUT -o lo -j ACCEPT

关于mongodb - iptables 阻止与 mongodb 的本地连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22707899/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com