how to change ssh port

Publish : 2022 May 25 940 view Cat : AVANETCO + Linux
change ssh port
5/5 - (2 votes)

There are number of reasons to change the default ssh port for security and as a hosting provider or a user it is highly recommended to do as said, so lets get to how to change ssh port on linux servers

there are many attacks on port 22 so any server that is can be reached though internet is threatened by this attack. that is why for securing your connections most of hostings change this port to a random number

first you have to find a port that is not used (not in listening state) in our exmp we use port 2021

ss -tlpn| grep PORT-NUMBER

now ssh to server and edit the file below

nano /etc/ssh/sshd_config

change ssh port

in this file uncomment the line that says port 22 and change 22 with the number you found from the first command

now you have to bind the new port to selinux and if this command is not recognized then download policycoreutils package.

semanage port -a -t ssh_port_t -p tcp 2021

firewall-cmd --remove-service=ssh --permanent

then allow your new port in firewall

firewall-cmd --add-port=2021/tcp --permanent

after editing every config file in linux in order for the changes to take place you should restart the service

systemctl restart sshd

one important tip. until the last step which is connecting to server via new port do not close the current ssh session

now that you learned how to change ssh port try to ssh to server like the post earlier we provided for your ease