合 如何修改ssh端口及添加使用备用端口连接ssh服务
Tags: OSssh修改端口号Connection refusedpacket_write_wait备用端口
简介
SSH(Secure Shell)是一种安全的远程登录协议,它允许您通过网络远程连接到Linux系统并进行管理操作。默认情况下,SSH使用22端口进行通信。然而,为了增强系统的安全性,有时候我们需要更改SSH端口,以减少潜在的攻击。
若22端口被封,则不能使用ssh连接,会卡住,最后报错:
1 2 3 | debug3: set_sock_tos: set socket 3 IP_TOS 0x48 debug1: connect to address 168.18.0.85 port 22: Connection timed out ssh: connect to host mdw port 22: Connection timed out |
若22端口的网络有限制,则也不建议使用22端口进行scp连接,类似报错packet_write_wait: Connection to 192.21.169.16 port 22: Broken pipe:
1 2 3 | [gpadmin@mdw2 ~]$ scp nohup.out smdw:/tmp/ nohup.out 0% 0 0.0KB/s --:-- ETApacket_write_wait: Connection to 192.21.169.16 port 22: Broken pipe lost connection |
或者网速会进行成倍的降低,从最初的150MB/s降低到最后的0KB/s.
不能启用备用端口案例
在修改 /etc/ssh/sshd_config文件后,并重启后,发现并不能启用新的2222备用端口:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | [root@smdw ~]# systemctl status sshd ● sshd.service - SYSV: OpenSSH server daemon Loaded: loaded (/etc/rc.d/init.d/sshd; bad; vendor preset: enabled) Active: active (running) since Fri 2024-01-19 14:20:36 CST; 5s ago Docs: man:systemd-sysv-generator(8) Process: 17015 ExecStop=/etc/rc.d/init.d/sshd stop (code=exited, status=0/SUCCESS) Process: 17022 ExecStart=/etc/rc.d/init.d/sshd start (code=exited, status=0/SUCCESS) Main PID: 17030 (sshd) CGroup: /system.slice/sshd.service ├─12148 sshd: gpadmin [priv] ├─12150 sshd: gpadmin@pts/0 ├─12151 -bash ├─14275 sshd: gpadmin [priv] ├─14277 sshd: gpadmin@notty ├─14278 scp -v -t /tmp/ ├─15592 sshd: gpadmin [priv] ├─15594 sshd: gpadmin@notty ├─15595 scp -t /tmp/ ├─16335 sshd: gpadmin [priv] ├─16337 sshd: gpadmin@notty ├─16338 scp -t /tmp/ ├─16972 sshd: gpadmin [priv] ├─16974 sshd: gpadmin@pts/1 ├─16975 -bash ├─17030 sshd: /usr/sbin/sshd [listener] 0 of 10-100 startups Jan 19 14:20:36 smdw systemd[1]: Stopped SYSV: OpenSSH server daemon. Jan 19 14:20:36 smdw systemd[1]: Starting SYSV: OpenSSH server daemon... Jan 19 14:20:36 smdw sshd[17030]: Server listening on 0.0.0.0 port 22. Jan 19 14:20:36 smdw sshd[17030]: Server listening on :: port 22. Jan 19 14:20:36 smdw sshd[17022]: Starting sshd:[ OK ] Jan 19 14:20:36 smdw systemd[1]: Started SYSV: OpenSSH server daemon. [root@smdw ~]# netstat -tulnp | grep 22 tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 17030/sshd: /usr/sb tcp6 0 0 :::22 :::* LISTEN 17030/sshd: /usr/sb |
从 systemctl status sshd
中可以看到服务是/etc/rc.d/init.d/sshd
,而正常情况应该是/usr/lib/systemd/system/sshd.service
1 2 3 4 5 6 | [gpadmin@sdw1 logs]$ rpm -qf /usr/lib/systemd/system/sshd.service openssh-server-7.4p1-16.el7.x86_64 [gpadmin@sdw1 logs]$ rpm -qf /usr/sbin/sshd openssh-server-7.4p1-16.el7.x86_64 [gpadmin@sdw1 logs]$ |
所以,去有问题的服务器查看,发现并没有安装这个openssh-server包。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | [root@smdw ~]# rpm -qf /usr/sbin/sshd file /usr/sbin/sshd is not owned by any package [root@smdw Packages]# rpm -i openssh-server-7.4p1-16.el7.x86_64.rpm --nodeps warning: openssh-server-7.4p1-16.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY warning: /etc/ssh/sshd_config created as /etc/ssh/sshd_config.rpmnew [root@smdw Packages]# rpm -qa | grep openssh-server openssh-server-7.4p1-16.el7.x86_64 [root@smdw Packages]# systemctl restart sshd [root@smdw Packages]# systemctl status sshd ● sshd.service - OpenSSH server daemon Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled) Active: active (running) since Fri 2024-01-19 14:31:17 CST; 5s ago Docs: man:sshd(8) man:sshd_config(5) Main PID: 17431 (sshd) CGroup: /system.slice/sshd.service ├─12148 sshd: gpadmin [priv] ├─12150 sshd: gpadmin@pts/0 ├─12151 -bash ├─14275 sshd: gpadmin [priv] ├─14277 sshd: gpadmin@notty ├─14278 scp -v -t /tmp/ ├─15592 sshd: gpadmin [priv] ├─15594 sshd: gpadmin@notty ├─15595 scp -t /tmp/ ├─16821 /usr/local/greenplum-cc-6.10.0/bin/ccagent -udpport 9898 -rpcaddr mdw2:8899 masterport5432e ├─16972 sshd: gpadmin [priv] ├─16974 sshd: gpadmin@pts/1 ├─16975 -bash ├─17431 /usr/sbin/sshd -D Jan 19 14:31:17 smdw systemd[1]: Starting OpenSSH server daemon... Jan 19 14:31:17 smdw sshd[17431]: WARNING: 'UsePAM no' is not supported in Red Hat Enterprise Linux and may cause several problems. Jan 19 14:31:17 smdw sshd[17431]: Server listening on 0.0.0.0 port 3322. Jan 19 14:31:17 smdw sshd[17431]: Server listening on 0.0.0.0 port 22. Jan 19 14:31:17 smdw systemd[1]: Started OpenSSH server daemon. [root@smdw Packages]# netstat -tulnp | grep 22 tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 17431/sshd tcp 0 0 0.0.0.0:2222 0.0.0.0:* LISTEN 17431/sshd |