How to change the SSH port on Linux Servers

A novices guide to changing the SSH port on a linux server, by default Linux boxes are set to use port 22 for SSH ports, because it is a widely known port it’s a recommendation that you change the port to limit the automated port scans and brute force attempts, changing the port to something completely random can drastically hinder any brute force attempts and therefore becomes a nice and easy additional security measure.

How to change SSH port linux

Things to consider before changing your port,  if you use a firewall such as CSF you will need to add the new port to TCP_INTCP_OUTTCP6_IN, TCP6_OUT. Also removing port 22 from both TCP_IN and TCP6_IN.

To Change the SSH Port for Your Linux Server

  1. Connect to your server using SSH, most commonly with a program such as Putty


  2. If you have changed your default user, you will need to switch to Root user using a command such as
    sudo su -
  3. If you were already logged in as root , skip step 2 and you can go straight ahead and use the following command:
    vi /etc/ssh/sshd_config
  4. Press Shift + I on your keyboard to enable insert in VI mode,  Now locate the following :
    # Port 22
  5. Using the arrow keys remove “#” from the above line and change the number “22” to a port number of your choice ( such as a random 8536 ), Please make sure you note down the port number.
  6. Once you have changed the above number press ESC on your keyboard and then “:x” to save the session.
  7. You should now be back to the command prompt,  you now need to restart the SSHD service to finalise the process :
    service sshd restart
  8. Thats it ! now you should be able to exit Putty,  try and connect to SSH Port 22 as you did at the start and it should reject the login,  if you try again with the port you changed above it should let you in as normal.

If you do happen to forget your new port number and you run WHM/Cpanel,  a neat little trick is to login to WHM, restart the SSH daemon and it will tell you the SSH port number  upon restart.  ( Thanks to Matt W for the above tip )

If you require ongoing maintenance for your website or server please get in touch to discuss which of our Website Maintenance plans is best suited for you.