Change ssh port on centOS 8; Through this tutorial, we will learn how to change ssh port on centOS 8.
SSH or Secure Shell is a network communication protocol that enables two computers to communicate (c.f http or hypertext transfer protocol, which is the protocol used to transfer hypertext such as web pages) and share data.
Change SSH Port on CentOS 8
Follow the following steps to change ssh port on centOS 8:
- Step 1 – Backup Current SSH configuration
- Step 2 – Change SSH service port
- Step 3 – Allow new SSH port on SELinux
- Step 4 – Open SSH port on Firewalld
- Step 5 – Restart sshd service
- Step 6 – Verify SSH Service
Step 1 – Backup Current SSH configuration
First of all, open ssh and execute the following command into it to take backup of current ssh daemon configuration file:
date_format=`date +%Y_%m_%d:%H:%M:%S` sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config_$date_format
Then execute the following command on terminal to verify backup of current ssh daemon configuration file:
$ ls /etc/ssh/sshd_config* /etc/ssh/sshd_config /etc/ssh/sshd_config_2022_07_06:22:40:10
Step 2 – Change SSH service port
Now, execute the following command on command line to open SSH service configuration file:
sudo vi /etc/ssh/sshd_config
Now the file has been opened with the above-given command. Search #Port 22 in that file:
#Port 22
Now remove # from this line and enter any port. We will put port 33000 in it:
Port 33000
Save the changes and close the file.
Step 3 – Allow new SSH port on SELinux
If we will see that now the label of the default port is. is what it was before. can see by executing the following command on terminal:
$ semanage port -l | grep ssh ssh_port_t tcp 22
If we want to allow ssh to bind to the network port configured, then we need to execute the following command to modify the port type t ssh_port_t:
sudo semanage port -a -t ssh_port_t -p tcp 33000
Like now we have added the new port. We can use the command given below to verify it:
$ semanage port -l | grep ssh ssh_port_t tcp 33000, 22
Step 4 – Open SSH port on Firewalld
Now the port we have added. For that we also need to do firewall configuration.
So execute the following command on terminal to allow firewall configuration for above-added port:
sudo firewall-cmd --add-port=33000/tcp --permanent sudo firewall-cmd --reload
If Firewalld is not installed, use yum to install it and start the service:
sudo yum -y install firewalld sudo systemctl enable --now firewalld sudo firewall-cmd --add-port=33000/tcp --permanent sudo firewall-cmd --reload
Step 5 – Restart sshd service
Finally, execute the following command on terminal to restart the ssh service for the make changes effect:
sudo systemctl restart sshd
Step 6 – Verify SSH Service
Finally, we have changed the SSH port. To verify this, you can use the below command:
$ netstat -tunl | grep 33000 tcp 0 0 0.0.0.0:33000 0.0.0.0:* LISTEN tcp6 0 0 :::33000 :::* LISTEN
Conclusion
Through this tutorial, we have learned how to change ssh port on centOS 8.