How to setup port redirection after a ssh connection has been opened?
I’m using openssh on both client and server. I know I can run
ssh -L or
ssh -R to setup some port redirections over ssh. However, since they are command-line parameters, such redirections must be written before the connection is opened.
Can I setup port redirections on-the-fly using the command-line ssh client?
When I used the
PuTTY ssh client, I could interactively setup a port redirection while a connection was already opened, without dropping nor reconnecting, thus I know it is technically possible.
If you’re using the SSH command line, and you haven’t switched the escape character feature off, then you can type
~C after a newline to open a mini-console on the ssh client. Then type
-L port:host:port or
-R port:host:port or
-D port as you would on the command line to add a redirection, or
-KR port to remove a redirection.
A more flexible method to set up redirections without redoing the authentication is to start the first ssh client as a master (
-o ControlMaster=auto) and subsequent clients as slaves (
-o ControlMaster=auto). The slaves tunnel through the connection established by the master. You may need to set
ControlPath on the command line or in your
~/.ssh/config; see the description of the options in the
ssh_config man page for more information.