Solution:
Root Cause: SELinux Context Misconfiguration or Port Mapping Conflict
On SELinux-enforced systems (RHEL, CentOS, Fedora, Rocky Linux),
sshd must run under the correct security context (
sshd_t), and port 22 must be bound to the allowed SELinux port type (
ssh_port_t). If port 22 was modified or if filesystem labeling is corrupt, SELinux blocks the daemon from accepting socket connections, resulting in connection refusal.
Diagnostic Verification:
Check SELinux status:bash
sestatus
Inspect audit logs for AVC denial rejections involving sshd:bash
sudo grep -i avc /var/log/audit/audit.log | grep -i sshd
Verify port labeling in SELinux policy:bash
sudo semanage port -l | grep ssh_port_t
Step-by-Step Fix:
1.Add Port 22 to SELinux Port Type (If modified):
If running SSH on a custom port (e.g., 2222), assign it to ssh_port_t: bash
sudo semanage port -a -t ssh_port_t -p tcp 2222
2.Restore Default File Contexts:
Fix potential filesystem context corruption on binary executables: bash
sudo restorecon -v /usr/sbin/sshd
3.Temporarily Test in Permissive Mode:
Verify if SELinux is causing the rejection by switching to permissive mode: bash
sudo setenforce 0
If connection succeeds, analyze AVC denials using audit2why to craft custom policy modules.Prevention & Long-Term Monitoring:
Always use semanage port rather than raw policy bypasses when changing default SSH operational ports on hardened systems.