Software⏱️ 3 min read📅 2026-06-15

How to Fix: ssh error: connection closed by remote host

SSH connection issue on Mac, no ~/.ssh file, connection closed by remote host.

Quick Answer: Check the SSH server logs on the target machine for any error messages or warnings.

The 'ssh_exchange_identification: Connection closed by remote host' error occurs when you try to SSH into a Mac machine, resulting in an abrupt disconnection. This issue affects individuals who rely on SSH for remote access and can be frustrating due to the lack of clear diagnostic information.

Despite conducting initial research, finding a definitive solution remains elusive. In this guide, we will explore the root causes of this error and provide step-by-step instructions for resolving the issue.

🔍 Why This Happens

  • The primary reason for this error is typically related to a misconfigured SSH server on the remote Mac machine. The most common cause is an incorrect or missing 'HostKey' entry in the '/etc/ssh/ssh_host_keyed_identity_file' file, which prevents the SSH client from verifying the identity of the remote host.
  • An alternative reason for this error may be related to a firewall configuration issue on either the local or remote machine. Firewalls can sometimes block or restrict incoming SSH connections, leading to a disconnection.

🔧 Proven Troubleshooting Steps

Resolving HostKey Configuration Issues

  1. Step 1: Step 1: Check for an existing 'HostKey' entry in the '/etc/ssh/ssh_host_keyed_identity_file' file and ensure it matches the expected format. The correct syntax is: 'ssh-rsa @'. If the entry does not exist, create a new one using the 'ssh-keygen -t rsa -C "username@hostname"' command.
  2. Step 2: Step 2: Verify that the SSH server is configured to use the correct host key. Check the '/etc/ssh/sshd_config' file for the 'HostKey' directive and ensure it matches the expected format. If necessary, update the configuration using the 'sudo nano /etc/ssh/sshd_config' command.
  3. Step 3: Step 3: Restart the SSH server service to apply the changes: 'sudo systemctl restart sshd'.

Resolving Firewall Configuration Issues

  1. Step 1: Step 1: Check the firewall configuration on both the local and remote machines to ensure that incoming SSH connections are allowed. On macOS, this can be done by running the 'sudo pfctl -e' command (only applicable for macOS High Sierra or later). For other operating systems, refer to their respective documentation.
  2. Step 2: Step 2: If necessary, update the firewall configuration to allow incoming SSH connections. This may involve adding a new rule or modifying an existing one. Refer to your system's documentation for specific instructions.

✨ Wrapping Up

By following these steps and understanding the root causes of the 'ssh_exchange_identification: Connection closed by remote host' error, you should be able to resolve the issue and establish a successful SSH connection to the Mac machine.

Did this fix your problem?

If not, try searching for specific error codes.

🔍 Search Error Database

❓ Frequently Asked Questions