In a previous article we mention how to change the SSH port on Ubuntu, In this article, we want to show you how to change the SSH port in CentOS 7.
The default port of the SSH service is 22, and if we have it by default, hackers may be able to attack the server through port 22, take control of it, or cause irreparable damage to it. Therefore, we suggest that you change the default SSH port to increase the security of your server.
How to change SSH port in Centos 7
As you know you need to log in as a non-root user with root privileges. you can get a quick look at the article about the Initial server setup with Centos 7. Now you can start to change the SSH port on Centos 7.
Changing SSH port
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.
After entering SSH in Centos 7, go to sshd_config file editing mode with the following command:
sudo vi /etc/ssh/sshd_config
After entering the above command, we will see the contents of the sshd_config file.
Next, we need to change the specified number 22 to another value (for example port 6842). We also need to remove the # character from the beginning of this line to use this line as an active operating command on the server. And save the file.
After saving, don’t quit until you’ve completed these steps to change the SSH port.
sudo semanage port -a -t ssh_port_t -p tcp 6842
If you run the commands above and get an error that the semanage command is not found, run the commands below to install it.
sudo yum -y install policycoreutils-python
After that, run the commands below to allow the new SSH port through the firewall on Centos 7.
sudo firewall-cmd --permanent --zone=public --add-port=6842/tcp
Reload the firewall configurations. run the following command:
sudo firewall-cmd --reload
Restart SSH by running the commands below.
sudo systemctl restart sshd.service
Verify that SSH is now running on the new port on Centos 7 by running the commands below.
ss -tnlp | grep ssh
Exit and try signing in using the new port number.
ssh [email protected] -p 6842
At this point, you about the SSH port and you can easily change the SSH port for more security.
If you are interested, you can read the What is SSH and What does it do?
Hope you enjoy this article about How to change the SSH port in centos 7.