Skip to content

Changing the SSH Server Port Number

By default, SSH servers listen on port 22. For security reasons, or when required by third-party services, you may want to change this port number. This guide explains how to safely update the SSH port on your Server.

Connect to your server using SSH on the current port (default is 22):

ssh root@yourdomain.com -p 22
  1. Open the SSH configuration file in a text editor:

    nano /etc/ssh/sshd_config
  2. Locate the line:

    Port 22
  3. Change 22 to your desired port number (e.g., 7822).

    Port 7822
  4. Save and exit the file.

If your server has a firewall enabled, you must allow the new port:

  • iptables example:

    Terminal window
    iptables -A INPUT -p tcp --dport 7822 -j ACCEPT
    service iptables save
  • firewalld example (CentOS/AlmaLinux):

    Terminal window
    firewall-cmd --permanent --add-port=7822/tcp
    firewall-cmd --reload
  • ufw example (Ubuntu/Debian):

    Terminal window
    ufw allow 7822/tcp
    ufw reload

Restart SSH to apply the changes:

  • On AlmaLinux/Fedora:

    Terminal window
    service sshd restart
  • On Debian/Ubuntu:

    Terminal window
    service ssh restart

Open a new terminal window and try connecting with the new port:

Terminal window
ssh root@yourdomain.com -p 7822

Do not close your original session until you confirm the new connection works.