更改Linux系统中SSH服务的默认端口(通常是22)可以有效提高服务器的安全性。默认端口经常成为攻击者的目标,他们会利用自动化脚本扫描常见端口并进行暴力破解攻击。通过更改SSH端口,可以减少被扫描和攻击的机会,使服务器更难成为攻击的对象。此外,非标准端口可以隐藏SSH服务的存在,从而进一步增强服务器的安全性。虽然这并不是绝对的防护措施,但它确实增加了一个额外的安全层。
Ubuntu
由于 Ubuntu 默认端口全开,因此我们不必配置 iptables 或 ufw 放行新的端口。不过为了保险起见,我们依旧应该先测试新端口是否能正常连接再关闭旧端口。
注意: 由于在Ubuntu 22.10及之后版本中的sshd(SSH服务器)默认从传统的服务激活方式切换到基于套接字的激活方式,导致配置方式发生了一些变化,因此本教程分为了两个版本。更多详细情况请查看 StackExchange 的讨论。
22.10及之后版本
- 禁用套接字激活 (socket-based activation) 并启用传统的服务激活 (service-based activation) 方式,让 sshd 直接处理端口和地址
1
2
| systemctl disable --now ssh.socket
systemctl enable --now ssh.service
|
- 备份 SSH 的配置文件
1
| sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak
|
- 修改配置文件
1
| vim /etc/ssh/sshd_config
|
为了出现不正确的改了SSH端口,导致设备无法正常登陆的情况,我们先保留22端口并添加新的端口,确认无问题后再禁掉22端口
1
2
3
4
5
6
7
8
9
10
11
12
| # The strategy used for options in the default sshd_config shipped with
# OpenSSH is to specify options with their default value where
# possible, but leave them commented. Uncommented options override the
# default value.
Include /etc/ssh/sshd_config.d/*.conf
Port 22
Port 10022
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::
|
- 重启 SSH 服务,查看SSH服务状态,并检查是否已经正常监听新端口
若使用默认的套接字激活 (socket-based activation) 模式,使用如下命令会报错 (更多信息请查看 Reddit 讨论):
因此我们无论是使用套接字激活或传统的服务激活,都应该直接重启 ssh.service
1
2
3
| systemctl restart ssh
systemctl status ssh
ss -tnlp | grep ssh
|
- 使用新端口登陆测试,如果成功则禁用原 SSH 端口
1
2
3
4
5
6
7
8
9
10
11
12
| # The strategy used for options in the default sshd_config shipped with
# OpenSSH is to specify options with their default value where
# possible, but leave them commented. Uncommented options override the
# default value.
Include /etc/ssh/sshd_config.d/*.conf
Port 22
#Port 10022
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::
|
- 重启 SSH 服务
22.10 及更老的版本
- 备份 SSH 的配置文件
1
| sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak
|
- 修改配置文件
1
| vim /etc/ssh/sshd_config
|
为了出现不正确的改了SSH端口,导致设备无法正常登陆的情况,我们先保留22端口并添加新的端口,确认无问题后再禁掉22端口
1
2
3
4
5
6
7
8
9
| # If you want to change the port on a SELinux system, you have to tell
# SELinux about this change.
# semanage port -a -t ssh_port_t -p tcp #PORTNUMBER
#
Port 22
Port 10022
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::
|
然后保存配置。
- 重启 SSH 服务,查看SSH服务状态,并检查是否已经正常监听新端口
1
2
3
| systemctl restart sshd
systemctl status sshd
ss -tnlp | grep ssh
|
- 使用新端口登陆测试,如果成功则禁用原 SSH 端口
1
2
3
4
5
6
7
8
9
| # If you want to change the port on a SELinux system, you have to tell
# SELinux about this change.America/Phoenix
# semanage port -a -t ssh_port_t -p tcp #PORTNUMBER
#
# Port 22
Port 10022
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::
|
- 重启 SSH 服务
CentOS
教程更新于 2021年08月,对于新版本 CentOS 可能已不适用。
- 备份 SSH 的配置文件
1
| sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak
|
- 修改配置文件
1
| vim /etc/ssh/sshd_config
|
为了出现不正确的改了 SSH 端口,导致设备无法正常登陆的情况,我们先保留 22 端口并添加新的端口,确认无问题后再禁掉 22 端口
1
2
3
4
5
6
7
8
9
| # If you want to change the port on a SELinux system, you have to tell
# SELinux about this change.
# semanage port -a -t ssh_port_t -p tcp #PORTNUMBER
#
Port 22
Port 10022
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::
|
然后保存配置。
- SELINUX添加新的端口以供SSH登陆
安装 SEManage
1
| yum -y install policycoreutils-python
|
通过 SEManage 添加新的SSH端口
1
| semanage port -a -t ssh_port_t -p tcp 10022
|
- 防火墙放行新的 SSH 端口
放行新的 SSH 端口
1
| firewall-cmd --permanent --zone=public --add-port=10022/tcp
|
查看防火墙状态
- 重新加载防火墙状态
- 重启 SSH 服务,查看SSH服务状态,并检查是否已经正常监听新端口
1
2
3
| systemctl restart sshd
systemctl status sshd
ss -tnlp | grep ssh
|
- 使用新端口登陆测试,如果成功则禁用原 SSH 端口
1
2
3
4
5
6
7
8
9
| # If you want to change the port on a SELinux system, you have to tell
# SELinux about this change.America/Phoenix
# semanage port -a -t ssh_port_t -p tcp #PORTNUMBER
#
# Port 22
Port 10022
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::
|
- 重启 SSH 服务