Server Fault is a question and answer site for system and network administrators. 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 use dynamic SSH port-forwarding (-D) and normal SSH port-forwarding (-L or -R) for many different things, including adding a layer of encryption to my wireless web traffic. The command I use normally looks something like this:

ssh -l raam -D 9000

After starting the tunnel, I configure my web browser to use a SOCKS v5 Proxy of with port 9000. Now all traffic in my browser (except DNS) is transmitted through the SSH tunnel.

When I'm ready to close the tunnel (when I'm taking my laptop to another location, for example), I simply type "logout". However, the SSH session hangs and I have to press CTRL+c to get my local prompt back.

Why does this happen and how can I prevent it?

(My guess is that the connections I opened through the tunnel remain open and my local SSH client is waiting for them to close before giving me my prompt back. If this is the case, how can I force all those connections to close when I'm ready to logout?)

share|improve this question
up vote 16 down vote accepted

As you expected, this happens because SSH won't exit if there are outstanding connections going through the tunnel.

If you exit your browser (and all other programs that are going through the port 9000 tunnel) then SSH should exit.

The ssh man page says:

 The session terminates when the command or shell on the remote machine exits and all X11 and TCP connections have been closed.

And I don't see any options to change that behavior, so I suspect there's nothing you can do.

share|improve this answer
Thanks, that confirms my suspicion. :) – Raam Dev Jun 5 '09 at 23:24

You can background SSH doing:

share|improve this answer
or even better, use ssh -f – Thomas Jun 5 '09 at 18:38
@Thomas That won't work since the ssh connection is being used interactively until loging out. But in some cases it may be useful to keep port forwardings and interactive usage on separate ssh connections. – kasperd Sep 6 '15 at 14:18

As stated in the comment you can create a tunnel only connection using -f, but you'll still have the issue of the connection not terminating until everything using the tunnel has exited. You can use the ~# option (escape sequence and a 'pound') to list the forwarded connections you have open on a given connection before you logout.

share|improve this answer
I hadn't heard of ~# to list the connections. That's really helpful. Thanks! – Raam Dev Jun 5 '09 at 21:24
Type <Enter>~? for help - there are more escape sequences out there. – kubanczyk Jul 8 '09 at 20:02

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.