侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

SSH登录Linux服务器慢或者登陆错误的解决方法分享

linux  /  管理员 发布于 7年前   141

每次PuTTY使用SSH登录到远程的Linux进行管理的时候,远程登录的过程都非常慢——输入完用户名之后,非要等到30秒左右才会出来输入密码的提示。在实际处理问题的时候,特别需要快速响应的时候,这种状况着实让人难以忍受。

但后来具体测试了一下,发现这又并非是每种系统的通病,出现问题的机器主要集中的CentOS上,同样的Debian系统,在远程连接的过程就是健步如飞,丝毫没有卡顿犹豫的感觉。这难道是CentOS的问题?

出于好奇,查看了下两个系统在SSH时的差别
CentOS:

复制代码代码如下:
ssh -v [email protected]
SSH远程登录的时候显示的信息如下:

OpenSSH_6.0p1 Debian-4, OpenSSL 1.0.1e 11 Feb 2013
...Some sensitive information...
debug1: Remote protocol version 2.0, remote software version OpenSSH_5.3
debug1: match: OpenSSH_5.3 pat OpenSSH_5*
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_6.0p1 Debian-4
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client aes128-ctr hmac-md5 none
debug1: kex: client->server aes128-ctr hmac-md5 none
debug1: SSH2_MSG_KEX_DH_GEX_REQUEST(1024<1024<8192) sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_GROUP
debug1: SSH2_MSG_KEX_DH_GEX_INIT sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_REPLY
...Some sensitive information...
debug1: ssh_rsa_verify: signature correct
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: Roaming not allowed by server
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,password
debug1: Next authentication method: gssapi-keyex
debug1: No valid Key exchange context
debug1: Next authentication method: gssapi-with-mic
debug1: Unspecified GSS failure.  Minor code may provide more information
Cannot determine realm for numeric host address
 
debug1: Unspecified GSS failure.  Minor code may provide more information
Cannot determine realm for numeric host address
 
debug1: Unspecified GSS failure.  Minor code may provide more information
 
 
debug1: Unspecified GSS failure.  Minor code may provide more information
Cannot determine realm for numeric host address
 
debug1: Next authentication method: publickey
debug1: Trying private key: /home/mitchellchu/.ssh/id_rsa
debug1: Trying private key: /home/mitchellchu/.ssh/id_dsa
debug1: Trying private key: /home/mitchellchu/.ssh/id_ecdsa
debug1: Next authentication method: password
而Debian使用同样的命令测试的结果为:

OpenSSH_6.0p1 Debian-4, OpenSSL 1.0.1e 11 Feb 2013
...Some sensitive information...
debug1: Remote protocol version 2.0, remote software version OpenSSH_6.0p1 Debian-4
debug1: match: OpenSSH_6.0p1 Debian-4 pat OpenSSH*
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_6.0p1 Debian-4
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client aes128-ctr hmac-md5 none
debug1: kex: client->server aes128-ctr hmac-md5 none
debug1: sending SSH2_MSG_KEX_ECDH_INIT
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
...Some sensitive information...
debug1: ssh_ecdsa_verify: signature correct
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: Roaming not allowed by server
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,password
debug1: Next authentication method: publickey
debug1: Trying private key: /home/mitchellchu/.ssh/id_rsa
debug1: Trying private key: /home/mitchellchu/.ssh/id_dsa
debug1: Trying private key: /home/mitchellchu/.ssh/id_ecdsa
debug1: Next authentication method: password
从上面可以看到,在CentOS中,系统使用了publickey,gssapi-keyex,gssapi-with-mic,和password来进行认证(上面颜色标记行,23行),而Debian此时则使用了Publickey和password两种。在连接CentOS的时候,在23行处花费了相当多的时间。我们在那里开始往下看,就能非常清楚的看到下面的信息:

#下面使用的是GSSAPI-KEYEX来进行验证
debug1: Next authentication method: gssapi-keyex
#但是报错:没有可用的Key来交换信息
debug1: No valid Key exchange context
#系统接着又使用下一个验证方法:GSSAPI-WITH-MIC
debug1: Next authentication method: gssapi-with-mic
#但遗憾的是,GSSAPI-WITH-MIC方法也失败。
#原因:不能确定数字主机地址的域
debug1: Unspecified GSS failure.  Minor code may provide more information
Cannot determine realm for numeric host address
 
