Check SSH socket activation
These instructions cover fresh Ubuntu Server installations using systemd socket activation. In this setup, ssh.socket listens for connections and passes the listening sockets to the SSH service.
First, check whether the socket is active:
__$ systemctl is-active ssh.socket
Expected output:
active
If the output is inactive, SSH may be running without socket activation. In that case, changing #Port 22 to Port 22123 in /etc/ssh/sshd_config may be sufficient. The instructions below for socket activation do not apply in this case. Search Google for a guide to changing the SSH port without socket activation on your Ubuntu version.
Check SSH rules in UFW
In the Activate firewall chapter, the service name ssh opened the standard port 22. This firewall rule does not automatically follow a port change, so we must explicitly allow the new port before restarting SSH.
Let's retrieve the current firewall rules for safety:
__$ sudo ufw status verbose
Output:
Status: active
Logging: on (low)
Default: deny (incoming), allow (outgoing), disabled (routed)
New profiles: skip
To Action From
-- ------ ----
22/tcp ALLOW IN Anywhere
22/tcp (v6) ALLOW IN Anywhere (v6)
We first allow the new port in the firewall while the existing SSH connection remains open:
__$ sudo ufw allow 22123/tcp
If your server provider has an additional firewall, allow TCP port 22123 there as well.
Change SSH port
Ubuntu provides the base configuration for ssh.socket. We put our settings in a separate override (drop-in) file, which systemd reads after the base configuration. This keeps our changes separate from the package files and prevents package updates from overwriting them.
Create the override directory and open the configuration file. Nano creates the file when you save it:
__$ sudo mkdir -p /etc/systemd/system/ssh.socket.d
__$ sudo nano /etc/systemd/system/ssh.socket.d/override.conf
Save the following settings for port 22123:
/etc/systemd/system/ssh.socket.d/override.conf
[Socket]
ListenStream=
ListenStream=0.0.0.0:22123
ListenStream=[::]:22123
BindIPv6Only=ipv6-only
The empty ListenStream= clears the previous listeners, including port 22. The next two lines enable port 22123 on all IPv4 and IPv6 interfaces.
BindIPv6Only=ipv6-only keeps the IPv6 listener separate from the IPv4 listener. If IPv6 is disabled on your server, omit the ListenStream=[::]:22123 line.
The commented #Port 22 line in /etc/ssh/sshd_config can remain unchanged; this override sets the listening port.
Apply the socket configuration
Check the SSH daemon configuration before restarting. A successful check produces no output:
__$ sudo sshd -t
This checks sshd_config, not the socket override. Resolve any errors before continuing.
Reload systemd so it reads the new override:
__$ sudo systemctl daemon-reload
Stop the SSH service, restart the socket and start the service with the new listeners:
__$ sudo systemctl stop ssh.service
__$ sudo systemctl restart ssh.socket
__$ sudo systemctl start ssh.service
Verify the new SSH port
Check whether a TCP listener is running on port 22123:
__$ sudo ss -lntp 'sport = :22123'
The output should show port 22123, something like this:
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 4096 *:22123 *:* users:(("systemd",pid=1,fd=138))
Look for a LISTEN entry with :22123 in the local address column. This confirms the local listener; a new SSH login also checks that the port is reachable from your computer.
Keep the current connection open. Open a second PuTTY window or terminal and log in through port 22123 using the instructions for your SSH client below. Only a successful login with the second client confirms that the new SSH access works and that you have not locked yourself out through an incorrect SSH configuration.
On Windows with PuTTY
Open another PuTTY window, load your saved session, change the port to 22123 and save the session again. Then connect:
On Windows with PowerShell
In a new local PowerShell window, use -p 22123 for the new port and -i followed by the Windows path to your private key in OpenSSH format:
ssh -p 22123 -i "D:\linux-server\keys\private-key" tom@116.203.69.89
Under Mac and Linux in Terminal
We add the parameter -p and the port number. The complete SSH command looks like this in the terminal:
__$ ssh tom@116.203.69.89 -p 22123 -i ~/linux-server/id_rsa
Remove the old firewall rule
Only after the new SSH login succeeds do we remove the old 22/tcp firewall rule:
__$ sudo ufw delete allow 22/tcp
If another rule still allows port 22, such as the OpenSSH application profile, remove that matching rule as well. See the UFW cheat sheet for deleting individual rules.
Check the firewall rules again:
__$ sudo ufw status
Output:
Status: active
To Action From
-- ------ ----
22123/tcp ALLOW IN Anywhere
22123/tcp (v6) ALLOW IN Anywhere (v6)