SSH 連接時長時間停頓「等待密碼提示」,多由服務端 UseDNS yes 導致的反向 DNS 解析延遲。關閉此選項可顯著提升登錄速度。
UseDNS yes 時,SSH 服務端會:
動態 IP/無 PTR 記錄環境下,此過程會超時等待,造成 10-30 秒延遲。
vi /etc/ssh/sshd_config
# 找到 #UseDNS yes,在其下方添加:
UseDNS no
# CentOS 7 / RHEL 7+
systemctl restart sshd
# CentOS 6 / 舊版
service sshd restart
# 或
/etc/init.d/sshd restart
# 客戶端測試連接時間
time ssh user@server_ip
# 應從 10s+ 降至 1-2s 內
AllowUsers、DenyUsers、PubkeyAuthentication yes、PasswordAuthentication no 等措施# 禁用 GSSAPI 認證(常導致延遲)
GSSAPIAuthentication no
# 縮短登錄超時
LoginGraceTime 30
# 最大認證嘗試
MaxAuthTries 3
# 僅允許密鑰認證
PubkeyAuthentication yes
PasswordAuthentication no
ChallengeResponseAuthentication no
設置 UseDNS no 是 SSH 登錄加速最立竿見影的單項優化,配合密鑰認證、禁用 GSSAPI,可將連接建立時間控制在 1 秒內。