5

I have a few idle ssh sessions that I want to kill. This remote server is used by many users, how do I identify the pids of the sessions that I own?

When I run who, I get a list of ssh sessions labeled by username.

$ who
                                                                                       
me      pts/0        2020-11-23 18:05 (tmux(id).%0)
me      pts/1        2020-11-06 13:28 (<ip 1>)
someone    pts/2        2020-11-09 09:36 (<ip 2>)
someone    pts/3        2020-11-26 20:26 (<ip 2>)
...

When I run pstree, the sessions are no longer labeled by username.

$ pstree -p | grep sshd 

|-sshd(930)-+-sshd(3518)---sshd(3536)---python(5736)-+-{python}(5744)
           |           |-sshd(4285)---sshd(4308)
           |           |-sshd(5428)---sshd(5450)
...

2 Answers 2

4

Unless you are the root user, you will not be able to do anything to anybody else's processes, so there's no danger in just signalling all sshd processes:

pkill sshd

If you want to explicitly limit this to your own processes:

pkill -U "$USER" sshd

or, if the USER variable isn't used in the shell that you are running, try "$LOGNAME" in place of "$USER", or

pkill -U "$(id -un)" sshd

See also the manual for the pkill command.

Use pgrep instead of pkill if you want to get a list of the process IDs of the matched processes. Use pkill or pgrep with -o to only signal or list the oldest process.

2
  • 1
    Awesome, thanks. And when I run pkill sshd, will that kill the ssh session I'm currently running the command from? And will that also permanently kill my tmux sessions?
    – user443888
    Nov 27, 2020 at 16:53
  • @user443888 It would send the TERM signal to all sshd processes that you own. I'm unsure whether that would kill tmux, but I don't think so. It would be fairly easy to test (on my OpenBSD system, it does not kill tmux and I doubt it would do on Linux).
    – Kusalananda
    Nov 27, 2020 at 20:34
0

If you are root, don't run pkill sshd on a remote server, or you won't be able to reconnect: ssh: connect to host yourdomain.com port 22: Connection refused. Instead:

  1. Run w. Find your idle command in the last column (labeled WHAT). Then look at the second column (labeled TTY) to find your idle session, ex: pts/0.

  2. Run ps aux | grep pts/0. Find your idle command in the last column - its PID will be in the second column, ex: 12345. Also look for -bash and note its PID, ex: 12346.

  3. Run kill 12345; kill 12346 with the PIDs from above.

  4. Rerun w. pts/0 should no longer show.

1
  • You won't be able to reconnect after pkill sshd if pkill kills the sshd process which happens to be the daemon. Usually the daemon runs as root, regular users cannot kill/pkill it, they can only kill sshd processes they own. Compare this. Your answer is relevant if one logs in via SSH as root and if one wants to kill root's sessions; then yes, it's easy to kill the daemon as well. Some systems/setups disallow logging in via SSH as root, the problem does not exist there. Regular users can pkill sshd and the daemon will survive. Oct 16 at 21:34

You must log in to answer this question.

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