Changing the SSH Port on CentOS 7
This article explains in detail how to change the SSH port on CentOS 7.
Many people who run a server or virtual server know that changing the SSH port from the default 22 can improve security, because everyone, including attackers, knows that the default SSH port is 22.
Moving SSH to a port other than 22 makes your server a little harder to target, since people with bad intentions will not know which port SSH is listening on.
The best way to protect your SSH server is to sign in with keys and encryption. With this method, only machines that hold the key are allowed to sign in over SSH.
Another way is to configure your firewall so that SSH connections are accepted only from machines whose IP addresses you have whitelisted.
If you want to change the default SSH port on CentOS 7, you can follow the steps below.
1. Back up the sshd_config file.
cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak
2. Change the port number.
nano -w /etc/ssh/sshd_config
In the file that opens, remove the # at the start of the #Port line and enter the port number you want to use.
Save and exit with ctrl + x.
3. Allow access to the port.
semanage port -a -t ssh_port_t -p tcp YOURPORTNUMBER
If you get a "command not found" error, install the package below.
yum -y install policycoreutils-python
4. Restart the SSH service.
systemctl restart sshd.service