I've been having this issue, that when I exit a ssh session, it never quits properly. I have to kill it with Ctrl-C.

Example Session Below:

[stefan@desktop ~]$ ssh stefan@
[stefan@laptop ~]$ # do some stuff
[stefan@laptop ~]$ exit
^CKilled by signal 2.
[stefan@desktop ~]$

Is there a reason this happens? ( some running proccess that blocks a clean exit perhaps ). And how can I fix it?

  • What happens if you use logout directly or issue a Ctrl+D? – N J Nov 12 '10 at 22:52
  • Note that if you use X-forwarding and you exit some program it could be that a socket is left it CLOSE_WAIT state. During this time the connection is still not completely terminated. The length of CLOSE_WAIT is configurable. (a socket goes into CLOSE_WAIT state to wait for possible stray TCP packets arriving late). – kasterma Nov 13 '10 at 0:10

From the ssh man page:

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

You've killed your shell, but it looks like some application still has a connection open over a forwarded port (the agent, X11, or a forwarding set up with -D, -L, -R or their ~/.ssh/config equivalent).

If you don't know what program is using the forwarding, use lsof -p $pid where $pid is the process id of the ssh client (on the client) or the sshd process that was the parent of the shell (on the server) to find out what ports it has open connections on. Then use lsof -i -n or netstat -pnt and search for processes that have connections open on these ports.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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