购买的tencent云主机,部署了ubuntu 24最新版本操作系统,各种软件都安装个差不多以后想修改ssh服务端口,发现开始掉坑,无论如何你也改不了默认的ssh 22服务端口,首先,你查看sshd服务状态,会发现这个服务根本就没有:
systemctl status sshd
Unit sshd.service could not be found.
正常的ubuntu安装以后肯定是有这个服务的,那直接去/etc/ssh/路径下面去修改sshd_config文件呢?正常一通修改,然后systemctl daemon-reload,systemctl restart ssh ,netstat -an | more 查看新配置的端口号根本不生效,还是坚固的在22端口监听。没办法联系腾讯云的客服,客服小哥要求连上机器去操作,我这着急演示,不敢让客服乱动配置,算了,还是自己研究吧,执行下面的命令:
root@VM-ubuntu:~# find / -name ssh.*
/etc/systemd/system/sockets.target.wants/ssh.socket
/etc/systemd/system/ssh.service.requires
/etc/systemd/system/ssh.service.requires/ssh.socket
/usr/lib/python3/dist-packages/sos/report/plugins/ssh.py
/usr/lib/python3/dist-packages/sos/report/plugins/__pycache__/ssh.cpython-312.pyc
/usr/lib/python3/dist-packages/cryptography/hazmat/primitives/serialization/ssh.py
/usr/lib/python3/dist-packages/cryptography/hazmat/primitives/serialization/__pycache__/ssh.cpython-312.pyc
/usr/lib/systemd/system/ssh.service
/usr/lib/systemd/system/ssh.socket
/usr/share/man/man1/ssh.1.gz
/sys/fs/cgroup/system.slice/ssh.socket
/sys/fs/cgroup/system.slice/ssh.service
/run/systemd/generator/ssh.socket.d
/var/lib/systemd/deb-systemd-helper-enabled/ssh.socket.dsh-also
/var/lib/systemd/deb-systemd-helper-enabled/sockets.target.wants/ssh.socket
/var/lib/systemd/deb-systemd-helper-enabled/ssh.service.requires
/var/lib/systemd/deb-systemd-helper-enabled/ssh.service.requires/ssh.socket
/var/lib/systemd/deb-systemd-helper-enabled/ssh.service.dsh-also
发现有个叫做ssh.socket的服务,打开修改里面的内容变成下面这样:
root@VM-5-35-ubuntu:~# cat /usr/lib/systemd/system/ssh.socket
[Unit]
Description=OpenBSD Secure Shell server socket
Before=sockets.target ssh.service
ConditionPathExists=!/etc/ssh/sshd_not_to_be_run
[Socket]
ListenStream=22
Accept=no
FreeBind=yes
[Install]
WantedBy=sockets.target
RequiredBy=ssh.service
然后再打开/etc/default/ssh这个文件,修改内容如下:
# Default settings for openssh-server. This file is sourced by /bin/sh from
# /etc/init.d/ssh.
# Options to pass to sshd
SSHD_OPTS="-p 22222"
在这里指定以下新的端口号,再打开/etc/ssh/sshd_config修改里面的内容:
Port=22222
然后systemctl daemon-reload,systemctl restart ssh 就可以了,记得放开防火墙
ufw allow 22222/tcp
测试的时候不要断开旧的ssh连接,因为万一有问题后续你就再也连不上去了,再打开一个新的ssh连接去测试,要是修改ssh端口成功就reboot重启确认是否有问题。