how to change ssh port

There are several security reasons to change the default SSH port, and we recommend it for hosting providers and users alike. Here is how to change the SSH port on Linux servers.
Port 22 is a constant target of attacks, so any server reachable over the internet is exposed to them. That is why many hosting providers change this port to a random number to secure SSH connections.
First, find a port that is not in use (not in a listening state). In our example, we use port 2021.
ss -tlpn| grep PORT-NUMBER
Now connect to the server over SSH and edit the file below:
nano /etc/ssh/sshd_config
In this file, uncomment the line that says Port 22 and replace 22 with the port number you found with the first command.
Next, bind the new port in SELinux. If the command is not recognized, install the policycoreutils package.
semanage port -a -t ssh_port_t -p tcp 2021 firewall-cmd --remove-service=ssh --permanent # then allow your new port in the firewall firewall-cmd --add-port=2021/tcp --permanent
After you edit any config file in Linux, restart the service so the changes take effect:
systemctl restart sshd
One important tip: do not close your current SSH session until the last step, when you have successfully connected to the server on the new port.
Now that you know how to change the SSH port, try connecting to your server as described in our earlier post.