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

How to Fix: SSH error: “Connect to host: Bad file number”

Troubleshooting SSH connection issues with port forwarding.

Quick Answer: Check the firewall settings and ensure that the port is not being blocked by any network devices.

The 'Bad file number' error when connecting to a remote host via SSH can be frustrating, especially when it occurs consistently. This issue affects individuals who have set up port forwarding correctly but still experience connectivity problems.

Understanding why this error happens is crucial to resolving the problem. The 'Bad file number' error typically indicates an issue with the file descriptor or the way the operating system manages connections.

🔍 Why This Happens

  • The primary reason for the 'Bad file number' error when connecting to a remote host via SSH is that the file descriptor limit on the server has been reached. When multiple connections are established, each connection requires a unique file descriptor. If the limit is exceeded, the system will return an error message indicating a bad file number.
  • Another possible cause of this error is a misconfigured firewall or router. If the port forwarding rules are not set up correctly, it can lead to issues with establishing connections to the remote host.

🔧 Proven Troubleshooting Steps

Increasing the File Descriptor Limit

  1. Step 1: Step 1: Identify the current file descriptor limit on the server by running the command 'ulimit -n' in the terminal. This will give you an idea of how many connections can be established before reaching the limit.
  2. Step 2: Step 2: Increase the file descriptor limit using the 'ulimit -n' command followed by the desired number of connections. For example, to increase the limit to 1024, run the command 'ulimit -n 1024'.
  3. Step 3: Step 3: Restart the SSH server after increasing the file descriptor limit to ensure that the changes take effect.

Configuring Firewall and Router Rules

  1. Step 1: Step 1: Check the firewall rules on both the client and server-side to ensure that port forwarding is correctly set up. Typically, this involves setting a rule to forward incoming traffic from a specific port on the local network to the remote host's IP address.
  2. Step 2: Step 2: Verify that the router's port forwarding rules are also configured correctly. This may involve checking the router's configuration interface or contacting the manufacturer's support team for assistance.

💡 Conclusion

Resolving the 'Bad file number' error when connecting to a remote host via SSH requires identifying and addressing either the file descriptor limit issue or the misconfigured firewall/router rules. By following these steps, users can troubleshoot and fix this common problem, ensuring reliable connectivity to their remote hosts.

Did this fix your problem?

If not, try searching for specific error codes.

🔍 Search Error Database

❓ Frequently Asked Questions