debug1: Unspecified GSS failure.  Minor code may provide more information
Cannot determine realm for numeric host address
 
debug1: Unspecified GSS failure.  Minor code may provide more information
 
 
debug1: Unspecified GSS failure.  Minor code may provide more information
Cannot determine realm for numeric host address
# 在尝试几次后,SSH认证终于放弃了这种验证。进入下一个验证:Publickey
debug1: Next authentication method: publickey
除了这个方法还有其他方法么?这个自然是有的,CentOS其实就已经提供给我们一个解决方案了——使用ssh远程登录的时候禁用GSSAPI验证。当然,还有一个问题不得不注意,如果你的机器上启用了UseDNS的话,需要一并关闭,具体可参见最后的说明。

从错误可以看出应该是和主机域相关的问题——应该是无法确认IP对应的域,因此会出现这个问题。GSSAPI主要是基于Kerberos的,因此要解决这个问题也就变得要系统配置有Kerberos,这对于没有Kerberos的筒子们来说,配置个Kerberos就为了解决个登录延时问题,似乎不是个明智的决定——特别是在生产环境中!最小化满足需求才是王道。

下面先放出处理GSSAPI的方法
禁用GSSAPI认证有两个方式:客户端和服务端

1. 客户端禁用
比较简单,影响的只有单个客户端用户,可以用下面的方法实现:

复制代码代码如下:
ssh -o GSSAPIAuthentication=no your-server-username@serverIP
用上面的方法登录远程,即可实现禁用GSSAPIAuthentication。

如果你嫌麻烦,直接配置你ssh客户端的文件/etc/ssh/ssh_config来达到永久解决这个问题:

复制代码代码如下:
vi /etc/ssh/ssh_config
### 找到ssh_config文件里面的GSSAPIAuthentication yes这行
### 修改为GSSAPIAuthentication no
### 保存ssh_config文件并退出
这个修改方法是将所有这个机器上的用户都影响到了,如果你影响面不要那么的广泛,只要在指定的用户上实施禁用GSSAPIAuthentication的话,那么你可以在该用户的目录下,找到.ssh目录,在其下面添加config文件,并在文件内添加上面这句,如果没有这个文件,你也可以直接这么做:

复制代码代码如下:
cat >>~/.ssh/config<<EOF
GSSAPIAuthentication no
EOF
使用cat,直接将输入导出到文件中,这时候,你在使用ssh连接远程的目标主机时,就不会再使用GSSAPI认证了。

上面这些文件是在客户端,不是服务端的。也就是说,要修改这个文件,你的客户端也要是Linux才行。

如果你是在Windows下使用PuTTY这样的客户端工具,就不使用上面这个方法了,PuTTY下可以尝试在连接之前进行设置:

复制代码代码如下:
PuTTY Configuration -> Connection -> SSH -> Auth -> GSSAPI -> (取消勾选)Attempt GSSAPI authentication(SSH-2 only)

如果没有关闭PuTTY的GSSAPIAuthentication,你可以在连接的窗口右键(或:Ctrl + 右键)查看日志,可以发现PuTTY会自动尝试GSSAPI连接的日志:

2014-05-18 23:46:54 Using SSPI from SECUR32.DLL
2014-05-18 23:46:54 Attempting GSSAPI authentication
2014-05-18 23:46:54 GSSAPI authentication request refused
恩,上面基本上将客户端禁止GSSAPIAuthentication的方法罗列了一下。

注意:上面这些方法是比较通用的。

2、如果你已经配置了Kerberos的情况下
那么你也可以尝试下如下的客户端解决这个问题的方法:

添加远程主机的主机名到你本机的host文件中(Linux是/etc/hosts,Windows是系统盘:\Windows\System32\drivers\etc\hosts)。Linux和Windows下都可以添加下面这行。

复制代码代码如下:
### 注意:下面这样的IP-Addr要替换成你的远程机器的IP地址,HostName,自然是主机名
IP-Addr HostName
添加完毕之后,保存退出。

如果你没有配置Kerberos的话,仅配置这个hosts文件一样是不能解决问题的,在使用ssh登录的时候,你可以看到报错日志会类似下面这样:

debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mi
debug1: Next authentication method: gssapi-keyex
debug1: No valid Key exchange context
debug1: Next authentication method: gssapi-with-mic
debug1: Unspecified GSS failure.  Minor code may provide more information
Credentials cache file '/tmp/krb5cc_0' not found
 
