修改iptables后SSH连接被拒绝

昨天晚上我正在搞我的iptables做这个特定的命令:

sudo iptables -F sudo iptables -X sudo iptables -P INPUT DROP sudo iptables -P OUTPUT ACCEPT sudo iptables -P FORWARD DROP sudo iptables -A INPUT -i lo -j ACCEPT sudo iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT 

如果我是对的,没有什么真正的危害 然后我的ssh connexion倒下了。 试图重启服务器,但一直在说

 ssh: connect to host XXXX port 22: Connection refused 

我试图启动到救援模式,挂载/ dev / sda2 / mnt,chroot / mnt,只是刷新了iptables。 但仍然没有工作。 看起来没有什么需要外部访问正在工作了…

我试图删除(–purge)openssh-client和服务器,并重新安装,仍然是一样的。

编辑:

所以哈立德提到我试图补充

 iptables -A INPUT -p tcp --dport 22 -j ACCEPT 

我的iptables在救援模式,然后将其保存到/etc/iptables.rules,但它不起作用。 我也尝试在启动时将这些指令添加到/etc/rc.local以刷新iptables:

 #!/bin/sh -e sudo iptables -X sudo iptables -t nat -F sudo iptables -t nat -X sudo iptables -t mangle -F sudo iptables -t mangle -X sudo iptables -P INPUT ACCEPT sudo iptables -P FORWARD ACCEPT sudo iptables -P OUTPUT ACCEPT 

依然不起作用。

当我(仍然处于chrooted救援模式)尝试使用-d选项启动ssh时,这也是结果

 /etc/init.d/ssh start -d [....] Starting OpenBSD Secure Shell server: sshddebug1: sshd version OpenSSH_6.7, OpenSSL 1.0.1k 8 Jan 2015 debug1: private host key: #0 type 1 RSA debug1: private host key: #1 type 2 DSA debug1: private host key: #2 type 3 ECDSA debug1: private host key: #3 type 4 ED25519 debug1: rexec_argv[0]='/usr/sbin/sshd' debug1: rexec_argv[1]='-d' debug1: Bind to port 22 on 0.0.0.0. Bind to port 22 on 0.0.0.0 failed: Address already in use. debug1: Bind to port 22 on ::. Bind to port 22 on :: failed: Address already in use. Cannot bind any address. failed! 

但对我来说这似乎很合理(作为一个大noob),因为我已经在救援模式下使用SSH …

编辑2:

如问,在这里的服务器启动,而不是救援模式下的telnet的结果:

 telnet XXXX 22 Trying XXXX.. telnet: connect to address XXXX: Connection refused telnet: Unable to connect to remote host 

您发布的规则集不允许任何传入连接到服务器。 您只允许在本地通过回送接口和RELATEDESTABLISHED状态进行通信。 这些不足以访问服务器。 您至less需要使用规则lile来允许SSH端口:

 iptables -A INPUT -p tcp --dport 22 -j ACCEPT 

另外需要注意的是你执行这些iptables命令的方式。 您应该一次执行它们作为一个脚本而不是一个命令。 当你input第三个命令sudo iptables -P INPUT DROP你将丢弃所有传入的数据包到你的服务器,并且变得不可访问。 这是因为iptables命令立即生效!

通常情况下,拒绝连接错误是由不监听指定端口的进程或显式iptables REJECTR规则引起的(这里不是这种情况)。 如果您可以访问服务器,则需要确保ssh服务器正在运行并侦听端口22.您可以使用:

 sudo netstat -lnp | grep 22