How to change ssh port on Linux/Centos/Ubuntu ?

Running SSH on a port other than 22 will make more unlikely to be hit with a random port scan. You will be targeted only if the attacker is trying to get your server, not any server. Normally attackers start scan on entire subnet of IPs of a datacenter and they will look for all common ports. 

In this article we will see how to change ssh port on Linux/Centos/Ubuntu servers 

Procedure to change SSH port on the server


Make sure you open the new port in your firewall . Else you wont be able to get into server.

If its a remote machine , its good to have KVM or VNC console access available. Incase you locked out of the server on ssh ( chances are less) , you can still access the servers

1 Locate the sshd configuration file

On Centos , you will find the file at

On Ubuntu

2.Edit the file using vi

Locate the line with following

Comment that line out and Add new line with a different port number

Now save and close the file.

3. Restart sshd service

Now we need to restart sshd , then only changes will be affected

On CentOS/RHEL/Fedora Linux , restart sshd using

Or with

Or with systemctl (Centos7)

On Ubuntu/Debian

Now you should be able to login to server using ssh on new port

Feel free to contact me if you face any issue. Always Happy to help ????

Leave a Reply

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