Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question

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...

share|improve this answer

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

ssh -D 9000 user@host.com

share|improve this answer
    
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 Mar 2 '13 at 19:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .