# Init file for OpenSSH server daemon
# description: OpenSSH server daemon
# config: /etc/ssh/ssh_host_key
# config: /etc/ssh/ssh_host_key.pub
# config: /etc/ssh/ssh_random_seed
# config: /etc/ssh/sshd_config
# pidfile: /var/run/sshd.pid
# source function library
. /etc/rc.d/init.d/functions
# pull in sysconfig settings
[ -f /etc/sysconfig/sshd ] && . /etc/sysconfig/sshd
# Some functions to make the below more readable
KEYGEN=/usr/bin/ssh-keygen
RSA1_KEY=/etc/ssh/ssh_host_key
RSA_KEY=/etc/ssh/ssh_host_rsa_key
DSA_KEY=/etc/ssh/ssh_host_dsa_key
PID_FILE=/var/run/sshd.pid
runlevel=$(set -- $(runlevel); eval "echo \$#" )
if [ ! -s $RSA1_KEY ]; then
echo -n quot;Generating SSH1 RSA host key: "
if $KEYGEN -q -t rsa1 -f $RSA1_KEY -C '' -N '' >&/dev/null; then
if [ -x /sbin/restorecon ]; then
/sbin/restorecon $RSA1_KEY.pub
success quot;RSA1 key generation"
failure quot;RSA1 key generation"
if [ ! -s $RSA_KEY ]; then
echo -n quot;Generating SSH2 RSA host key: "
if $KEYGEN -q -t rsa -f $RSA_KEY -C '' -N '' >&/dev/null; then
if [ -x /sbin/restorecon ]; then
/sbin/restorecon $RSA_KEY.pub
success quot;RSA key generation"
failure quot;RSA key generation"
if [ ! -s $DSA_KEY ]; then
echo -n quot;Generating SSH2 DSA host key: "
if $KEYGEN -q -t dsa -f $DSA_KEY -C '' -N '' >&/dev/null; then
if [ -x /sbin/restorecon ]; then
/sbin/restorecon $DSA_KEY.pub
success quot;DSA key generation"
failure quot;DSA key generation"
do_restart_sanity_check()
if [ ! "$RETVAL" = 0 ]; then
failure quot;Configuration file or keys are invalid"
# Create keys if necessary
cp -af /etc/localtime /var/empty/sshd/etc
echo -n quot;Starting $prog: "
$SSHD $OPTIONS && success || failure
[ "$RETVAL" = 0 ] && touch /var/lock/subsys/sshd
echo -n quot;Stopping $prog: "
if [ -n "`pidfileofproc $SSHD`" ] ; then
failure quot;Stopping $prog"
# if we are in halt or reboot runlevel kill all running sessions
# so the TCP connections are closed cleanly
if [ "x$runlevel" = x0 -o "x$runlevel" = x6 ] ; then
killall $prog 2>/dev/null
[ "$RETVAL" = 0 ] && rm -f /var/lock/subsys/sshd
echo -n quot;Reloading $prog: "
if [ -n "`pidfileofproc $SSHD`" ] ; then
failure quot;Reloading $prog"
if [ -f /var/lock/subsys/sshd ] ; then
if [ "$RETVAL" = 0 ] ; then
status -p $PID_FILE openssh-daemon
echo quot;Usage: $0 {start|stop|restart|reload|condrestart|status}"