whiteink

Forwarding additional ports from a live SSH session

While reading the SSH man page1, I came across the following rather neat little trick.

You may know that if you type <CR>~. (that’s “enter”, “tilde”, “dot”, in order) you can terminate a hung SSH session. But it gets better than that: if you type <CR>~# you can see a list of all the current session’s forwarded ports. Yet more useful: type <CR>~C (capital “C”), and you’ll be dropped into a shell where you can add additional port forwards on the fly:

$ <CR>~C
ssh> help
Commands:
      -L[bind_address:]port:host:hostport    Request local forward
      -R[bind_address:]port:host:hostport    Request remote forward
      -D[bind_address:]port                  Request dynamic forward
      -KR[bind_address:]port                 Cancel remote forward
$ <CR>~C
ssh> -L 5678:127.0.0.1:80
Forwarding port.

Very handy if you’re in the middle of something and don’t particularly want to disconnect and reconnect to the server just to add a -L ... to your SSH invocation.


  1. I’ll take this opportunity to stress that unix man pages are an amazing and underused learning resource. Two more powerful tools with detailed man pages are lsof and openssl. [return]