Linux

How to change the SSH port in Linux Servers

Changing the SSH port of your server is always a good idea to increase security. Every server on the internet will be scanned by bot servers looting for any weakness in your security. If you do not have a firewall on your server, think about installing CSF. It’s a great free firewall that can really help protect your server. If you also use the standard SSH port of 22 then your server will be brute-forced and eventually the password could be compromised. Follow this guide to change the default SSH Port on Linux systems like CentOS, RockyLinux, Ubuntu and Debian.

Change the SSH Port CentOS/AlmaLinux/RockyLinux

To get started ensure you have nano installed and let’s edit the sshd_config file;

yum install nano -y 
nano /etc/ssh/sshd_config

Change the SSH Port Ubuntu/Debian

apt install nano -y 
nano /etc/ssh/sshd_config

Look for the block of text that looks like this, your text will have a # in front of the lines;

Port 22
AddressFamily any
ListenAddress 0.0.0.0
ListenAddress ::

To change specific parameters within sshd_config, you need to uncomment the line by removing the number sign (#) and changing the value for the line. For example, the default SSH port appears in a line like this:

#Port 22

To change the ssh port to 4566, you will need to make the line appear like this:

Port 4566

The file will look like this after the modifications;

Port 4566
AddressFamily any
ListenAddress 0.0.0.0
ListenAddress ::

Save the file (cntl +x) keeping the same file name.

Restart SSHD

Finally, for the new settings to take effect restart the SSHD service;

service sshd restart

After you restart ssh, you will need to log out of your server and log in again using the new port number you specified in the sshd_config file.

IMPORTANT: If you have a firewall deployed to your server remember to close port 22 and open the port you specified above or you won’t be able to connect after you close the console.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button