
Changing the SSH port can cut down on automated login attempts in your logs. The change is simplest when you keep the old port working, test the new one from the computer you actually use, and then update every client that needs access.
1. Record how SSH currently listens
Leave the working SSH session open. On the VPS, list its listening TCP ports and check whether systemd is managing the SSH listener through a socket.
sudo ss -ltnp
systemctl is-active ssh.socketFind the row for your current SSH port. An active response for ssh.socket means you need the socket instructions below when applying the change. Otherwise confirm that ssh.service is managing the listener. Check that nothing already uses the port you want and note which IP families the current listener uses.
2. Allow the new port through both firewalls
If your VPS uses the Control Panel firewall and UFW inside Ubuntu, allow the new port in both places. Leave the old rules in place. Duplicate the existing SSH rule’s source restriction if you use a stable office IP or VPN. Make sure you include the source addresses your administrators and monitoring systems actually use.
sudo ufw allow from 198.51.100.20 to any port 2222 proto tcp
sudo ufw status numberedThe address above is reserved for examples. Replace it with the public source address the VPS sees for your administrator connection. If that connection uses IPv6, add the appropriate IPv6 rule. If administrators move between networks, work out the access route before retiring their existing one.
3. Add the new listener alongside the old port
Save the SSH configuration and any active socket overrides in a private backup location before editing. Check /etc/ssh/sshd_config and /etc/ssh/sshd_config.d/ for existing Port and ListenAddress settings. A port assigned to a particular ListenAddress can affect which addresses accept the new connection.
On a standard Ubuntu installation that includes the snippets directory, use sudoedit /etc/ssh/sshd_config.d/00-local-ports.conf to set the two ports. If a local snippet or configuration manager already owns those settings, update that configuration instead.
Port 22
Port 2222Run sudo sshd -t on the VPS. It normally prints nothing when the configuration is valid. If it reports an error, correct the reported file and line before applying anything.
4. Apply the change the way your server runs SSH
If ssh.service owns the listener and ssh.socket is inactive, apply the change with sudo systemctl reload ssh.service. On standard Ubuntu 24.04 socket-managed installations, reload the generated systemd configuration and restart the socket.
sudo systemctl daemon-reload
sudo systemctl restart ssh.socketRun sudo ss -ltnp again. Both ports should be listening on the addresses you intend to use. If they aren’t, check systemctl cat ssh.socket for a ListenStream override left by an older installation or defined by your configuration. Resolve that difference from the Console before proceeding.
5. Prove a new login works from your computer
Open a separate terminal and connect to the new port. The -S none option ensures the test opens its own connection even if you share SSH connections in your client settings.
ssh -S none -o ConnectTimeout=5 -p 2222 vpsuser@server.example.comIf the host key prompt is new for this port, compare its fingerprint against the trusted fingerprint you read through the Console before accepting it. After sign-in, run whoami and sudo -v if your account needs administrator privileges. If the login times out, check the listening address and port, the actual client source address, and both firewalls while the old access route still works.
6. Update clients and retire the old port
Update saved SSH profiles, file transfers, backup tasks and deployment jobs. If you use an OpenSSH alias, inspect its effective settings with ssh -G my-vps and confirm it shows the new port. Test any job that connects unattended.
After those checks succeed, remove the old port from the SSH configuration while leaving the new port in place. Validate, apply and check the listener again before retiring the old firewall rules. Make one final fresh login to the new port.
7. Recover if something stops working
If you can't reach the new port, use the original session or the Console to restore the previous settings and apply them using the correct service or socket steps. Confirm the old port is listening and permitted by both firewalls. Once a fresh login works on the original port again, change one piece at a time so you can see what caused the problem.
Common questions
Does a different port make SSH more secure?
It can reduce routine log noise. Use tested SSH keys, restrict who can connect where practical, and keep Ubuntu updated. Start with the SSH key setup guide if you still rely on an account password.
Why does SSH still listen on port 22?
A second Port line can add a listener without removing the first one. Check every active snippet as well as the socket unit if your server uses socket activation. Use the actual listener list to confirm the outcome.


