Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> 電腦軟體教學 >> 伺服器技術 >> 關於伺服器

CentOS 6/7 SSH 連接緩慢根因與一鍵優化方案

編輯:關於伺服器

故障現象

CentOS 6.5/7.x 系統,使用 PuTTY/Xshell 連接 SSH 時,輸入密碼後需等待 10-30 秒才出現 Shell 提示符,但連接建立後操作正常。

根因分析

SSH 服務端默認啟用 GSSAPIAuthentication(通用安全服務 API 認證)。客戶端連接時,服務端嘗試進行 GSSAPI/Kerberos 認證協商,涉及 DNS 反向解析、域控通信等網絡操作。若網絡環境無 Kerberos 域控、DNS 解析慢或失敗,協商過程超時等待導致登錄明顯延遲。

解決方案

方法一:禁用 GSSAPIAuthentication(推薦,即時生效)


# 編輯 SSH 服務端配置
vi /etc/ssh/sshd_config

# 找到或添加
GSSAPIAuthentication no
# 同時建議禁用客戶端發起的 GSSAPI
GSSAPICleanupCredentials no
# UseDNS no  # 可選:禁用 DNS 反向解析,進一步加速

# 重啟 SSH 服務
systemctl restart sshd
# 或 CentOS 6
service sshd restart

方法二:客戶端配置優化(無服務器權限時)

PuTTY:Connection → SSH → Auth → GSSAPI → 取消勾選「Attempt GSSAPI authentication」

Xshell:屬性 → 連接 → SSH → 認證 → 取消勾選「GSSAPI」

OpenSSH 客戶端 ~/.ssh/config:


Host *
    GSSAPIAuthentication no
    GSSAPIDelegateCredentials no

驗證優化效果


# 客戶端測試連接時間
time ssh -o GSSAPIAuthentication=no user@server 'echo connected'

# 服務端查看認證日誌
grep -i gssapi /var/log/secure
# 禁用前會見到:Failed to acquire credentials, GSSAPI authentication failed
# 禁用後無相關日誌

延伸優化:UseDNS 設置

UseDNS yes(默認)會在登錄時對客戶端 IP 做反向 DNS 解析,再正向驗證。內網環境、無 PTR 記錄場景下建議關閉:


# /etc/ssh/sshd_config
UseDNS no

注意:若依賴基於主機名的 AllowUsers user@host.example.com 匹配,關閉 UseDNS 將導致匹配失效。

完整 SSH 加固配置模板


# /etc/ssh/sshd_config 安全基線
Port 2222                    # 修改默認端口
Protocol 2
PermitRootLogin no           # 禁用 root 直接登錄
PubkeyAuthentication yes     # 僅允許密鑰登錄
PasswordAuthentication no    # 禁用密碼登錄
PermitEmptyPasswords no
MaxAuthTries 3               # 最大嘗試次數
ClientAliveInterval 300      # 保活間隔
ClientAliveCountMax 2
LoginGraceTime 30            # 登錄超時
AllowUsers admin@10.0.0.0/8  # 白名單用戶+IP
DenyUsers *                  # 默認拒絕其他

# 關鍵性能優化
GSSAPIAuthentication no
GSSAPICleanupCredentials no
UseDNS no

# 日誌審計
SyslogFacility AUTH
LogLevel VERBOSE

總結

SSH 連接緩慢 90% 由 GSSAPIAuthentication 導致。服務端設置 GSSAPIAuthentication no 並重啟 sshd,即可將登錄延遲從秒級降至毫秒級。生產環境建議配合密鑰登錄、非標端口、IP 白名單、UseDNS 關閉構建安全高效的 SSH 訪問體系。

Copyright © Windows教程網 All Rights Reserved