How to change SSH port on CentOS 7
Secure shell, more commonly known as SSH, is a network protocol that provides secure channels for server communications on a network in a client-server architecture. This protocol is used in many applications, such as users accessing shell accounts on a server or during communications for authentication. SSH communications go through something called a port, which is essentially a communications endpoint on a server. By default, SSH uses the TCP (Transmission Control Protocol) port 22. Being able to modify this SSH port number on your CentOS 7 server is an easy and proven way that you can enhance server security by switching to a less-commonly known port.
In this guide, we will show you how you can change SSH port number to any desired choice, on your own CentOS 7 server.
Confirm that you have the following before you follow this guide:
- 1 Server (Cloud Server or Dedicated Server) running CentOS 7, Centos 8.
- Root access to the node
Install nano package to have easy access to the configuration file
- Run the following command to install nano package:
yum install nano -y -q
Open and edit the SSH daemon configuration file
- Run the following command to open the sshd_config file:
2. Use the arrow keys to scroll through the file until you locate the following text:
#Port 22 #AddressFamily any #ListenAddress 0.0.0.0 #ListenAddress ::
3. Place the cursor on the line below #Port 22 Press Enter to create a new line and type:
In our case the port number we’ll be using is “1515” you can customize it as you desire.
4. Restart the SSH service to apply the changes using the following command:
systemctl restart sshd.service
Bind SSH daemon to the new port
1. Install the policycoreutils package to bind the SSH daemon to the new SSH port by using the following command:
yum install policycoreutils -y -q
2. Add the following rules to ensure that the SSH daemon binds with the specified port:
semanage port -a -t ssh_port_t -p tcp 1515 semanage port -m -t ssh_port_t -p tcp 1515
Congratulations! Now you have successfully configured a new port for SSH on your CentOS 7 server. If this introduction to the basics was helpful to you, please feel free to share this tutorial with your friends.