You are not logged in.

#1 2009-04-26 00:09:17

tjwallace
Member
Registered: 2009-03-07
Posts: 40
Website

sshd not disconnecting clients before shutdown

I setup an Arch server virtual machine and am having some problems.  When I tell the vm to shutdown over SSH, it shutdowns fine but never closes the shh connection.  I had to add add the servertimeout option otherwise my terminal would freeze.  I tried the same thing with a Ubuntu 9.04 server and it does close the shh connection.  Anything I should check for in the sshd_config?

Offline

#2 2009-04-26 07:36:35

cactus
Taco Eater
From: t͈̫̹ͨa͖͕͎̱͈ͨ͆ć̥̖̝o̫̫̼s͈̭̱̞͍̃!̰
Registered: 2004-05-25
Posts: 4,622
Website

Re: sshd not disconnecting clients before shutdown

To get that behavior, many distros just check to see if the runlevel is 6 (or 0), and then put a 'killall sshd' line into the 'stop' script.

        
    prog=sshd
    if [ "x$runlevel" = x0 -o "x$runlevel" = x6 ] ; then
        killall $prog 2>/dev/null
    fi

I know rc.shutdown checks PRELEVEL, but I don't recall if the init that arch uses (minimal sysv init binary) sets RUNLEVEL=6 on shutdown or not (so you could add a check to the sshd daemon script as above). You could give it a try though.

Make sure it doesn't fire if you do an /etc/rc.d/sshd restart though, as it should only killall sshd on actual system shutdown, and not just daemon shutdown.

Last edited by cactus (2009-04-26 07:37:25)


"Be conservative in what you send; be liberal in what you accept." -- Postel's Law
"tacos" -- Cactus' Law
"t̥͍͎̪̪͗a̴̻̩͈͚ͨc̠o̩̙͈ͫͅs͙͎̙͊ ͔͇̫̜t͎̳̀a̜̞̗ͩc̗͍͚o̲̯̿s̖̣̤̙͌ ̖̜̈ț̰̫͓ạ̪͖̳c̲͎͕̰̯̃̈o͉ͅs̪ͪ ̜̻̖̜͕" -- -̖͚̫̙̓-̺̠͇ͤ̃ ̜̪̜ͯZ͔̗̭̞ͪA̝͈̙͖̩L͉̠̺͓G̙̞̦͖O̳̗͍

Offline

#3 2009-04-26 15:50:20

tjwallace
Member
Registered: 2009-03-07
Posts: 40
Website

Re: sshd not disconnecting clients before shutdown

cactus wrote:

To get that behavior, many distros just check to see if the runlevel is 6 (or 0), and then put a 'killall sshd' line into the 'stop' script.

        
    prog=sshd
    if [ "x$runlevel" = x0 -o "x$runlevel" = x6 ] ; then
        killall $prog 2>/dev/null
    fi

I know rc.shutdown checks PRELEVEL, but I don't recall if the init that arch uses (minimal sysv init binary) sets RUNLEVEL=6 on shutdown or not (so you could add a check to the sshd daemon script as above). You could give it a try though.

Make sure it doesn't fire if you do an /etc/rc.d/sshd restart though, as it should only killall sshd on actual system shutdown, and not just daemon shutdown.

I'm not sure if $runlevel is getting set...  Could I use "who -r" instead?

Offline

#4 2009-04-26 18:01:26

tjwallace
Member
Registered: 2009-03-07
Posts: 40
Website

Re: sshd not disconnecting clients before shutdown

This seems to work:

#!/bin/bash

. /etc/rc.conf
. /etc/rc.d/functions
. /etc/conf.d/sshd

PID=`cat /var/run/sshd.pid 2>/dev/null`
case "$1" in
  start)
    stat_busy "Starting Secure Shell Daemon"
    [ -f /etc/ssh/ssh_host_key ] || { /usr/bin/ssh-keygen -t rsa1 -N "" -f /etc/ssh/ssh_host_key >/dev/null; }
    [ -f /etc/ssh/ssh_host_rsa_key ] || { /usr/bin/ssh-keygen -t rsa -N "" -f /etc/ssh/ssh_host_rsa_key >/dev/null; }
    [ -f /etc/ssh/ssh_host_dsa_key ] || { /usr/bin/ssh-keygen -t dsa -N "" -f /etc/ssh/ssh_host_dsa_key >/dev/null; }
    [ -z "$PID" ] && /usr/sbin/sshd $SSHD_ARGS
    if [ $? -gt 0 ]; then
      stat_fail
    else
      add_daemon sshd
      stat_done
    fi
    ;;
  stop)
    stat_busy "Stopping Secure Shell Daemon"
    [ ! -z "$PID" ]  && kill $PID &> /dev/null
    if [ $? -gt 0 ]; then
      stat_fail
    else
      rm_daemon sshd

      # remove all running sshd processes
      runlevel | grep "[06]" > /dev/null
      if [ $? -eq 0 ] ; then
        printhl "Killing all sshd processes"
        killall -q sshd &> /dev/null
      fi

      stat_done
    fi
    ;;
  restart)
    $0 stop
    sleep 1
    $0 start
    ;;
  *)
    echo "usage: $0 {start|stop|restart}"  
esac
exit 0

Only one minor problem, the killall also kills the script shutting down the sshd daemon.  Does anyone know a way around this?

Also, why isn't something like this the default?

Offline

#5 2009-04-26 18:08:06

tjwallace
Member
Registered: 2009-03-07
Posts: 40
Website

Re: sshd not disconnecting clients before shutdown

Maybe this problem is caused by /etc/rc.shutdown when killall5 is called, since it won't kill the terminal that is calling the shutdown (the SSH terminal, see the man page for killall5)?

Offline

#6 2009-04-26 18:13:17

tjwallace
Member
Registered: 2009-03-07
Posts: 40
Website

Re: sshd not disconnecting clients before shutdown

Also I found this bug report for Gentoo: http://bugs.gentoo.org/show_bug.cgi?id=259183

Also found this: http://www.snailbook.com/faq/background-jobs.auto.html

Last edited by tjwallace (2009-04-26 18:20:38)

Offline

Board footer

Powered by FluxBB