How to Change the SSH Port in Linux

This step-by-step guide explains how to change the default SSH port (22) on your Linux machine.

Here’s a useful tip for people that access their computer or server over the Internet using SSH. Change your SSH port from the default port 22 to something else. Although this is not a foolproof hack to secure your server, it can help. Let’s look at you this would be done on an Ubuntu machine. NOTE that this change should not be performed over a remote SSH connection, you might lose all contact with your server.

  1. First, check to see if the SSH service is running at all, and if it is, then on which port. Run the following command:

    # netstat -tulpn

    In the output you should see an entry for port 22. This is the SSH service.

  2. output of netstat in a terminal
    click to enlarge

  3. Now let’s make the change. Open the SSH service configuration file:

    # sudo vim /etc/ssh/sshd_config

    At the beginning of the file you will a line that looks has Port 22 on it. Change the number 22 to something of your choice, for example, 678. Save the file and exit.

  4. a terminal with the ssh config file open
    click to enlarge

  5. Now you need to restart the SSH server so that the new setting can kick in. To do so execute the command:

    # sudo /etc/init.d/ssh restart

  6. a terminal restarting the linux ssh server with new port
    click to enlarge

  7. Make sure that the restart goes through correctly. If you are running a firewall, make sure that you allow access to the new SSH port.
  8. You would have been using a command like # ssh to access your server remotely. As the SSH client is preconfigured to use the port 22 as the default port you do not need to explicitly tell it to use the port 22. Henceforth you will need to tell it to your the port you chose for your server. Your new command will look something like this:

    # ssh -p 678

    Little hacks like these go a long way to helping you secure your Linux box.

If this article helped you, I'd be grateful if you could share it on your preferred social network - it helps me a lot. If you're feeling particularly generous, you could buy me a coffee and I'd be super grateful :)

buy a coffee for

Home » Linux » How to Change the SSH Port in Linux

Leave a Comment

Your email address will not be published. Required fields are marked *