You’d like to change the sshd port on CentOS, RHEL or Fedora.
Let’s assume you’d like to change it from port 22 (default) to port 22220 and have selinux enabled.
Also your remote hostname is example.com .
Let’s also assume semanage is installed. If not, the package name start with policycoreutils-python.
yum install policycoreutils-python
or
dnf install policycoreutils-python
dnf install policycoreutils-python-utils
If unsure press tab. And if you don’t know what tab is, what are you doing here anyway?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# make a backup of your config cp /etc/ssh/sshd_config /etc/ssh/sshd_config.`date +%Y%m%d-%H%M%S` # edit /etc/ssh/sshd_config # and replace #Port 22 with Port 22220 # make sure it's not commented out # there should be no leading hash symbol sed -i 's/^#Port 22$/Port 22220/g' /etc/ssh/sshd_config # add the port to selinux semanage port -a -t ssh_port_t -p tcp 22220 # add open the port in your firewall firewall-cmd --add-port=22220/tcp --permanent # remove the previous ssh service definition firewall-cmd --remove-service=ssh --permanent # restart sshd systemctl restart sshd # reload the firewall firewall-cmd --reload # hit ctrl-d or type exit # In your local host add a config cat <<EOF >> ~/.ssh/config Host example.com User root Port 22220 EOF |
And we’re done
You can now use
1 |
ssh example.com |
to log into your server
If you don’t want to add a config, you can use
1 |
ssh -p22220 root@example.com |
to do it.