萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 伺服器教學

CentOS 解決 Putty/SSH 連接緩慢問題:禁用 GSSAPIAuthentication

CentOS 6/7 預設開啟 SSH 服務端的 GSSAPIAuthentication (Kerberos/GSSAPI 認證),導致客戶端連接時嘗試進行 GSSAPI 協商,在無 Kerberos 域環境下造成明顯延遲 (通常 10-30 秒)。本文提供標準解決方案與驗證方法。

現象描述

  • Putty / Xshell / SecureCRT / OpenSSH 客戶端連接 CentOS 緩慢,停留在「正在協商認證」或黑屏等待。
  • 輸入密碼後登入正常,但初始連接建立階段極慢。
  • ssh -v user@host 除錯輸出顯示 debug1: Next authentication method: gssapi-with-mic 並長時間停頓。

根因分析

GSSAPIAuthentication yes 使 SSH 服務端嘗試使用 Kerberos/GSSAPI 進行單點登入 (SSO) 認證。若客戶端未配置 Kerberos 憑證 (大多數場景),協商過程會經歷 DNS 查詢 (尋找 KDC)、超時重試等多輪網路交互,造成延遲。

解決方案:修改 sshd_config 禁用 GSSAPI

# 1. 備份配置
cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak.$(date +%F)

# 2. 編輯配置
vi /etc/ssh/sshd_config

# 找到並修改 (或新增) 以下兩行:
GSSAPIAuthentication no
# 可選:同時禁用 GSSAPI 清理憑證 (若不需要單點登出)
GSSAPICleanupCredentials no

# 3. 重啟 SSH 服務生效
# CentOS 6
service sshd restart
# CentOS 7/8/9
systemctl restart sshd

# 4. 驗證
ssh -v user@host 2>&1 | grep -i gssapi
# 不應再出現 gssapi-with-mic 協商嘗試
# 連接速度應明顯提升 (秒級建立)

客戶端同步優化 (Putty / OpenSSH)

Putty 設定

  • Connection → SSH → Auth → GSSAPI:取消勾選 Attempt GSSAPI authentication (SSH-2 only)。
  • Connection → SSH → Auth → GSSAPI → Allow GSSAPI credential delegation 取消勾選。

OpenSSH 客戶端 (~/.ssh/config)

Host *
    GSSAPIAuthentication no
    GSSAPIDelegateCredentials no
    # 可選:優先使用公鑰認證
    PubkeyAuthentication yes
    PasswordAuthentication yes
    # 連接超時優化
    ConnectTimeout 10
    ServerAliveInterval 60
    ServerAliveCountMax 3

進階:保留 GSSAPI 但加速協商 (Kerberos 環境)

若企業部署了 Kerberos (FreeIPA/AD) 且需 SSO:

  1. 確保客戶端已加入域並獲取 TGT (kinit)。
  2. 配置 /etc/krb5.conf 指向正確 KDC。
  3. DNS 需正確配置 _kerberos._udp SRV 記錄,避免 DNS 查詢超時。
  4. 服務端 sshd_config 保持 GSSAPIAuthentication yes,但確保 UseDNS no (預設) 避免反向解析延遲。

其他導致 SSH 連接緩慢的常見原因

原因排查命令解決
UseDNS yes (反向 DNS 解析慢)grep UseDNS /etc/ssh/sshd_config設定 UseDNS no 並重啟 sshd
DNS 解析失敗 (無有效 nameserver)cat /etc/resolv.conf; time nslookup hostname修正 /etc/resolv.conf 或 /etc/hosts
SELinux 禁止 SSH 讀取金鑰ausearch -m avc -ts recent | grep sshdrestorecon -Rv /etc/ssh; setsebool -P ssh_keysign on
防火牆/安全組阻擋導致重試tcptraceroute host 22放行埠、檢查安全組規則
SSH 金鑰驗證嘗試過多ssh -vvv 觀察客戶端 IdentitiesOnly yes 或指定 -i key
copyright © 萬盛學電腦網 all rights reserved