How can I logout an open, remote SSH session?

50

31

I sshed into a Linux machine (bash shell) from a public Windows machine (in our lab) and forgot to log out. I'm now back at my seat in another room and I am too lazy to walk back and log out that session; I can ssh into the Linux machine from my current PC though. Can I force-logout the other session from a new SSH session?

When I ssh to the Linux box from my current PC and type users command, I can see that I'm still logged in there; my name is listed twice - one for the current session and another for the session from lab PC.

I don't have root privileges on the said machine, but I guess that shouldn't matter as I'm just trying to log out myself.

Amarghosh

Posted 2010-09-27T04:49:30.203

Reputation: 643

Answers

74

Run tty on your current session, to find out on which tty you are working, so you do not log yourself out from current session. Run w to show you current users and associated pseudo-terminals(tty). Assuming that you are logged twice and there are no other users on your ssh server, your previous ssh session will be on pts/0 and current on pts/1. To ditch the session on pts/0 simply kill processes that are associated to it with

pkill -9 -t pts/0 

Casual Coder

Posted 2010-09-27T04:49:30.203

Reputation: 3 614

For some reason, pkill -9 pts/tty-number didn't work for me; then I found the pid of the process using ps aux | grep amar and tried pkill -9 -P pid and it worked. Thanks! – Amarghosh – 2010-09-27T05:24:49.150

7pkill -9 -t pts/tty-number. -t is the switch to specify tty – Casual Coder – 2010-09-27T05:35:28.163

Ooops, somehow I missed that -t in your answer when I read it first. – Amarghosh – 2010-09-27T06:37:44.917

+1, awesome fix. I just reset my router while I was SSHing to a machine on the same network, and then realized it left that session logged in... This worked perfectly. – Breakthrough – 2013-07-15T03:37:49.467

6Found this question today (and it works great, so thanks!) but found the -9 sounded a bit harsh. A simple -HUP sufficed for me. – Matijs – 2014-03-05T22:13:48.377

4

Assuming you don't have any other processes you don't want to be 86'ed, you can just do:

$ ssh <systemname> pkill -u <yourlogin>

And the other session will go away.

P. Heffner

Posted 2010-09-27T04:49:30.203

Reputation: 184

0

You can type:

~.

You won't see that you typed it but it will close the connection.

kellbells

Posted 2010-09-27T04:49:30.203

Reputation: 1

I would like to suggest to refer, https://superuser.com/help/how-to-answer will help you to compose better text.

– Rajesh S – 2018-02-23T06:37:56.510