Changing the OpenSSH Server Port
-
By default, the OpenSSH port is indicated in the
/etc/ssh/sshd_configfile with a line that reads# Port 22. Uncomment the line by removing its leading#character, and replace the port number with another value of your choosing (for example, the port can be changed to 222 by changing the line toPort 222). -
For security reasons you should disallow the Key Manager agent user
sshmgrfrom connecting to the host via the OpenSSH server. To do this, find the line in the/etc/ssh/sshd_configfile that begins withDenyUsers, and appendsshmgrto it.If there is no line in the
/etc/ssh/sshd_configfile that starts withDenyUsers, deny thesshmgruser by appending the following line to the file:DenyUsers sshmgrSave your changes to the file.
-
Install the SELinux management tools (if they have not been installed already):
- On Red Hat 8.x / Rocky Linux 8.4:
# yum install policycoreutils-python-utils
- On Red Hat 8.x / Rocky Linux 8.4:
-
Add the new OpenSSH port to the list of acceptable ports in SELinux (in this example, port 222):
# semanage port -a -t ssh_port_t -p tcp 222 -
Restart the OpenSSH server:
# service sshd restart -
Optional: You can run the following command to verify that the OpenSSH server is listening on the new port (substitute 222 with your chosen port number):
# netstat -tulpn | grep 222 -
For the OpenSSH server to be usable via the new port, that port must be allowed by the firewall. Allow the new OpenSSH port on the firewall by adding the following line to the
/etc/sysconfig/iptablesfile (substitute 222 with your chosen port number):-A INPUT -m state --state NEW -m tcp -p tcp --dport 222 -j ACCEPTSave your changes to the file. Restart the firewall to apply the changes:
# service iptables restart
The OpenSSH server should now function under the new port.