debug1: Unspecified GSS failure.  Minor code may provide more information
Credentials cache file '/tmp/krb5cc_0' not found
 
debug1: Unspecified GSS failure.  Minor code may provide more information
 
 
debug1: Unspecified GSS failure.  Minor code may provide more information
Credentials cache file '/tmp/krb5cc_0' not found
 
debug1: Next authentication method: publickey
这个错误我在刚开始的时候也犯了的,需要注意。

3、服务端禁用GSSAPIAuthentication。
直接到/etc/ssh/sshd_config里面,将GSSAPIAuthentication yes改为no即可了,同时也请注意,你可能也需要将UseDNS这个也修改成UseDNS no(这个要注意,每个系统的默认值不同,此处以CentOS 6为例):

复制代码代码如下:
sudo vi /etc/ssh/sshd_config
### 普通用户权限不够,需要root权限
### 找到GSSAPIAuthentication yes,修改为
### GSSAPIAuthentication no
### 注意,这里你也需要将UseDNS修改为no,CentOS默认是yes,即使这行已被注释,你也需要加上
### UseDNS no
### 有看到人说UseDNS yes不需要修改为UseDNS no,Mitchell测试下来是需要的。
### 保存文件,退出
当禁用之后,我们需要重启SSH服务来保证新的配置文件被正确应用:

复制代码代码如下:
service sshd restart
这个时候,再次使用SSH登录这个主机时,是不是感觉飞快了?

呼~ 终于完成了这篇长文,要一边捣腾一边弄出这些个文字,还是真是有点困难。不过,这样也就将问题捣腾的差不多了,希望看文章的你能够看的明白,欢迎讨论。 

说明:
1. GSSAPI:Generic Security Services Application Program Interface,GSSAPI本身是一套API,由IETF标准化。其最主要也是著名的实现是基于Kerberos的。一般说到GSSAPI都暗指Kerberos实现。

2. UseDNS:是OpenSSH服务器上的一个DNS查找选项,而且默认还是打开的,在打开的状态下,每当客户端尝试连接OpenSSH服务器的时候,服务端就自动根据用户客户端的IP进行DNS PTR反向查询(IP反向解析才会有记录),查询出IP对应的Hostname,之后在根据客户端的Hostname进行DNS正向A记录查询。通过这个查询,验证IP是否和连接的客户端IP一致。但绝大部分我们的机器是动态获取IP的,也就是说,这个选项对于这种情况根本就没用——即使是普通静态IP服务器,只要没有做IP反向解析,也难以适用。如果你符合这些情况,建议关闭UseDNS以提高SSH远程登录时候的认证速度。

ssh证书登录错误
错误描述
使用证书ssh链接的时候提示下面错误信息

Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password). 可能原因
authorizedkeys 或.ssh的权限太open .ssh 目录改成755 权限 authorizedkeys 改成600
解决
查看日志: cat /var/log/secure 发现 Aug 8 17:15:13 CentOS62 sshd[5624]: Authentication refused: bad ownership or modes for file /home/abc/.ssh/authorized_keys 查看.ssh权限为775 .ssh 手动创建的时候是775权限,改成755权限后正常 # chmod 755 ~/.ssh


  • 上一条:
    Linux系统下CVS的账号控制操作简介
    下一条:
    Linux系统下Flash Player崩溃的一个解决办法分享
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 在Linux系统中使用Iptables实现流量转发功能流程步骤(0个评论)
    • vim学习笔记-入门级需要了解的一些快捷键(0个评论)
    • 在centos7系统中实现分区并格式化挂载一块硬盘到/data目录流程步骤(0个评论)
    • 在Linux系统种查看某一个进程所占用的内存命令(0个评论)
    • Linux中grep命令中的10种高级用法浅析(0个评论)
    • 近期文章
    • 智能合约Solidity学习CryptoZombie第三课:组建僵尸军队(高级Solidity理论)(0个评论)
    • 智能合约Solidity学习CryptoZombie第二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-11
    • 2017-07
    • 2017-10
    • 2017-11
    • 2018-01
    • 2018-02
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2021-02
    • 2021-03
    • 2021-04
    • 2021-06
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-11
    • 2021-12
    • 2022-01
    • 2022-03
    • 2022-04
    • 2022-08
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-02
    • 2023-03
    • 2023-06
    • 2023-07
    • 2023-10
    • 2023-12
    • 2024-01
    • 2024-04
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客