Determining the reverse port tunnels

Damien Miller djm at
Wed Apr 22 11:31:57 AEST 2015

On Tue, 21 Apr 2015, Cary FitzHugh wrote:

> Hi all,
> If I were to connect to a system with a command like this:
> ssh -R *:0:localhost:3000 user at server
> Is there any way for openssh to tell my shell what ports it opened for me?
> i.e. what port on server is my localhost:3000 exposed to?
> I know that there is a stderr / stdout printout of the port, but wonder if
> there is a way to know in my shell.  Maybe an env var?  Maybe some grepping
> of netstat?

At the moment, the only way is to use the multiplexing support. E.g.

$ ssh -nNfS -oControlPath=~/.ssh/ctl-%r@%h:%p
$ ssh -oControlPath=~/.ssh/ctl-%r@%h:%p -O forward -R 0:
Allocated port 27313 for mux remote forward to
Allocated port 27313 for remote forward to

The double logging is a bug - patch below.

In general, it's unreliable to put the port number in an environment
variable because the remote shell session is separate to the forwardings
and may be establised before they are set up.


Index: mux.c
RCS file: /cvs/src/usr.bin/ssh/mux.c,v
retrieving revision 1.50
diff -u -p -r1.50 mux.c
--- mux.c	20 Jan 2015 23:14:00 -0000	1.50
+++ mux.c	22 Apr 2015 01:30:43 -0000
@@ -592,7 +594,7 @@ mux_confirm_remote_forward(int type, u_i
 	if (type == SSH2_MSG_REQUEST_SUCCESS) {
 		if (rfwd->listen_port == 0) {
 			rfwd->allocated_port = packet_get_int();
-			logit("Allocated port %u for mux remote forward"
+			debug("Allocated port %u for mux remote forward"
 			    " to %s:%d", rfwd->allocated_port,
 			    rfwd->connect_host, rfwd->connect_port);
 			buffer_put_int(&out, MUX_S_REMOTE_PORT);
@@ -1703,7 +1716,7 @@ mux_client_forward(int fd, int cancel_fl
 		if (cancel_flag)
 			fatal("%s: got MUX_S_REMOTE_PORT for cancel", __func__);
 		fwd->allocated_port = buffer_get_int(&m);
-		logit("Allocated port %u for remote forward to %s:%d",
+		verbose("Allocated port %u for remote forward to %s:%d",
 		    fwd->connect_host ? fwd->connect_host : "",

More information about the openssh-unix-dev mailing list