SSH normally accepts connections on port 22. Using a different port can reduce automated connection attempts against that port, but it does not replace strong authentication or server updates.
Why change the SSH port?
Change the port only when it fits your server’s access policy. You need administrator access, a working provider console and permission to change any network firewall rules. Keep your existing SSH session open throughout the change.
Incorrect SSH or firewall settings can lock you out. Confirm that you can use the server console before proceeding, and keep the old port available until a separate login on the new port succeeds.
Steps to change the SSH port
-
Choose an unused TCP port allowed by your provider, for example
2222. The commands below use that example; replace it consistently if you choose another port. -
Check how SSH is started. These steps cover a service-managed OpenSSH server. If
ssh.socketcontrols the listener, follow your distribution’s socket-activation instructions instead; changing and reloadingsshd_configalone may not change the listening port. -
Back up
/etc/ssh/sshd_configand any files under/etc/ssh/sshd_config.d/. Check included files for existingPortsettings. -
Allow the new port through the server firewall and the provider firewall before changing SSH. Use the firewall tool already managing the server. For UFW, an example is:
sudo ufw allow 2222/tcpFor firewalld, identify the active zone with
sudo firewall-cmd --get-active-zones, then add the port to that zone in both runtime and permanent settings. Replacepublicbelow if necessary:sudo firewall-cmd --zone=public --add-port=2222/tcp sudo firewall-cmd --permanent --zone=public --add-port=2222/tcp -
On systems enforcing SELinux, ensure the new port is labelled for SSH. If it is unassigned, an administrator can add it with:
sudo semanage port -a -t ssh_port_t -p tcp 2222Stop and investigate if the port is already assigned to another service. Do not relabel it blindly.
-
Edit the global section of the SSH configuration, before any
Matchblock. Temporarily specify both the old and new ports. If the existing port is not 22, keep its actual value:Port 22 Port 2222 -
Check the configuration. Resolve any errors before reloading:
sudo sshd -t sudo sshd -T | grep '^port ' -
Reload the service using its name on your distribution, such as
sudo systemctl reload sshon a service-managed Ubuntu installation orsudo systemctl reload sshdon RHEL. Check that the new listener exists:sudo ss -ltnp -
From a separate terminal on your computer, test a new login. Replace the username and server address:
ssh -p 2222 username@server-address -
Update SFTP clients, monitoring and any automation that uses SSH. Once all required access works on the new port, remove the old
Portsetting, validate and reload again. Only then remove the old port’s firewall rules if it is no longer required.
Security best practices recommended
Use SSH keys where appropriate, keep the operating system updated and restrict access to authorised users. A port change does not prevent someone who discovers the new port from attempting to connect.