Telnet "Connection closed by foreign host"
Connection closed by foreign host means the far end closed the Telnet connection. When it appears shows why: access rules, a port test, login timeout or logout.
Trying 192.0.2.10...
Connected to host.example.com.
Escape character is '^]'.
Connection closed by foreign host.
The last line means the other end closed the TCP connection. "Foreign host" is the remote computer. The Telnet clients on Linux, macOS and the BSDs print it whenever the connection ends from the far side. If you close it yourself from the telnet> prompt, the same clients print only Connection closed.
"Connected to" proves the network path works and something accepted the connection. That rules out the problems behind "Connection refused" or a timeout, which are covered in Connection abort, reset and timed-out errors in SSH and Telnet clients. What is left is a decision made by the service at the other end, and when the line appears is the best clue to which one.
Right after "Connected", before any text
The service accepted the connection, then refused to serve you.
- Access rules. Many servers check the client's address first and close the connection at once if it is not allowed. Some firewalls and load balancers also accept a connection and then drop it when nothing behind them is available.
- A connection limit. The service is at its maximum number of sessions and closes new ones.
- The service failed while starting the session, for example a Telnet server started on demand whose program is missing or broken.
From the client side these look the same. The service's log on the host shows which it was.
After you type something, on a port that is not Telnet
Telnet is often used to test whether a port is open. The other service then waits for its own protocol, and closes the connection when you type something it does not understand, or after its own time limit.
- A web server on port 80 usually closes after one request and its answer.
- A mail server on port 25 waits for its own commands and closes after a time limit.
- An SSH server on port 22 sends a line starting
SSH-2.0-, and closes after the first line you type. See Telnet to port 22: what the SSH-2.0 banner means.
Here the close is normal. The port test already succeeded at "Connected to".
At the login prompt
- You waited too long. Unix login programs end an unfinished login after a set time. On Linux this is
LOGIN_TIMEOUTin/etc/login.defs, given in seconds. - Too many wrong passwords. The login program allows a limited number of attempts (
LOGIN_RETRIES, often overridden by PAM settings) and then closes the connection.
After you were logged in
- You logged out.
exitorlogoutends the shell, and the host closes the connection. This is the normal end of a session. - The session sat idle. In bash,
TMOUTmakes an interactive shell exit after that many seconds without input. Other hosts have their own idle timers; for IBM i see Why an IBM i session ends or signs off when it sits idle. - Someone ended it on the host, or the host restarted.
What to do
Note exactly when the line appeared, then try again. If it happens at once on every attempt, ask the host's administrator to check the service's log and its access rules for your address. For a dropped working session, start with Why did my session disconnect?.
Related
- Telnet basics
- Telnet to port 22: what the SSH-2.0 banner means
- Connection abort, reset and timed-out errors in SSH and Telnet clients
- ssh "Could not resolve hostname": Name or service not known