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
NOTES
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
1 2 3 | /etc/ssh/sshd_config |
On Ubuntu
1 2 3 | /etc/ssh/sshd_config |
2.Edit the file using vi
1 2 3 | vi /etc/ssh/sshd_config |
Locate the line with following
1 2 3 | Port 22 |
Comment that line out and Add new line with a different port number
1 2 3 4 | #Port 22 Port 20786 |
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
1 2 3 4 5 6 | [root@test.test.com ~]# service sshd restart Stopping sshd: [ OK ] Starting sshd: [ OK ] [root@test.test.com ~]# |
Or with
1 2 3 4 5 6 | [root@test.test.com ~]# /etc/init.d/sshd restart Stopping sshd:[ OK ] Starting sshd: [ OK ] [root@test.test.com ~]# |
Or with systemctl (Centos7)
1 2 3 | systemctl restart sshd |
On Ubuntu/Debian
1 2 3 | systemctl restart ssh |
Now you should be able to login to server using ssh on new port
1 2 3 | ssh –p 20786 user@IPADDRESS –v |
Feel free to contact me if you face any issue. Always Happy to help ????