Stefan Langenmaier
1 year
Claus-Theodor Riegg
6 years
Andreas Vöst
1 year

SSH add/remove port forwarding to active session

Posted . Visible to the public.

It is possible to manipulate the forwarded ports of an established interactive SSH session.
This is done by opening the command line with the escape character ~C. This will open a ssh> prompt. If it doesn't, make sure it's the first character in a line, i.e. press enter first.

You have following commands at your disposal

-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
-KL[bind_address:]port                 Cancel local forward
-KR[bind_address:]port                 Cancel remote forward
-KD[bind_address:]port                 Cancel dynamic forward

Excerpt from man ssh, line 487

~C      Open command line.  Currently this allows the addition of
        port forwardings using the -L, -R and -D options (see
        above).  It also allows the cancellation of existing port-
        forwardings with -KL[bind_address:]port for local,
        -KR[bind_address:]port for remote and -KD[bind_address:]port
        for dynamic port-forwardings.  !command allows the user to
        execute a local command if the PermitLocalCommand option is
        enabled in ssh_config(5).  Basic help is available, using
        the -h option.
Marc Dierig
Last edit
Emma Heinle
License
Source code in this card is licensed under the MIT License.