How To Change SSH port on AlmaLinux

In this tutorial, we want to teach you How To Change the SSH port on AlmaLinux.

Every communication that is done using ssh, needs ports to connect and start the communication. Whether the device is wired or wireless, it will require ports. There are more than 65k communication ports available and you can start communication using any of these ports.

By default, all the communication is done using the ssh port number 22 which can be changed and set to any other available port number. Whenever the user runs the command to start the communication between the two devices over the network, the connection will use port number 22.

But, if you are carrying out any crucial communication that requires safety, it is recommended that you change the default ssh port number to other to avoid a brute force attack. As everyone knows that 22 is the default ssh port number and is most prone to attacks and any hacker will easily hack your transferred data. Using the ssh port, you can implement the SSH protocol.

Change SSH port on AlmaLinux

In this guide, we will show you to change the default SSH port to increase the security of your AlmaLinux server.

Steps To Changing SSH Port on AlmaLinux

To change the default SSH port, the first thing you want to do is backup the current SSH configuration on your system. To do that, run the command below.

sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak

This creates a new named sshd_config.bak with the current settings of the sshd_config file. If something goes wrong, you can then restore the file from the backup.

Then, switch to sshd_config file editing mode with the following command:

sudo vi /etc/ssh/sshd_config

In the file, you will see the contents of the sshd_config file.

Next, you need to change the specified number port 22 to another value (for example port 6842). Also, you need to remove the # character from the beginning of the port line to use this line as an active operating command on the server.

Port 6842

When you are done, save and close the file.

Next, you need to run the semanage command as shown below to complete your task:

sudo semanage port -a -t ssh_port_t -p tcp 6842

Note: If you are facing an error that the semanage command is not found, you can visit this guide on How to Fix ‘semanage command’ Not Found Error in AlmaLinux.

Configure Firewall to Allow New SSH Port on AlmaLinux

At this point, you must allow the new SSH port through the firewall.

sudo firewall-cmd --permanent --zone=public --add-port=6842/tcp

Then, reload the firewall to apply the new rules:

sudo firewall-cmd --reload

Restart SSH by running the command below.

sudo systemctl restart sshd.service

Verify that SSH is now running on the new port on AlmaLinux by running the commands below.

ss -tnlp | grep ssh
LISTEN 0 128* users:(("sshd",pid=14635,fd=4))
LISTEN 0 128 [::]:6842 [::]:* users:(("sshd",pid=14635,fd=6))

Exit and try signing in using the new port number.

ssh root@ip-address -p 6842

That’s it.


At this point, you learn to Change the SSH port on AlmaLinux.

Hope you enjoy it.

Please subscribe to us on Facebook and Twitter.

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Stay informed and not overwhelmed, subscribe now!