SSH Tunnels

Jump to: navigation, search

Being able to reuse the same port redirections for multipl hosts without rebooting your WIndows PC begins with how ytou create the redirect.

Assuming you have the following in your ssh config:

host    mongo
        hostname <ip>
        user <user>
        BatchMode yes


ssh -M -S ~/.ssh/control-socket -fnNT -L 9769: mongo

Check or get the PID:

ssh -S ~/.ssh/control-socket -O check mongo


ssh -S ~/.ssh/control-socket -O exit mongo

Breaking it down:

ssh -M -S ~/.ssh/control-socket -fnNT -L 9769: mongo

-M Master Mode -S Location of Control Socket ( File ) -f Fork into background -n Redirects stn from /dev/null -N Do not execute a remote command ( Executing a remote command will hold the connection open until the command exits. -T Disable pseudo-terminal allocation ( We aren't interested in an interactive shell ) -L Tells SSH we are forwarding things.

Use a control command to check the status of the connection.

ssh -S ~/.ssh/control-socket -O check mongo Master running (pid=5136)

'-o' Valid commands are: - “check” (check that the master process is running), - “forward” (request forwardings without command execution), - “cancel” (cancel forwardings), - “exit” (request the master to exit), - “stop”

Use Netstat to verify your port is bound.


  1. netstat -n -o -a | grep 9769
 TCP              LISTENING       7500  ( <-- 7500 is NOT the PID of the connection. )
 TCP    [::1]:9769             [::]:0                 LISTENING       7500  ( <-- Same thing, for IPv6 ( Shudder ))


  1. ss -ln | grep 9769

tcp LISTEN 0 128 *:*

  1. ssh -S ~/.ssh/control-socket -O exit mongo

  1. netstat -n -o -a | grep 976

< Nothing Returned >

via Chris McCormick on StackOverflow