Set up SSH keys without losing access to your VPS
Create an SSH key, install the public key and test a new login before changing password access.
Read the guideVPS security · Practical guide
Check the SSH listener, allow the new port and test a second connection before retiring the old one.

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.
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.
cat /etc/os-release
sudo ss -ltnp
systemctl is-active ssh.socketNote 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.
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.
sudo ufw allow 2222/tcp
sudo ufw status numberedIf 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.
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.
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.confUse 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.
Port 22
Port 2222sudo sshd -tNo output normally means the syntax is valid. This is not a firewall or reachability test. Resolve errors before applying the settings.
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.
sudo systemctl reload ssh.servicesudo systemctl daemon-reload
sudo systemctl restart ssh.socketDo 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.
sudo ss -ltnpYou 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.
ssh -p 2222 vpsuser@server.example.comCheck 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.
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.
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.
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.
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.