Change SSH Port

The default port for the SSH protocol is port 22. Bots scan it along with other standard ports. Since the attack in most cases is not targeted. To cut off unnecessary traffic and secure the server, we will replace the standard port with our own.

This guide is written for users of the UFW firewall as the base for Ubuntu.

Steps to Change the SSH Port

  1. Change port 22 to a new one in the sshd_config file.

  2. Внести новый порт в список разрешенных UFW. Add the new port to the list of allowed by UFW.

  3. Restart the sshd service.

Replace port 22 with a new one

circle-info

The port number must not exceed 65535

Most often, the ssh config file is located at /etc/ssh/sshd_config The exact location can be found with the command find / -name "sshd_config" Let's edit it.

How? You need to find the line Port 22, and if it is commented out, remove the # symbol, and also enter a random number instead of port 22, for example 1234.

sudo nano /etc/ssh/sshd_config

After making changes, save and exit the file ctrl+x --> y --> Enter

Add the new port to the allowed list for UFW

sudo ufw allow 1234/tcp
sudo ufw deny 22

If we do not update the permissions of the brandmauer, then after restarting the sshd service, we will lose access to the server via the SSH protocol.

circle-exclamation

Sshd service restart

Done 🎉. Now we can only access via SSH on port 1234.

Last updated