The sshd port doesn't change after editing sshd_config in Ubuntu
Greetings!

A short preface: in the course of my work I most often deal with Debian and RHEL/Centos distributions.
So somehow I missed the changes in recent versions of the Ubuntu distribution described below🫣

Now for the case itself💼

Recently, while writing a playbook for a previous post, I ran into a nuance of how the sshd daemon behaves in Ubuntu 24: after changing the server’s listening port in the /etc/ssh/sshd_config file and restarting the service:

BASH
systemctl restart ssh
Click to expand and view more

The listening port didn’t change. That is:

BASH
ss -tln
Click to expand and view more

The system was still listening on port 22:

BASH
tcp  LISTEN  0  4096  *:22  *:*
Click to expand and view more

It turns out that in Ubuntu, starting from version 22 / 23.10 if I’m not mistaken, the ssh daemon uses a socket-based model for connecting clients to the server. So the usual service restart has no effect😐

Without knowing this nuance, you can really mess things up, for example, when configuring a server, if after editing the config and doing such a “restart” you close the old port with a firewall😬

Let me tell you a bit more about this behavior of uebuntu 😡

If you conditionally connect to the server console (not via ssh) and run:

BASH
pgrep -af ssh
Click to expand and view more

You’ll find that there is no ssh service at all🤷‍♂️
But at the same time:

BASH
tcp  LISTEN  0  4096  *:22  *:*
Click to expand and view more

The mechanism here is as follows: instead of the usual systemd unit ssh.service, the socket unit — ssh.socket — is active by default. The point of this model is to avoid keeping the ssh daemon process active all the time, to quote: “for the purpose of saving resources”🧏‍♂️

In the socket-based model, when a request comes in on the given port (22 by default), it hits the systemd socket, which in turn starts the ssh.service to activate the daemon and establish the client connection🙄

If we look at the contents of the socket unit:

BASH
systemctl cat ssh.socket
Click to expand and view more

We’ll see that the listening port is specified here:

PLAINTEXT
[Socket]
ListenStream=22
Click to expand and view more

Now, what to do in this case💀

After changing the port number in the sshd_config file, for example to 2222, to apply the changes you need to reload the init system’s configuration and restart the socket unit(!):

BASH
systemctl daemon-reload

systemctl restart ssh.socket

# check
ss -tln
Click to expand and view more

The listening port should change:

BASH
tcp LISTEN  0  4096  *:2222  *:*
Click to expand and view more

Now let’s look at the socket unit config:

BASH
systemctl cat ssh.socket
Click to expand and view more

And we see the new port:

PLAINTEXT
[Socket]
ListenStream=
ListenStream=2222
Click to expand and view more

Done✔️

If you need to revert to the classic sshd operation model, here are the steps described on the official Ubuntu site:

BASH
# disable the socket
systemctl disable --now ssh.socket

# remove socket configs
rm -f /etc/systemd/system/ssh.service.d/00-socket.conf

rm -f /etc/systemd/system/ssh.socket.d/addresses.conf

# reload configuration
systemctl daemon-reload

# start/enable the service
systemctl enable --now ssh.service
Click to expand and view more

This behavior turned out to be unexpected for me. You can’t keep track of everything🤷‍♂️

Copyright Notice

Author: Ivan Cherniy

Link: https://r4ven.me/en/networking/ne-menyaetsya-port-sshd-posle-pravki-fajla-sshd_config-v-ubuntu/

License: CC BY-NC-SA 4.0

Blog materials may be used with attribution to the author and source, for non-commercial purposes, and under the same license.

Start searching

Enter keywords to search articles

↑↓
ESC
⌘K Shortcut