Change the default SSH port 22 for your Ubuntu VPS Server

The default SSH port on the Ubuntu server is 22. It is highly advisable to change this default port to a custom port number. This is because the hackers try to exploit this port in order to gain unauthorized access to your server by means of Brute Force attack. It is easier for the hacker to deploy a Brute Force attacker on your Server if the port 22 is open and accessible. This is the port with the highest number of attacks being received by the hackers.

How to change port 22 to a custom port ?

We have learnt about the importance of changing port 22 to a custom port. Now we shall see how to bring this change in action. Follow the steps below:

  • You must have these 2 software installed on your system. Putty and WinSCP
  • Use Putty to access the Ubuntu Server by using your root user (It is presumed that you know this process already)
  • By now, you must have a custom port number in your mind that you want to use in place of port 22. Say you have chosen port 4567 (Make sure that the custom port that you choose is not a default or standard port or it being used by some other process)
				
					$ sudo ufw allow 4567/tcp
				
			
				
					Output
Rule added
Rule added (v6)
				
			
  • You have now successfully opened port 4567 on your Ubuntu Server. You can verify if your rule was successfully added or not using the below command
				
					$ sudo ufw show added
				
			
  • The output will show you that you have added port 4567
  • Once you have confirmed that the port 4567 has been opened, you have to allow this port to be used for SSH access to your Ubuntu Server. In order to do that, you need to use the software WinSCP
  • Sign in to WinSCP using your root account, in the same manner which you have used to access through Putty, to your Ubuntu Server
  • Once you have gained access to your server, navigate to the file
Path to the sshd file
  • Right click on the file sshd_config and press Edit
  • Find the string #Port 22
  • You need to remove the # before the word Port and replace 22 with your newly opened port 4567 in this file
  • Once you have made the changes, save this file and close it
  • In order for the changes to be in action, we need to restart the SSH service. Switch back to your Putty window and run the below command
				
					$ systemctl restart sshd.service
				
			
  • Once the service has restarted, exit the shell. This time if you try and connect again via port 22, you should get an error message telling you that the connection has been refused like this:
  • That’s all. Now try to sign in using Putty using your host name / IP and port as 4567 using your root account and password.