关于SSHD的端口维护(改变及增加侦听端口)

时间:2022-09-30 11:58:25


常见的有两种维护需求

1. 改变SSHD的侦听端口

vim /etc/ssh/sshd_config

Port 2222
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::

效果

[root@server ~]# systemctl restart sshd
[root@server ~]# netstat -ntpl | grep sshd
tcp 0 0 0.0.0.0:2222 0.0.0.0:* LISTEN 1620/sshd
tcp6 0 0 :::2222 :::* LISTEN 1620/sshd

2. 增加和改变SSHD的侦听端口

vim /etc/ssh/sshd_config

#Port 22
#AddressFamily any
#ListenAddress 0.0.0.0
ListenAddress 0.0.0.0:2201
ListenAddress 0.0.0.0:2202
#ListenAddress ::

效果

[root@server ~]# systemctl restart sshd
[root@server ~]# netstat -ntpl | grep sshd
tcp 0 0 0.0.0.0:2201 0.0.0.0:* LISTEN 1666/sshd
tcp 0 0 0.0.0.0:2202 0.0.0.0:* LISTEN 1666/sshd

原来已经建立的ssh连接不会断开

在完成systemctl restart sshd重启后,配置文件sshd_config中原来配置的老端口如果已经建立了连接,那么该连接不会立即断开。

[root@server ~]# netstat -ntpa | grep sshd
tcp 0 0 0.0.0.0:2222 0.0.0.0:* LISTEN 1620/sshd
tcp 0 36 192.168.74.141:22 192.168.74.1:8602 ESTABLISHED 1571/sshd: root@pts
tcp6 0 0 :::2222 :::* LISTEN 1620/sshd

修改配置文件sshd_config将Port改为2222,但修改前的Port 22已经建立了连接,则该连接在重启sshd后会依然保持连接。