4

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?

Albert
  • 141
  • 2

1 Answers1

2

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 lightning.home.kaarsemaker.net
[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
Trying 127.0.0.1...
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 camel.ams6.corp.booking.com 
Last login: Sun Feb  3 11:43:07 2013 from lightning.home.kaarsemaker.net
[dkaarsemaker@camel ~]$ telnet localhost 1111
Trying 127.0.0.1...
connect_to localhost port 1112: failed.
Connected to localhost.localdomain (127.0.0.1).
Escape character is '^]'.
Connection closed by foreign host.
Dennis Kaarsemaker
  • 19,277
  • 2
  • 44
  • 70
  • I think you misunderstood my problem. The `RemoteForward` succeeds. There is just no one on the other side who is listening. – Albert Feb 03 '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 03 '13 at 13:19