How to connect to a SSH session that was backgrounded with -f -N?

I'm doing some work where I need to pivot off a machine using proxychains so I'm connecting to a system and binding a local port like so...

ssh -f -N -D 9000 [email protected]

… which returns me back to my command prompt after opening the connection. I can then use something like proxychains to run commands through host.com.

My question is: How can I connect/interact with that same session so I can get a remote shell on host.com? The way I'm doing it now is opening up another ssh session with a simple ssh [email protected], but I'm thinking there has to be a way to just utilize that first session that I opened.


If it wasn't created with multiplex enabled, you cannot.

Next time, start the background session like this:

ssh -f -N -M -S ~/.ssh/[email protected] -D 9000 [email protected]

Here -M enables multiplex master mode, and -S sets the socket path.

Now you can use ssh -S ~/.ssh/[email protected] dummyhost to open a second session over the same connection. It's possible to control the master by giving -O exit, -O check, and various other options. (Sadly, -O forward is a very recent addition.)

To make this automatic, you can use the following options in .ssh/config:

Host *
    ControlPath ~/.ssh/S.%l.%r@%h:%p
    ControlMaster auto
    ControlPersist 10m

Setting ControlPath means you do not need to specify -S every time.

Setting ControlMaster auto means that every new connection will automatically continue in background as a multiplex master. (Without it, you can still start new masters with ssh -fNM host).

Setting ControlPersist 10m means that the automatic masters will stick around for 10 minutes when they don't have any active sessions. (This is a recently added option.)

Note that batch transfers over a multiplex connection will cause interactive sessions to become really slow...