Ubuntu 24.04 SSH port won't change (ssh.socket)
Port is set in sshd_config and ssh is restarted, but port 22 still listens. ssh.socket owns the port. How to check, and the order that keeps you connected.
You added Port 2222 to /etc/ssh/sshd_config and ran systemctl restart ssh, yet ss still shows only port 22. Since Ubuntu 22.10, openssh-server uses systemd socket activation by default, so the unit holding the port is ssh.socket, not sshd. On a server you are logged into remotely, follow the order below or you will lock yourself out.
[1] Find out which mode the server runs in. If ssh.socket is active (listening), it is socket mode. If it is inactive and ssh.service runs directly, as below, it is the classic mode.
# systemctl status ssh.socket ssh.service --no-pager○ ssh.socket - OpenBSD Secure Shell server socket Loaded: loaded (/usr/lib/systemd/system/ssh.socket; disabled; preset: enabled) Active: inactive (dead) Triggers: ● ssh.service Listen: 0.0.0.0:22 (Stream) [::]:22 (Stream) ● ssh.service - OpenBSD Secure Shell server Loaded: loaded (/usr/lib/systemd/system/ssh.service; enabled; preset: enabled) Active: active (running) since Mon 2026-10-19 06:54:08 KST; 6h ago# ss -tlnp 'sport = :22'State Recv-Q Send-Q Local Address:Port Peer Address:Port ProcessLISTEN 0 128 0.0.0.0:22 0.0.0.0:* users:(("sshd",pid=883,fd=3))LISTEN 0 128 [::]:22 [::]:* users:(("sshd",pid=883,fd=4))[2] See which port the socket unit holds. The packaged unit is below; the ListenStream= lines in [Socket] decide the port.
# systemctl cat ssh.socket --no-pager# /usr/lib/systemd/system/ssh.socket[Unit]Description=OpenBSD Secure Shell server socketBefore=sockets.target ssh.serviceConditionPathExists=!/etc/ssh/sshd_not_to_be_run [Socket]ListenStream=0.0.0.0:22ListenStream=[::]:22BindIPv6Only=ipv6-onlyAccept=noFreeBind=yes [Install]WantedBy=sockets.targetRequiredBy=ssh.service[3] Open the new port in the firewall first. If you open it after the socket has moved, new connections are blocked in between. Do the same in your cloud security group. Do not close 22 yet.
# ufw allow 2222/tcp[4] On 24.04 the documented route is editing sshd_config. The sshd-socket-generator reads Port and ListenAddress and builds the ssh.socket settings. systemd runs generators at boot and on daemon-reload, so editing the file and restarting alone leaves the old port. The default sshd_config carries the same note near the top.
# When systemd socket activation is used (the default), the socket# configuration must be re-generated after changing Port, AddressFamily, or# ListenAddress.## For changes to take effect, run:## systemctl daemon-reload# systemctl restart ssh.socket#Port 2222#AddressFamily any#ListenAddress 0.0.0.0#ListenAddress ::Run the commands the note gives. The note was added in 9.6p1-3ubuntu13.12, prompted by the bug titled “Changing Port in sshd_config requires calling systemctl daemon-reload”.
# sshd -t# systemctl daemon-reload# systemctl restart ssh.socket[5] The other route is a drop-in on the socket unit. It is the pre-24.04 method from README.Debian; the 24.04 documentation points to step 4. Which value wins when you use both was not verified, so use one. A bare ListenStream=2222 opens 2222 in addition to 22, so reset the list with an empty line first if you want to drop 22. man systemd.socket says an empty assignment makes all earlier ListenStream= lines void.
[Socket]ListenStream=ListenStream=2222# systemctl daemon-reload# systemctl restart ssh.socket[6] Keep your existing session open and connect on the new port from a new terminal. Once it works, check what the socket listens on.
$ ssh -p 2222 user@192.0.2.10# ss -tlnp 'sport = :2222'# systemctl status ssh.socket --no-pager[7] Close 22 only after the new port is confirmed. Removing the firewall rule is the last step.
# ufw delete allow 22/tcpAnalyze your auth.log (SSH) log in the browser ↗ — nothing is uploaded.
Check it with the ssh_config generator tool ↗ — calculated in your browser.