Monday, 8 July 2024

ssh: connect to host port 22: Connection refused

If you're unable to connect to a server via SSH but ILOM is still accessible, you can use the ILOM console to investigate and resolve the issue. Here's a real-time example of troubleshooting SSH connectivity failure.

Issue:

# ssh test_server
ssh: connect to host test_server port 22: Connection refused

However, ILOM (test_server-ilo) was accessible.

1) Connected to ILOM using SSH:

# ssh test_server-ilo
Password:
->

2) Started the console using the following command:

-> start /HOST/console
Are you sure you want to start /HOST/console (y/n)? y
Serial console started. To stop, type ESC (
test_server login: root
Password:
# pwd
/root
# hostname
test_server

3) Checked if SSH service (sshd) was running:

# ps -ef | grep sshd

If SSH is running, you will see output like:

/usr/sbin/sshd -D

In my case, there was no such process — SSH was not running.

4) Started the SSH service:

# systemctl start sshd.service

✅ This resolved the issue.

🛠 Other Possible Causes

1) SSH is running on a different port

Sometimes, SSH is configured to run on a non-default port (not 22).

# grep ^Port /etc/ssh/sshd_config
Port 2222

In such case, connect using:

# ssh -p 2222 test_server

2) SSH service is disabled or masked

If SSH service is disabled or masked, it won’t start automatically on reboot.

# systemctl status sshd.service
# systemctl unmask sshd.service
# systemctl enable sshd.service
# systemctl start sshd.service

3) Firewall is blocking port 22

If port 22 is blocked by the firewall, SSH will be unreachable from the network.

# firewall-cmd --list-all
# firewall-cmd --add-port=22/tcp --permanent
# firewall-cmd --reload

4) sshd binary missing or corrupted

If the sshd binary or its dependencies are missing or corrupted, SSH service may fail to start.

# which sshd
/usr/sbin/sshd

If not found or broken, reinstall the OpenSSH server package:

# yum reinstall openssh-server

5) ssh_keys group missing in /etc/group

Sometimes the ssh_keys group might be missing in the /etc/group file. To check:

# cat /etc/group | grep ssh
sshd:!:74:

If ssh_keys is missing, add the following line to the /etc/group file:

ssh_keys:x:102:

Then restart the SSH service:

# systemctl start sshd.service

📌 Related Reading