0

We know that we can specify Local or Dynamic Port Forwarding through ssh by specifying command-line parameters:

ssh -D #### \         # For Dynamic Forwards
    -L ####:host:#### # For Local Forwards

But if I forget to specify a forward, can I somehow add one during the session, without having to close the connection and start a new session?

PuTTY (on Windows) can do this, but ssh is a different application.

2

As I discovered in a comment on a similar question, ssh supports the EscapeChar, which is the tilde (~) character by default. With this EscapeChar, you can perform many tasks, including the modification of Port Forwarding. The EscapeChar only works following a new line, so you may need to insert a newline (i.e. by pressing enter).

Assuming that ~ is the EscapeChar, ~? will list the available options. The SSH command line provides the interface to add a new port forwarding route, with ~C. To add a Local or Dynamic Forward, use:

~C
ssh> -L ####:host:####
Forwarding port.
~C
ssh> -D ####
Forwarding port.
# Even without a prompt, your input has returned to the remote session
3
  • Is there a way to script this? I tried: # echo ' > ~C -L 8443:localhost:8443 > ' > /dev/tty but only got me that on stdout. Mar 3 at 13:51
  • You can't script it on the server side, since it is a client-side input to the ssh client. You can add it to your client-side ~/.ssh/config file under a Host section as LocalForward #port# host:#port# or DynamicForward #port#.
    – palswim
    Mar 4 at 5:50
  • I never said server side, but it doesn't seem to be scriptable in the client side either. I can input all that in the session's console, but I can't put a line in a bash script to it. Mar 5 at 7:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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