This guide describes how to change the SSH daemon port on a RHEL host (e.g. a Hetzner bare metal server), including required updates for firewalld, SELinux, and the Hetzner Robot Firewall.
Source: Issue #292: "Add: How to switch ssh port on RHEL"
- Keep an active root session open while performing the change (e.g. SSH session + console access).
- Temporarily allow both ports (old + new) until you have verified login on the new port.
- Prefer restricting the firewall rule (Hetzner and host firewall) to your source IP/CIDR if possible.
In the examples below we use port 1984. Replace it with the port you prefer.
Edit /etc/ssh/sshd_config and add an additional Port line (keep 22 for now):
Port 22
Port 1984
Validate the config:
sshd -tfirewall-cmd --zone public --add-port 1984/tcp --permanent
firewall-cmd --reloadVerify:
firewall-cmd --zone public --list-portsInstall the SELinux tooling (package name depends on the RHEL major version):
# RHEL 8/9 commonly use:
dnf install -y policycoreutils-python-utilsAdd the new SSH port type:
semanage port -a -t ssh_port_t -p tcp 1984If you get an error that the port already exists, modify instead:
semanage port -m -t ssh_port_t -p tcp 1984Verify:
semanage port -l | grep -E '^ssh_port_t'If you use the Hetzner Robot Firewall, add a rule to accept TCP traffic to destination port 1984 (ideally from your source IP/CIDR).
Important note from this repository’s docs: Hetzner Firewall only supports IPv4; for IPv6 you must rely on the host firewall. See the firewall section in README.md.
Reload the daemon:
systemctl reload sshdVerify it is listening on the new port:
ss -tulpn | grep 1984Example output:
tcp LISTEN 0 128 0.0.0.0:1984 0.0.0.0:* users:(("sshd",pid=1349,fd=3))
tcp LISTEN 0 128 [::]:1984 [::]:* users:(("sshd",pid=1349,fd=4))From your workstation:
ssh -p 1984 root@YOUR_HOSTNAME_OR_IPOptional: configure your local SSH client so you don’t need -p every time:
Host my-hetzner-host
HostName pluto.openshift.pub
User root
Port 1984If you changed the SSH port and run this repo’s playbooks remotely, set ansible_port for the target host.
mkdir -p inventory/group_vars/all/
echo 'ansible_port: "1984"' > inventory/group_vars/all/custom_ssh_portOnly do this after you have confirmed you can log in using the new port.
- Remove
Port 22fromsshd_config - Remove
22/tcpfrom your host firewall and Hetzner Firewall rules - Reload
sshdagain:
systemctl reload sshdsshd -tfails: revert your last edit to/etc/ssh/sshd_config, then re-runsshd -t.- SELinux blocks the port: ensure
semanage port -l | grep ssh_port_tincludes your chosen port. - No connectivity from the internet: ensure both the host firewall and Hetzner Firewall allow the port (and the correct IP family).