Is it possible to run some command (as root/the sshd user) when a client disconnects?

Or some other method of killing sshd after a single connection terminates?

  • you can add a cronjob task to run every second (or minute) to check if there is an active ssh connection, and if not just kill the service
    – k.Cyborg
    Commented Jul 6, 2022 at 20:29
  • 1
    Please make sure there is no XY problem here. Edit the question if there is. Commented Jul 6, 2022 at 21:02
  • 1
    And I suspect you want this for a reason. What is the reason then? sshd -d does what you want, but it's not designed for regular use. Commented Jul 6, 2022 at 21:54
  • 2
    @mikem I meant exactly sshd -d, the debug mode where the tool processes exactly one connection and then exits. Commented Jul 7, 2022 at 3:51
  • 2
    @mikem In my tests sshd -D processes many connections (consecutive, simultaneous, it doesn't matter) just fine, it does not exit. This is exactly what I expected after reading the documentation. sshd -d actually exits after processing the first connection (which counts even if it fails to authenticate, even if it doesn't "speak" SSH). Commented Jul 7, 2022 at 5:17

1 Answer 1


Check this solution out

Or you can:-

alias exit="killall sshd ; exit"

You must log in to answer this question.

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