修改 SSH 配置文件

nano /etc/ssh/sshd_config

找到 #Port 22 这一行,取消注释并修改为:

Port 2026

重启ssh服务,发现端口修改不生效。依然还是22端口
注意:Ubuntu 24.04,除了修改默认的ssh配置文件之外,还需要修改SSH Socket配置。

修改 SSH Socket 配置文件

nano /lib/systemd/system/ssh.socket 

修改端口

[Socket]
ListenStream=2025

重启服务

sudo systemctl daemon-reload
sudo systemctl restart ssh.socket

依然没生效

需要先临时停止ssh.socket

systemctl stop ssh.socket

再次重启ssh

systemctl restart ssh

查看监听端口

netstat -anpt|grep 2026

输出:
tcp6 0 0 :::2025 :::* LISTEN 1/systemd
tcp6 0 88 172.16.0.72:2025 8.8.8.8 ESTABLISHED 3941929/sshd: root@

同时还需要关闭iptabls 或者 firewalld !

#停止firewall
systemctl stop firewalld.service
#禁止firewall开机启动
systemctl disable firewalld.service
#关闭iptables
service iptables stop
#去掉iptables开机启动
chkconfig iptables off

或者开放所有端口!

sudo iptables -P INPUT ACCEPT
sudo iptables -P FORWARD ACCEPT
sudo iptables -P OUTPUT ACCEPT
sudo iptables -F

或者卸载iptables
停止iptables服务sudo systemctl stop netfilter-persistent
卸载iptables软件包:sudo apt-get remove --purge iptables
清理残留配置文件:sudo apt-get autoremove&sudo apt-get autoclean
验证卸载:sudo iptables -L