Change SSH Default Port


January 10, 2025
Written by Sean David Reed
OS/Distro: Ubuntu 24.04.1 LTS

  1. Port numbers 1 - 1024 are reserved for system services, so they're best left alone. Choose any port number >1024.

  2. Allow the port through the firewall.

    $ sudo ufw allow <port>/tcp
  3. Add the port to the ssh config file.

    $ sudo vim /etc/ssh/sshd_config

    In Vim, type /Port 22, which should find the same commented out. Below it, add your port number:

    #Port 22
    Port 1492       # Added by seandavidreed
    #AddressFamily any
    #ListenAddress 0.0.0.0
    #ListenAddress ::
  4. Restart ssh.

    $ sudo systemctl restart sshd
  5. Check that the port has been changed.

    $ sudo systemctl status ssh

Troubleshooting a problem on Ubuntu 24.04.1 LTS

  1. On Ubuntu 24.04.1 LTS, I had trouble getting the default port to actually change using steps 4 and 5. Consulting an answer on Ask Ubuntu (shared above), I got the default port to change.

    $ sudo systemctl daemon-reload
    $ sudo systemctl restart ssh.socket
    $ sudo systemctl restart ssh.service
  2. Check that it worked.

    $ sudo systemctl status ssh

Resources Used