萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 作業系統 >  >> Linux 教學

SSH 連線緩慢 10-30 秒?關閉 GSSAPIAuthentication 或修正 DNS 反解析加速登入

區域網路 SSH 連線雖通但需等待 10-30 秒才出現密碼提示。ssh -v 除錯顯示卡在 gssapi-with-mic 認證階段,提示 Cannot determine realm for numeric host address。本文提供兩種根治方案。

問題根因分析

debug1: Next authentication method: gssapi-with-mic
debug1: An invalid name was supplied
debug1: Cannot determine realm for numeric host address

Client 端預設啟用 GSSAPIAuthentication yes,嘗試 Kerberos/GSSAPI 認證。以 IP 直連時需反解析 IP → 主機名 → Kerberos Realm,DNS 無 PTR 記錄導致長時間逾時重試。

方案一:Client 端關閉 GSSAPI 認證 (立即生效、無副作用)

# 編輯全域客戶端配置
sudo vim /etc/ssh/ssh_config
# 找到或新增:
GSSAPIAuthentication no
# 儲存離開,下次 ssh 即時生效

或使用者級配置 ~/.ssh/config:


Host *
    GSSAPIAuthentication no

方案二:修正 DNS 反解析 (根治、需網管配合)

  1. 本機 hosts 快速修正:
    echo "192.168.12.16 ourdev" >> /etc/hosts
    
  2. DNS 伺服器新增 PTR 記錄 (網管操作):
    ; 反解區域檔
    16.12.168.192.in-addr.arpa. IN PTR ourdev.example.com.
    
  3. 驗證反解:
    dig -x 192.168.12.16 +short
    # ourdev.example.com.
    

無效方案:修改 Server 端 UseDNS

網路常見建議修改 Server /etc/ssh/sshd_config:

UseDNS no

實測對 Client 端 GSSAPI 逾時無效,UseDNS 僅控制 Server 端是否對 Client IP 做反解析記錄日誌/權限檢查,不影響 Client 發起的 GSSAPI 流程。

驗證修復效果

ssh -v 192.168.12.16
# debug1: Authentications that can continue: publickey,password
# debug1: Next authentication method: publickey
# debug1: Next authentication method: password
# password: (立即出現,無延遲)

SSH 緩慢除錯示意

完整 ssh_config 建議配置


Host *
    GSSAPIAuthentication no
    ForwardAgent no
    ForwardX11 no
    ServerAliveInterval 60
    ServerAliveCountMax 3
    StrictHostKeyChecking accept-new
    UserKnownHostsFile ~/.ssh/known_hosts
    Compression yes
    ControlMaster auto
    ControlPath ~/.ssh/cm-%r@%h:%p
    ControlPersist 10m

補充:Server 端優化


# /etc/ssh/sshd_config
UseDNS no
GSSAPIAuthentication no
ClientAliveInterval 60
ClientAliveCountMax 3
MaxAuthTries 3
LoginGraceTime 30
PermitRootLogin prohibit-password
PasswordAuthentication no
PubkeyAuthentication yes

關閉 Client 端 GSSAPIAuthentication 是解決 SSH 連線緩慢最直接、相容性最高的方案;配合 DNS 反解析完善,從根源消除 Kerberos Realm 解析失敗導致的逾時等待。

copyright © 萬盛學電腦網 all rights reserved