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

關閉 SSH UseDNS 加速登錄連接速度

編輯:Linux 教學

SSH 連接時長時間停頓「等待密碼提示」,多由服務端 UseDNS yes 導致的反向 DNS 解析延遲。關閉此選項可顯著提升登錄速度。

原理

UseDNS yes 時,SSH 服務端會:

  1. 根據客戶端 IP 做 PTR 反向查詢獲取主機名
  2. 對獲取的主機名做 A 記錄正向查詢
  3. 驗證解析 IP 是否與原始 IP 一致

動態 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 內

安全性說明

  • UseDNS 主要防止 IP 欺騙,但依賴 PTR 記錄準確性
  • 生產環境建議配合 AllowUsers、DenyUsers、PubkeyAuthentication yes、PasswordAuthentication no 等措施
  • 內網/受信環境關閉無風險;公網服務器建議綜合評估

其他 SSH 加速優化

# 禁用 GSSAPI 認證(常導致延遲)
GSSAPIAuthentication no

# 縮短登錄超時
LoginGraceTime 30

# 最大認證嘗試
MaxAuthTries 3

# 僅允許密鑰認證
PubkeyAuthentication yes
PasswordAuthentication no
ChallengeResponseAuthentication no

總結

設置 UseDNS no 是 SSH 登錄加速最立竿見影的單項優化,配合密鑰認證、禁用 GSSAPI,可將連接建立時間控制在 1 秒內。

Copyright © Windows教程網 All Rights Reserved