ssh 提示Connection closed by * 的解决方案

使用ssh方式连接linux系统时,发现一直上报这个错误,重启了sshd服务之后,只能登陆一次就无法登录:

 # 出现问题的机器执行
 [root@localhost ~]# service sshd restart
 
 # 其他机器链接过去,只能连接一次就接着会报错
 [root@localhost ~]# ssh root@172.17.0.84
 The authenticity of host '172.17.0.84 (172.17.0.84)'can't be established.
 RSA key fingerprint is f7:ce:40:fd:7c:d5:c8:64:2a:b9:bb:00:42:6b:25:9f.
 Are you sure you want to continue connecting (yes/no)? 

再次尝试连接一直报错:

 [root@localhost ~] sshroot@172.17.0.84
 Connection closed by 172.17.0.84 port 22
 [root@localhost ~] sshroot@172.17.0.84
 Connection closed by 172.17.0.84 port 22
 [root@localhost ~] sshroot@172.17.0.84
 Connection closed by 172.17.0.84 port 22

刚开始还以为是端口以及防火墙的问题呢,通过查看和关闭,并没有发现

 [root@localhost ~]# nc -w 1 172.17.0.84 22 < /dev/null && echo "tcp port ok"
 SSH-2.0-OpenSSH_5.3
 tcp port ok
 [root@localhost ~]# service iptables status
 iptables: Firewall is not running.

什么错误,这就要详细的分析了。到底是哪儿出的问题呢?

根据思路来,先看log:从log可以看出,出错的原因很明显,就是加密文件权限有问题了,接下来就去查看和改动文件权限。

 [root@localhost ~]# vim /var/log/messages
 Jan 1511:24:53 localhost kernel: TCP: time wait bucket table overflow
 Jan 1511:24:57 localhost sshd[1740]: error: Could not load host key: /etc/ssh/ssh_host_rsa_key
 Jan 1511:24:57 localhost sshd[1740]: error: Could not load host key: /etc/ssh/ssh_host_dsa_key
 Jan 1511:24:58 localhost kernel: __ratelimit: 14450callbacks suppressed
 Jan 1511:24:58 localhost kernel: TCP: time wait bucket table overflow
 Jan 1511:24:58 localhost kernel: TCP: time wait bucket table overflow
 Jan 1511:24:58 localhost kernel: TCP: time wait bucket table overflow
 Jan 1511:24:58 localhost kernel: TCP: time wait bucket table overflow
 Jan 1511:24:58 localhost kernel: TCP: time wait bucket table overflow
 Jan 1511:24:58 localhost kernel: TCP: time wait bucket table overflow
 Jan 1511:24:58 localhost kernel: TCP: time wait bucket table overflow
 Jan 1511:24:58 localhost kernel: TCP: time wait bucket table overflow
 Jan 1511:24:58 localhost kernel: TCP: time wait bucket table overflow
 Jan 1511:24:58 localhost kernel: TCP: time wait bucket table overflow
 Jan 1511:25:01 localhost sshd[1744]: error: Could not load host key: /etc/ssh/ssh_host_rsa_key
 Jan 1511:25:01 localhost sshd[1744]: error: Could not load host key: /etc/ssh/ssh_host_dsa_key
 Jan 1511:25:03 localhost kernel: __ratelimit: 14573callbacks suppressed
 Jan 1511:25:03 localhost kernel: TCP: time wait bucket table overflow
 Jan 1511:25:03 localhost kernel: TCP: time wait bucket table overflow
 Jan 1511:25:03 localhost kernel: TCP: time wait bucket table overflow
 Jan 1511:25:03 localhost kernel: TCP: time wait bucket table overflow
 Jan 1511:25:03 localhost kernel: TCP: time wait bucket table overflow
 Jan 1511:25:03 localhost kernel: TCP: time wait bucket table overflow
 Jan 1511:25:03 localhost kernel: TCP: time wait bucket table overflow
 Jan 1511:25:03 localhost kernel: TCP: time wait bucket table overflow
 Jan 1511:25:03 localhost kernel: TCP: time wait bucket table overflow
 Jan 1511:25:03 localhost kernel: TCP: time wait bucket table overflow
 Jan 1511:25:05 localhost kernel: atkbd.c: Unknown key pressed (translated set2, code 0x0 on isa0060/serio0).
 Jan 1511:25:05 localhost kernel: atkbd.c: Use 'setkeycodes 00 <keycode>'to makeit known.
 Jan 1511:25:05 localhost kernel: atkbd.c: Unknown key released (translated set2, code 0x0 on isa0060/serio0).
 Jan 1511:25:05 localhost kernel: atkbd.c: Use 'setkeycodes 00 <keycode>'to makeit known.
 Jan 1511:25:08 localhost kernel: __ratelimit: 14688callbacks suppressed

在/etc/ssh目录下查看,的确是文件的问题,不知道什么原因,文件秘钥文件被删除了,按照别的系统的文件拷贝过来即可:

 [root@localhost ssh]# ls -Zd *
 -rw-------. root root system_u:object_r:etc_t:s0       moduli
 -rw-r--r--. root root system_u:object_r:etc_t:s0       ssh_config
 -rw-------root root ?                               sshd_config
 -rw-------root root ?                               ssh_host_dsa_key
 -rw-r--r--root root ?                               ssh_host_dsa_key.pub
 -rw-------root root ?                               ssh_host_key
 -rw-r--r--root root ?                               ssh_host_key.pub
 -rw-------root root ?                               ssh_host_rsa_key
 -rw-r--r--root root ?                               ssh_host_rsa_key.pub

总结:

默认情况下密钥对都在/etc/ssh/目录下,包括不同算法的公钥,私钥

ssh 按以下顺序从以下源获取配置数据:

1、命令行选项

2、用户的配置文件(〜/.ssh/config)

3、系统范围的配置文件(/etc/ssh/ssh_config)

如果文章有任何错误欢迎不吝赐教,其次大家有任何关于运维的疑难杂问,也欢迎和大家一起交流讨论。关于运维学习、分享、交流,笔者开通了微信公众号【运维猫】,感兴趣的朋友可以关注下,欢迎加入,建立属于我们自己的小圈子,一起学运维知识。群主还经营一家猫小铺饰品店,喜欢的小伙伴欢迎????前来下单。

扫描二维码

获取更多精彩

运维猫公众号

有需要技术交流的小伙伴可以加我微信,期待与大家共同成长,本人微信:

扫描二维码

添加私人微信

运维猫博主

扫码加微信

最近有一些星友咨询我知识星球的事,我也想继续在星球上发布更优质的内容供大家学习和探讨。运维猫公众号平台致力于为大家提供免费的学习资源,知识星球主要致力于即将入坑或者已经入坑的运维行业的小伙伴。

点击阅读原文  查看更多精彩内容!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值