1. 程式人生 > >Linux SSH端口更改和SSH遠程連接服務慢原因排查

Linux SSH端口更改和SSH遠程連接服務慢原因排查

linux 7 dom 端口更改 ecdsa sta rmi ppi 導致 ....

Linux SSH端口更改和優化

為什麽需要更改SSH默認連接端口

Windows服務器的默認遠程管理端口是3389,Linux服務器的默認端口是22。如果在公網上,經常會被工具掃,這是不安全的,為了系統安全,需要更改默認的配置。

Linux 6 操作過程

更改配置文件

# cp /etc/ssh/sshd_config /etc/ssh/sshd_config.ori   更改配置前備份
# vi /etc/ssh/sshd_config                            編輯sshd_config
####添加如下內容####
Port 58400
PermitRootLogin no
PermitEmptyPasswords no
UseDNS no
GSSAPIAuthentication no

以上修改完成後,保存退出。

說明??:

參數 說明
Port 指定sshd守護進程監聽的端口號,默認為22。默認在本機的所有網絡接口上監聽,也可以通過ListenAddress指定只在某個特定的接口上監聽。端口範圍:0-65535,不能與已有的服務器端口沖突。
PermitRootLogin 是否允許root登錄。可用值如下:“yes”(默認)表示允許;“no”表示禁止;“without-password”表示禁止使用密碼認證登錄;“forced-commands-only”表示只有在指定了command選項的情況下才允許使用公鑰認證登錄,同時其它認證方法全部被禁止,這個值常用於做遠程備份之類的事情
PermitEmptyPasswords 是否允許密碼為空的用戶遠程登錄。默認為“no”
UseDNS 指定sshd是否應該對遠程主機名進行反向解析,以檢查此主機名是否與其IP地址真實對應。默認值為“yes”。建議改成“no”,否則可能導致ssh連接很慢
GSSAPIAuthentication no 解決Linux之間使用ssh遠程連接慢的問題

重啟sshd

執行如下命令重啟sshd,使配置生效:

# /etc/init.d/sshd reload
Reloading sshd:                                            [  OK  ]
# /etc/init.d/sshd restart
Stopping sshd:                                             [  OK  ]
Starting sshd:                                             [  OK  ]

說明??:reload為平滑重啟,不影響正在ssh連接的其他用戶,因此要好於restart。

Linux 7 操作過程

更改配置文件

如同 Linux 6 操作一樣

重啟sshd

systemctl reload sshd   #平滑重啟
systemctl restart sshd  #重啟sshd

SSH遠程連接服務慢原因排查

連接慢的主要原因是DNS解析導致

處理方法:

在ssh服務端上更改/etc/ssh/sshd_config文件中的配置為如下內容:

UseDNS no
# GSSAPI options
GSSAPIAuthentication no

重啟sshd進程使上述配置生效。

如果上述處理方法作用不大或無效,請進行如下操作:

檢查ssh服務端上/etc/hosts文件中,127.0.0.1對應的主機名是否和uname -n的結果一樣,或者把本機ip和hostname(uname -n結果)加入到/etc/hosts裏。

# uname -n
wtf.datagrand.com
# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.246.171  wtf.datagrand.com

利用ssh-v的調試功能查找慢的原因

具體排查命令如下:

?  ~ ssh -v [email protected]
OpenSSH_7.3p1, LibreSSL 2.4.1
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 20: Applying options for *
debug1: Connecting to 192.168.246.171 [192.168.246.171] port 22.
debug1: Connection established.
debug1: identity file /Users/wtf/.ssh/id_rsa type 1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/wtf/.ssh/id_rsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/wtf/.ssh/id_dsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/wtf/.ssh/id_dsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/wtf/.ssh/id_ecdsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/wtf/.ssh/id_ecdsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/wtf/.ssh/id_ed25519 type -1
.....

然後看下上述步驟具體卡在哪處。。

參考文檔

linux下SSH遠程連接服務慢解決方案

Linux 更改SSH端口

Linux SSH端口更改和SSH遠程連接服務慢原因排查