GICT knowledge base
How to Change the SSH Port in Ubuntu 24.04 (ssh sockets)
Published
How to Change the SSH Port in Ubuntu 24.04 (The New Way)
If you’ve just deployed a new Ubuntu 24.04 VPS with InterServer, one of the first things on your to-do list is likely changing the default SSH port. In the past, this was as simple as editing sshd_config, but Ubuntu 24.04 has changed the game by moving to systemd socket activation.
Why the Switch to SSH Sockets?
In older versions, the SSH service ran in the background 24/7, waiting for someone to connect. With the new “Socket” system, a small listener stays active, and the full SSH daemon only fires up when someone actually tries to connect. This is a big win for your server’s performance:
- Lower Resource Usage: It saves on RAM and CPU because the full service isn’t idling.
- Better Security: It handles brute-force connection attempts much more efficiently, making your server harder to crash during an automated attack.
Step 1: Edit the SSH Socket
Instead of editing the main system files (which can be overwritten during updates), we use an override command. This creates a custom configuration that stays put even when the system updates.
Run the following command in your terminal:
sudo systemctl edit ssh.socket
Step 2: Input Your New Port Configuration
An editor will open up. You need to tell the system to stop listening on the default port and start listening on your chosen port. In this example, we’ll use Port 2222.
Paste the following block exactly as shown:
[Socket]
ListenStream=
ListenStream=0.0.0.0:2222
ListenStream=[::]:2222
Pro Tip: That first empty
ListenStream=line is not a typo! It clears the default port 22 so that the system only uses the new port you’re about to define.
Step 3: Reload and Restart
Now, tell Ubuntu to recognize the changes and restart the socket listener by running these two commands:
sudo systemctl daemon-reload
sudo systemctl restart ssh.socket
Step 4: Update Your Firewall
Don’t get locked out! You must tell your firewall to allow traffic through your new port. If you are using UFW, run these commands:
sudo ufw allow 2222/tcp
sudo ufw reload
Final Step: Test Your Connection
Important: Do not close your current terminal window yet. Open a second terminal on your local computer and try to log in using the new port:
ssh -p 2222 root@your-server-ip
If you can log in, you’ve successfully modernized your SSH security! If something went wrong, you still have your original window open to fix the config, or you can use the VNC Console in your InterServer dashboard to get back in.
