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.

Steps to change the SSH port

  1. 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.

  2. Check how SSH is started. These steps cover a service-managed OpenSSH server. If ssh.socket controls the listener, follow your distribution’s socket-activation instructions instead; changing and reloading sshd_config alone may not change the listening port.

  3. Back up /etc/ssh/sshd_config and any files under /etc/ssh/sshd_config.d/. Check included files for existing Port settings.

  4. 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/tcp

    For 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. Replace public below if necessary:

    sudo firewall-cmd --zone=public --add-port=2222/tcp
    sudo firewall-cmd --permanent --zone=public --add-port=2222/tcp
  5. 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 2222

    Stop and investigate if the port is already assigned to another service. Do not relabel it blindly.

  6. Edit the global section of the SSH configuration, before any Match block. Temporarily specify both the old and new ports. If the existing port is not 22, keep its actual value:

    Port 22
    Port 2222
  7. Check the configuration. Resolve any errors before reloading:

    sudo sshd -t
    sudo sshd -T | grep '^port '
  8. Reload the service using its name on your distribution, such as sudo systemctl reload ssh on a service-managed Ubuntu installation or sudo systemctl reload sshd on RHEL. Check that the new listener exists:

    sudo ss -ltnp
  9. From a separate terminal on your computer, test a new login. Replace the username and server address:

    ssh -p 2222 username@server-address
  10. Update SFTP clients, monitoring and any automation that uses SSH. Once all required access works on the new port, remove the old Port setting, validate and reload again. Only then remove the old port’s firewall rules if it is no longer required.

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.