Update your firewall rules to allow the new port. If you're using UFW:
ufw allow <new_port_number>/tcp
ufw reload
- Run the following command to edit the SSH socket unit file:
EDITOR=vim systemctl edit ssh.socket
- This will open an editor with an empty file. Add the following lines:
[Socket]
ListenStream=
ListenStream=<new_port_number>
The line ListenStream= is required that port 22 is no longer used. Without this line, the SSH server would then be accessible via port 22 (default) and <new_port_number>.
-
Save the file and exit the editor.
-
Reload the systemd manager configuration
systemctl daemon-reload
- Restart the SSH socket
systemctl restart ssh.socket
- Verify that the new port is being used:
ss -tlnp | grep ssh