解决使用SSH连接Linux服务器时连接失败的故障

最近使用Xshell连接Linux服务器,报错:Connection closed by foreign host. Disconnected from remote host,详细报错信息是:

[C:\~]$ ssh [email protected]


Connecting to 210.35.124.2:22...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.
Connection closing...Socket close.

Connection closed by foreign host.

Disconnected from remote host(210.35.124.82:22) at 08:34:46.

Type `help' to learn how to use Xshell prompt.

通过控制台从本地登录服务器,使用last指令查看登录情况,发现好几个终端依然登着,但是ip地址变化了,好吧,很有可能是网络ip地址变化导致xshell自动断开连接。于是修改本机ip为固定地址发现就不会轻易断了。

root     pts/3        183.186.203.80   Sat Aug 26 09:41   still logged in   
root     pts/2        171.127.72.216   Sat Aug 26 09:33   still logged in   
root     pts/2        118.77.199.116   Sat Aug 26 09:30 - 09:32  (00:01)    
root     pts/1        171.127.203.17   Sat Aug 26 09:05   still logged in   
root     pts/1        118.77.199.229   Fri Aug 25 09:16 - 09:30  (00:14)   

本人修改本机IP为固定地址了,还是不能解决问题。继续经过多次BAIDU和艰苦摸索,终于找到了解决办法:

一、修改配置文件: 

如果本机网络dns变化也会导致xshell自动断开连接,这种情况通过修改ssh配置文件将其中“UseDNS yes”去掉注释并改成no即可,过程如下。

vim /etc/ssh/sshd_config 

将UseDNS yes 改为 no,保存。

UseDNS no

二、重新更改ssh配置目录和文件的权限(为了安全系统配置文件勿随意开放为所有权限) 

cd /etc/ssh

sudo chmod 644 ./*
sudo chmod 600 ssh_host_dsa_key
sudo chmod 600 ssh_host_rsa_key
sudo chmod 755 .

修改完配置记得重新启动ssh

/etc/init.d/sshd restart

三、也有网友把权限设置为如下样子也可以了的

  • 修改ssh权限 
    chmod 400 /etc/ssh/*
  • 重启SSH 
    service sshd restart
  • 重启网关 
    service network restart

四、没有设置超时时间

  相关文章:http://blog.csdn.net/zgl07/article/details/40424637

解决方案是:    

sudo vim /etc/ssh/sshd_config

添加两行:    

ClientAliveInterval 60
ClientAliveCountMax 3

然后

sudo /etc/init.d/sshd restart

猜你喜欢

转载自blog.csdn.net/xieyunc/article/details/81603444