Linux SSH端口更改和SSH远程连接服务慢原因排查
  TEZNKK3IfmPf 2023年11月14日 19 0

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 root@192.168.246.171
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端口

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月14日 0

暂无评论

推荐阅读
TEZNKK3IfmPf