Customize SSH port on a Raspberry Pi

The standard SSH port 22 is a popular target for attackers. If you want to additionally secure your Raspberry Pi, you can simply change the port for SSH access. In this guide, I will show you step by step how to do this.

1. Select a free port

Before you get started, you should think about which port you want to use in the future. This is important:

  • No port duplication! A port already used by another service leads to conflicts.

  • Wikipedia can help: You can find an overview of known and reserved ports on Wikipedia.

Tip: It is best to choose a port between 1025 and 65535 that is not used elsewhere, e.g. 1357.


2. Edit SSH configuration file

Open a terminal and edit the sshd_config file, which contains the SSH server settings:

sudo nano /etc/ssh/sshd_config

3. Find and change the port line

Search for the Port entry in the file using the key combination CTRL + W. You will probably find the following (commented out) line:

#Port 22

Remove the # and replace 22 with your desired port:

Port 1357

4. Save file and close editor

  • Save: CTRL + O, then Enter

  • Exit: CTRL + X


5. Restart the SSH service

The SSH service must be restarted for the change to take effect:

sudo systemctl restart ssh

6. Test connection

If everything went well, you can now connect via the new port – enter the new port after -p:

ssh pi@your-ip-address -p 1357

…or:

ssh allsky.local -p 1357

Conclusion

With just a few simple steps, your Raspberry Pi is a little bit more secure. Of course, changing the default port does not replace other important security measures such as public key authentication or Fail2Ban – but it is a sensible first step.

Enjoyed this post?

You can support allsky-rodgau.de with a small coffee on BuyMeACoffee.

Buy me a coffee!