Raspberry Pi

How to Change Default SSH Port in Raspberry Pi

Secure Shell (SSH) allows users to remotely perform terminal-related tasks on Raspberry Pi from any location even if there is no monitor connected to Raspberry Pi for display. Although SSH communication is encrypted and doesn’t allow third parties to intercede between the communication of two devices, still if you want to secure your SSH network, it is recommended to change the SSH port number. So, if someone tries to access the device, they must have to enter the port number.

In this tutorial, we will show you the method to change the default SSH port in Raspberry Pi.

How to Change the Default SSH Port in Raspberry Pi?

To change the default SSH port in Raspberry Pi, open the sshd_config file using the nano editor by typing the below-mentioned command into the terminal:

Then into the configuration file, scroll to the point where #Port 22 is written:

Uncomment this port number by removing the # from the left and change the Port number to your desired number. You can choose any number between 102465535. Here, I have used 2500 for the SSH port.

Then press Ctrl+X then Y to exit and save the modified file.

Now to allow the traffic on our newly set port, follow the below-mentioned command:

$ sudo ufw allow 2500/tcp

Note: You can install Firewall (ufw) on Raspberry Pi from here.

Now restart the sshd service so that it can restart with the changes we have made for sshd:

$ sudo systemctl restart sshd

The default SSH port has changed successfully, and now user can access the Raspberry Pi through SSH using port number 2500.

Accessing SSH Through Windows with a New Port Number

PuTTY is required to access the Raspberry Pi through windows. The user just needs to install PuTTY on a laptop or PC. Once done, open the PuTTy application and enter the IP address of the Raspberry Pi server with the newly set port number as 2500.

If you do not know the IP address run the below-mentioned command into the Raspberry Pi terminal to find out the IP address of the server:

$ hostname -I

The IP address will be displayed as an output of the above command:

Once you have entered the IP address and port into the PuTTY it will take you to the login screen where you have to enter your Raspberry Pi username and password to get the SSH access into the Raspberry pi:

Below I have just run a simple ls command to show that I have accessed the Raspberry Pi through SSH on windows:

$ ls

Accessing SSH Through Mac with a New Port Number

If you are accessing an SSH port through Mac then you must mention the port name in the command. Just open the z-shell and type the below-written command along with the IP address and port number:

Syntax:

ssh pi@<IP Address> -p <Port Number>

Example:

In the example below I have used my server’s IP and the SSH port number which I have set for my SSH port:

ssh pi@192.168.18.67 -p 2500

Just by using the above command along with the Raspberry Pi password user can access the newly set port.

Conclusion

The default SSH port in Raspberry Pi is 22, which the users can change by opening the sshd_config file using the nano editor. The port number can be assigned according to their choice and once it’s done, restart the service of sshd so it can start with a newly assigned port number. After that, the Raspberry Pi system can be accessed through SSH using the new port number.

About the author

Zahra Zamir

An Electronics graduate who loves to learn and share the knowledge, my passion for my field has helped me grasp complex electronics concepts and now I am here to share them with others.