SSH linux how to kill an idle or innactive connection

Sometimes when you're disconnected abruptly from a remote server like if your network connection gets dropped, you can be left with a ghost or inactive SSH session.

This can become pretty annoying when you're doing some SSH port redirection since you will not be able to bind to a given port since the ghost connection is still using that port, and you might end up with error messages such as the following :

debug1: Local connections to LOCALHOST:21521 forwarded to remote address localhost:21521
debug1: Local forwarding listening on 127.0.0.1 port 21521.
bind: Address already in use
debug1: Local forwarding listen

Now there are a few ways of dealing with this issue, the one I prefer is a 3 step operation :

1.Check who is connected to the server

This step is actually optional but it allows you to check which user is currently connected to the server and prevent accidentally destroying another user's session (or at least warning him that you will do it ;) )

To check who's connected to the server you can run the w command. the w command shows who is logged in to the system and what they are doing.

So go ahead and run the following command on your prompt :

   [ufasoli]> w

You should see an output similar to this one

 20:16:14 up 132 days,  3:38,  2 users,  load average: 0.04, 0.03, 0.00
USER     TTY        LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0     20:07    0.00s  0.02s  0.00s w
root     pts/1     20:09    6:17   0.02s  0.02s -bash

Here you can identify your user with the WHAT column as it indicates what the user is doing (invoking the command w in our case) /

2.Display the running processes

By using the pstree command with the -p to show process identification numbers (PIDs)

Go ahead and run the following command :

[ufasoli]> pstree -p

pstree displays the processes (i.e., executing instances of programs) on the system in the form of a tree diagram so when running the above command you should have an output similar to the one below :

init(1)─┬─cron(8865)
        ├─cupsd(2385)
        ├─oracle(2063)
        ├─oracle(2065)
        ├─oracle(2067)
        ├─oracle(2069)
        ├─oracle(2176)
        ├─oracle(2180)
        ├─oracle(12830)
        ├─scsi_eh_1(226)
        ├─scsi_eh_2(232)
        ├─scsi_eh_3(236)
        ├─slpd(2136)
        ├─sshd(2411)─┬─sshd(12840)───bash(12842)───pstree(12908)
        │            └─sshd(12887)───bash(12889)
        ├─sshd(14276)
        ├─syslogd(2035)
        └─tnslsnr(3917)


3.Kill the unwanted process

In our case the unwanted session has the PID 12287 as it's the session without the pstree command that we've just issued.

You can now kill the unwanted PID (just be careful not to kill a colleagues session ;) ):

   [ufasoli]> kill -1 12887

Once the session is killed you should be able to bind to the previously occupied port

No comments:

Post a Comment

OSX show used ports or listening applications with their PID

On OSX you can display applications listening on a given port using the lsof the commands described below will show listening application...