How to change SSH port on CentOS7


First Take a backup copy of the existing SSH server configuration :

cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak

Now Edit the configuration file :

vim /etc/ssh/sshd_config

and change the Port number from 22 to any custom port you choose :

Port 2244

Save the file with :wq!

# If you want to change the port on a SELinux system, you have to tell
# SELinux about this change.
# semanage port -a -t ssh_port_t -p tcp #PORTNUMBER

Example Case :

you need to install policycoreutils package to run the semanage command. to do this :

yum -y install policycoreutils-python

now update SeLinux policy to allow new port for SSH service. to do this, run below :

semanage port -a -t ssh_port_t -p tcp 2244

now you need to update Firewall policy to allow new SSH Port, to do this run below :

firewall-cmd --permanent --zone=public --add-port=2244/tcp
firewall-cmd --reload
systemctl restart sshd.service

to Check the service status, run below :

ss -tnlp | grep ssh