linux ssh 登录响应慢

linux ssh 登录响应慢

一 环境信息

序号 名称 版本
1 操作系统 CentOS Linux release 7.6.1810 (Core)
2 内核 Linux xxs 3.10.0-957.el7.x86_64 #1 SMP Thu Nov 8 23:39:32 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
3 ssh OpenSSH_7.4p1, OpenSSL 1.0.2k-fips 26 Jan 2017

二 问题描述及解决

2.1 问题描述

ssh 登录主机时,需要很长时间才能最终登录。

2.2 解决

2.2.1 一般方案

修改/etc/ssh/sshd_config,然后重启ssh 服务

先备份

cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak.20250526

修改配置:

GSSAPIAuthentication no
GSSAPICleanupCredentials no


UseDNS no

重启ssh 服务

[root@xxs ssh]# systemctl restart sshd
[root@xxs ssh]# systemctl status sshd
● sshd.service - OpenSSH server daemon
   Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)
   Active: active (running) since 一 2025-05-26 09:53:11 CST; 4s ago
     Docs: man:sshd(8)

该方案 能解决80%的ssh 登录慢问题。

2.2.2 深度分析及解决

若以上还不能解决 ,则要深入分析ssh 登录过程,定位在哪一步登录慢

2.2.2.1 详细跟踪ssh 登录

[root@xxs ssh]# ssh -vvv root@10.3.162.35

跟踪日志 定位到在last login 之后卡住了。

2.2.2.2 手工跟踪脚本

ssh 登录后,会加载用户环境变量/root/.bashrc

[root@xxs ~]# bash -x .bashrc

然后看日志发现卡在执行

+ '[' -f /root/anaconda3/etc/profile.d/conda.sh ']'

我们打开/root/.bashrc

# .bashrc

# User specific aliases and functions

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi

# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('/root/anaconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
    eval "$__conda_setup"
else
    if [ -f "/root/anaconda3/etc/profile.d/conda.sh" ]; then
        . "/root/anaconda3/etc/profile.d/conda.sh"
    else
        export PATH="/root/anaconda3/bin:$PATH"
    fi
fi
unset __conda_setup
# <<< conda initialize <<<

把conda 代码块注释,把这块单独放到一个shell 里,等需要用conda 时再手工加载,这样重新登录ssh 就快了。

posted on 2025-06-05 18:49  weiwei2021  阅读(49)  评论(0)    收藏  举报