09 Aug

How To Change SSH Port Of Linux Server

There are lot of brute force attacks over default service ports and hence it is highly recommended to change the default port as a first steps security measure. The default SSH port of Linux distros is 22. You can change this to something else by following below steps:

 

1) Login to the server via SSH as root:

ssh [email protected]/IP

where hostname/IP should be the host name / IP address to connect to the server.

 

2) We will need to edit sshd_config file and hence it is recommended that you backup original file before proceeding:

cp /etc/ssh/sshd_config /etc/ssh/sshd_config_backup

 

3) Open sshd_config in your favorite text editor (vim / nano):

vi /etc/ssh/sshd_config

-OR-

nano /etc/ssh/sshd_config

 

4) Navigate to line containing text #Port (which is currently commended out with a # sign). Remove # and replace port 22 with your preferred new port # (eg. 2220).

#Port 22 should be replaced with

Port 2220 (without #)

 

5) Save your changes and close the sshd_config file

 

6) Important – If you have firewall running on the server, you will need to edit the rules and allow newly entered port # in firewall exception list. For default iptables, you can use below rule (for custom port 2220):
iptables -A INPUT -p tcp -m tcp –dport 2220 -j ACCEPT

 

7) Restart sshd service by running  /etc/init.d/sshd restart