VPS security · Practical guide

Change your SSH port carefully

Check the SSH listener, allow the new port and test a second connection before retiring the old one.

Ethernet connections on network equipment

Changing the SSH port can reduce automated login noise, but an incorrect listener or firewall change can also lock you out. Keep the existing port working while you add and test a second one, then retire the old port once every client is updated.

1. Record how SSH currently listens

Keep a working connection open and verify console access. Check the OS release, listening ports and whether ssh.socket is active. Ubuntu can use either a traditional SSH service or systemd socket activation, and the way you apply a port change differs.

On the VPS: identify the release and listener
cat /etc/os-release
sudo ss -ltnp
systemctl is-active ssh.socket

Note the current port and whether SSH listens on IPv4, IPv6 or both. The example adds TCP 2222 alongside 22; substitute your actual old port if it is different. Confirm that another application is not already using the new port. Do not change a production server during an important transfer or deployment.

2. Allow the new port through both firewalls

In the VPS Control Panel firewall, add an incoming TCP rule for 2222 with the same trusted source addresses as your current SSH rule. Keep the old rule. If UFW also runs inside the VPS, allow the new port there too.

On a VPS using UFW
sudo ufw allow 2222/tcp
sudo ufw status numbered

If administration is restricted to one public IP, use a matching source restriction instead of opening the port to everyone. Remember that your home or office address can change. A successful guest rule does not override a blocked provider firewall, and a rule for IPv4 does not prove that IPv6 is covered.

3. Add the new listener without removing the old one

Back up the configuration before editing. Search both the main file and included snippets for active Port or ListenAddress settings. OpenSSH permits multiple Port lines, but existing address-specific or socket overrides can affect the listener.

On the VPS: save and inspect the existing configuration
sudo cp -a /etc/ssh /etc/ssh.before-port-change
sudo grep -R -E "^[[:space:]]*(Port|ListenAddress)" /etc/ssh/sshd_config /etc/ssh/sshd_config.d
sudoedit /etc/ssh/sshd_config.d/00-local-ports.conf

Use an unused backup location if the example backup already exists. On a standard Ubuntu configuration that includes this directory, put the following in the new file. If the active port is not 22, use its actual value on the first line.

Configuration: keep the old and new port during the transition
Port 22
Port 2222
On the VPS: validate the configuration
sudo sshd -t

No output normally means the syntax is valid. This is not a firewall or reachability test. Resolve errors before applying the settings.

4. Apply the change for the way your server runs SSH

If ssh.socket is inactive and ssh.service manages the listener, reload the service. If ssh.socket is active on a standard Ubuntu 24.04 installation, refresh its generated configuration and restart the socket. Keep the browser console ready because socket and service restarts can affect access.

Traditional service: use this branch only
sudo systemctl reload ssh.service
Ubuntu 24.04 socket activation: use this branch only
sudo systemctl daemon-reload
sudo systemctl restart ssh.socket

Do not run both branches blindly. Older upgraded installations can have explicit socket overrides. Inspect systemctl cat ssh.socket if the listener does not follow your settings, and follow the release’s OpenSSH notes before altering an override. If the new port is still missing after a socket restart, do not close your current access or remove the old port; use the console to inspect the running ssh.service and resolve the listener conflict.

5. Confirm the listener, then test from outside

On the VPS: inspect the actual open listening ports
sudo ss -ltnp

You should see the new TCP port listening on the intended addresses. If it is absent, the configuration has not taken effect; opening more firewall rules will not create a listener. Once it appears, open a fresh terminal on your own computer.

On your computer: make a new connection
ssh -p 2222 vpsuser@server.example.com

Check the username and sudo access. A timeout usually points to routing or a firewall. Connection refused suggests nothing is listening on that address and port. Permission denied means the server was reached and authentication needs attention. These are different problems, so record the exact error.

6. Update clients and retire the previous port

Update saved SSH profiles, SFTP clients, backup jobs, deployment tools and monitoring. In SFTP’s command-line client the port option is capital -P, unlike ssh’s -p. Test an actual file transfer if it is part of your workflow.

When all clients work on the new port, remove the old Port setting, validate and reapply the correct service/socket procedure. Confirm the old port is no longer listening, then remove only its obsolete firewall rules. Test another new connection before closing the original session.

7. Recover if the new connection fails

Use the still-open session or browser console to restore the configuration you changed. If you added only the new snippet, rename that snippet so it no longer ends in .conf, then validate and apply the same service/socket branch. Restore old firewall access before retrying. Keep the failed connection’s timestamp and recent SSH logs if you need help; never share a private key as part of a diagnostic report.

Common questions

Is port 2222 more secure than port 22?

It can make automated scans less noisy, but anyone can scan for a listening SSH server. Use strong authentication, updates and appropriate access rules regardless of the port.

Why does the configuration say 2222 while SSH still uses 22?

A running listener or an ssh.socket override may still control the address. Inspect the actual listener and socket configuration rather than repeatedly editing the same Port line.

Further reading

← Back to all guidesDiscuss your hosting setup ↗