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

7

2

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 user@host.com

… 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 user@host.com, but I'm thinking there has to be a way to just utilize that first session that I opened.

Kevin

Posted 2013-03-02T19:00:58.670

Reputation: 103

Answers

11

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

Next time, start the background session like this:

ssh -f -N -M -S ~/.ssh/S.user@host.com -D 9000 user@host.com

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

Now you can use ssh -S ~/.ssh/S.user@host.com 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...

user1686

Posted 2013-03-02T19:00:58.670

Reputation: 283 655

0

Leave off the -f (Background SSH) and the -N (Do not execute remote command) parameters, so use

ssh -D 9000 user@host.com

davidgo

Posted 2013-03-02T19:00:58.670

Reputation: 49 152

This is going to start another new SSH session which will also try to do port-forwarding to the local machine's port 9000. It will give a warning about being unable to bind to port 9000 if there is already another SSH session open which I believe is what the OP's use case looks like. – Tuxdude – 2013-03-02T19:23:48.817