I am developing a some code in order to communicate between a PC and a Raspberry Pi via SSH. When the PC opens a SSH connection to the RPi a window with a short message pops up on the RPi's screen. When there are no open SSH connections said window closes itself. Everything works just fine so far.

Now, I want to add a button in said window on the RPi in order to terminate all SSH incoming connections. Is there a Unix command for that? A Python module or method that does that would be fine as well, since the RPi's code is written in Python.

According to my research terminating a SSH connection can be done by killing the corresponding process via its PID. Since I want to kill all SSH processes/connections autonomously I am looking for an easier solution.

Thanks for your help!

(The Raspberry Pi runs on Raspbian 8 (jessie))

1 Answer 1


Killing all the SSH server processes isn't hard if you have pkill/pgrep, just run pkill sshd. The problem is separating the main SSH daemon from the per-connection ones, and leaving that one alive.

A simple workaround for that would be to shut down the server, kill the remaining per-connection processes, and then restart the server, e.g. on a systemd system:

systemctl stop sshd
pkill sshd
systemctl start sshd

Or, given that sshd is probably set up to write a PID file, we can read the PID of the main process from there, and skip that. On my Debian with systemd, it's written in /run/sshd.pid but it could be set to something else in /etc/ssh/sshd_config with the PidFile option.

pgrep gives the output one PID per line, so we can use grep to remove the line with the main PID (grep -x should be used to avoid partial matches):

mainpid=$(cat /run/sshd.pid)
pids=$(pgrep sshd | grep -vxe "$mainpid")
kill $pids

The unquoted expansion of $pids splits the string on whitespace so kill gets the PIDs separately.

You'd have to run that as root, since there's actually two per-connection processes: one running as root, one as the logged-in user. Or, if you want to just kill the sshd processes owned by your user, use pgrep -u username sshd.

  • rather than pids=....kill $pids I'd suggest killall -HUP sshd ; sleep 1 ; killall -TERM sshd ; systmctl restart sshd
    – symcbean
    Dec 19, 2021 at 20:57
  • @symcbean, HUP is actually a good idea in that it looks like it'd terminate the per-connection processes, but causes the main process to just reload the configuration. Not sure if there's any reason the TERM would be needed after the HUP though. As far as I can see, the unprivileged per-connection sshd terminates of either, and the privileged one catches either signal or the unprivileged counterpart dying. As for killall, well, it does something really different on some systems, so I'd rather avoid using it.
    – ilkkachu
    Dec 19, 2021 at 21:08

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.