How to Change Default SSH Port 22 in Ubuntu or Debian?

Hackers always scan for servers running with default SSH port and try to connect using brute-force dictionary attack. Hackers make list of common used words and use them as password. These attacks are random and automated. If your password is weak and dictionary word, hacker might be lucky. By changing the default Port 22 you can prevent your system from being targeted.

One more scenario is sometimes work place, school or college default port will be blocked. To get ride of these issues it is better to change the default Port 22 to some random port number that is not currently in use.

Example: Port 80 will be used by Web server and Port 25 is used by Mail server. So make sure that port number which you assigned is not used by any other application in your server.

To change the port edit /etc/ssh/sshd_config with vi or nano.

sudo vi /etc/ssh/sshd_config


sudo nano /etc/ssh/sshd_config

change default port 22

Port 22 – Change to default port 22 to some random number that is not currently in use.
After changing the port, save the changes and restart sshd service:

sudo /etc/init.d/ssh restart

You should now be able to SSH into your server using the new port number. If you have iptables or firewall configured in your server make sure to update the new port number.