Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am using SSH to forward a port on a remote machine (Client) to another machine (Server) using (~/.ssh/config)

Host Client:

Hostname {ip}
    RemoteForward localhost:{port} localhost:{port}

The problem is that even when there is not one listening on the Server, the client can connect successfully to localhost:port. The only indication of the failure is a message on the server: connect_to localhost port {port}: failed.

Is there a way to forward this error to the client and terminate the connection?

share|improve this question
What is the client ? SSH usually terminate the connection as soon as it detects it can't connect, but cannot do so before accepting the connection, due to a limitation of the standard socket interface. – BatchyX Feb 3 '13 at 9:46
A custom client. – Albert Feb 3 '13 at 9:48
Are you sure that your client handles disconnection well ? – BatchyX Feb 3 '13 at 9:50
Not at all. But I tried using telnet as well and it doesn't disconnect either. – Albert Feb 3 '13 at 10:03

If sshd cannot make the connection specified by your LocalForward, you connection to the local port will immediately be closed. Your sshd is misbehaving if it does not do that.

Here's what happens for me:

Terminal 1:

dennis@lightning:~$ ssh -L 1111:localhost:1112 camel
Last login: Tue Jan 29 00:05:28 2013 from
[dkaarsemaker@camel ~]$ channel 3: open failed: connect failed: Connection refused
channel 3: open failed: connect failed: Connection refused

The error messages are printed whenever I try to connect in terminal 2.

Terminal 2:

dennis@lightning:~$ telnet localhost 1111
Connected to localhost.
Escape character is '^]'.
Connection closed by foreign host.

And I see I read the question wrong, as you use a RemoteForward. Though that has a similar result for me, immediate disconnect:

dennis@lightning:~$ ssh -R 1111:localhost:1112 
Last login: Sun Feb  3 11:43:07 2013 from
[dkaarsemaker@camel ~]$ telnet localhost 1111
connect_to localhost port 1112: failed.
Connected to localhost.localdomain (
Escape character is '^]'.
Connection closed by foreign host.
share|improve this answer
I think you misunderstood my problem. The RemoteForward succeeds. There is just no one on the other side who is listening. – Albert Feb 3 '13 at 13:10
Yeah, for some reason I read LocalForward, not RemoteForward. Though the outcome is the same for me: immediate disconnect, not hanging. – Dennis Kaarsemaker Feb 3 '13 at 13:19

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.