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

How to Fix: Docker container ssh error: ssh_exchange_identification: Connection closed by remote host

Docker container SSH error: ssh_exchange_identification: Connection closed by remote host.

Quick Answer: The issue is caused by the incorrect port mapping in the Docker run command. Change -p 2222:22 to -p 2222:22 and try again.

The error 'ssh_exchange_identification: Connection closed by remote host' occurs when you attempt to SSH into an Ubuntu container running with openssh-server but fail due to authentication issues or incorrect configuration. This issue affects users who have set up their Docker containers to use SSH for interactive sessions.

This error can be frustrating as it prevents the user from accessing the container's shell, hindering productivity and making it difficult to troubleshoot issues within the container.

💡 Why You Are Getting This Error

  • The primary reason for this error is that the SSH server in the container is not configured correctly. Specifically, the 'PermitRootLogin' option in the sshd_config file is set to 'no', which prevents root login by default. This can be corrected by setting 'PermitRootLogin yes' in the sshd_config file.

🔧 Proven Troubleshooting Steps

Configuring PermitRootLogin

  1. Step 1: Open the sshd_config file in the container using a text editor: chroot /usr/sbin /bin/bash -c "sudo nano /etc/ssh/sshd_config"
  2. Step 2: Locate the line starting with 'PermitRootLogin' and change its value to 'yes'. For example, uncomment the line by removing the '#' symbol at the beginning.
  3. Step 3: Restart the SSH service to apply the changes: chroot /usr/sbin /bin/bash -c "sudo service ssh restart"

Verifying SSH Server Configuration

  1. Step 1: Check the SSH server configuration files for any other incorrect settings or missing configurations.
  2. Step 2: Verify that the SSH client on your host machine is correctly configured to use the container's IP address and port number.

💡 Conclusion

To resolve the 'ssh_exchange_identification: Connection closed by remote host' error, you can try configuring the PermitRootLogin option in the sshd_config file or verify the SSH server configuration. If neither method works, ensure that your SSH client is correctly configured to connect to the container's IP address and port number.

Did this fix your problem?

If not, try searching for specific error codes.

🔍 Search Error Database

❓ Frequently Asked Questions