歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> 關於Linux >> sshd系統自帶啟動腳本詳解

sshd系統自帶啟動腳本詳解

日期:2017/3/3 16:21:53   编辑:關於Linux

SSH 為 Secure Shell 的縮寫。sshd服務是linux系統中最經常使用的服務之一。由於其規避了明文傳送口令、內容本文及中間人攻擊的安全隱患,因此經常作為遠程管理系統的首選方案。雖然各個linux發行版本或多或少都會有所差異,但sshd服務一定會作為標准配置出現。

本文通過分析/etc/init.d/sshd腳本來理解linux系統是如何處理sshd服務的啟動、關閉等操作的。幫助我們在理解sshd服務的同時,也能夠在遇到問題時快速排查、定位。不詳之處,還望見諒,希望大家能夠多多提出寶貴意見。

#!/bin/bash

#

# Init file for OpenSSH server daemon

#

# chkconfig: 2345 55 25

# description: OpenSSH server daemon

#

# processname: sshd

# 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

#"."等價於source,在這裡等同於C語言中的include。講function中函數及變量的定義導入到當前腳本的執行環境中。

# pull in sysconfig settings

[ -f /etc/sysconfig/sshd ] && . /etc/sysconfig/sshd

#若/etc/sysconfig/sshd為文件,就將其內容導入到當前shell運行環境

RETVAL=0

prog="sshd"

KEYGEN=/usr/bin/ssh-keygen

SSHD=/usr/sbin/sshd

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

#定義變量

do_rsa1_keygen() { #定義函數

if [ ! -s $RSA1_KEY ]; then

echo -n $"Generating SSH1 RSA host key: "

if $KEYGEN -q -t rsa1 -f $RSA1_KEY -C '' -N '' >&/dev/null; then

chmod 600 $RSA1_KEY

chmod 644 $RSA1_KEY.pub

if [ -x /sbin/restorecon ]; then

/sbin/restorecon $RSA1_KEY.pub

fi

success $"RSA1 key generation"

echo

else

failure $"RSA1 key generation"

echo

exit 1

fi

fi

}

#如果/etc/ssh/ssh_host_key不存在就打印"Generating SSH1 RSA host key: "並通過命令ssh-keygen生成類型為rsa1的私鑰及公鑰(.pub),並修改私鑰權限為600,公鑰權限為644。並通過restorecon命令修復公鑰的selinux上下文即屬性。

do_rsa_keygen() {

if [ ! -s $RSA_KEY ]; then

echo -n $"Generating SSH2 RSA host key: "

if $KEYGEN -q -t rsa -f $RSA_KEY -C '' -N '' >&/dev/null; then

chmod 600 $RSA_KEY

chmod 644 $RSA_KEY.pub

if [ -x /sbin/restorecon ]; then

/sbin/restorecon $RSA_KEY.pub

fi

success $"RSA key generation"

echo

else

failure $"RSA key generation"

echo

exit 1

fi

fi

}

#如果/etc/ssh/ ssh_host_rsa_key不存在就打印"Generating SSH2 RSA host key: "並通過命令ssh-keygen生成類型為rsa的私鑰及公鑰(.pub),並修改私鑰權限為600,公鑰權限為644。並通過restorecon命令修復公鑰的selinux上下文即屬性。

do_dsa_keygen() {

if [ ! -s $DSA_KEY ]; then

echo -n $"Generating SSH2 DSA host key: "

if $KEYGEN -q -t dsa -f $DSA_KEY -C '' -N '' >&/dev/null; then

chmod 600 $DSA_KEY

chmod 644 $DSA_KEY.pub

if [ -x /sbin/restorecon ]; then

/sbin/restorecon $DSA_KEY.pub

fi

success $"DSA key generation"

echo

else

failure $"DSA key generation"

echo

exit 1

fi

fi

}

#如果/etc/ssh/ ssh_host_dsa_key不存在就打印"Generating SSH2 DSA host key: "並通過命令ssh-keygen生成類型為dsa的私鑰及公鑰(.pub),並修改私鑰權限為600,公鑰權限為644。並通過restorecon命令修復公鑰的selinux上下文即屬性。

do_restart_sanity_check()

{

$SSHD -t

RETVAL=$?

if [ ! "$RETVAL" = 0 ]; then

failure $"Configuration file or keys are invalid"

echo

fi

}

#-t參數測試配置文件,如果配置文件有問題,提示Configuration file or keys are invalid

start()

{

# Create keys if necessary

do_rsa1_keygen

do_rsa_keygen

do_dsa_keygen

cp -af /etc/localtime /var/empty/sshd/etc

echo -n $"Starting $prog:"

initlog -c "$SSHD $OPTIONS" && success || failure

RETVAL=$?

[ "$RETVAL" = 0 ] && touch /var/lock/subsys/sshd

echo

}

#sshd服務啟動函數,執行之前定義的三個函數,生成rsa、rsa1和dsa方式的公鑰及私鑰。將文件localtime拷貝到/var/empty/sshd/etc,通過initlog命令將執行sshd $OPTIONS的輸出發送到syslog服務,相當於使用logger。即通過日志記錄sshd服務的啟動信息。

stop()

{

echo -n $"Stopping $prog:"

if [ -n "`pidfileofproc $SSHD`" ] ; then

killproc $SSHD

else

failure $"Stopping $prog"

fi

RETVAL=$?

[ "$RETVAL" = 0 ] && rm -f /var/lock/subsys/sshd

echo

}

#通過killproc殺掉進程,並刪除/var/lock/subsys/sshd。可如果sshd服務沒有啟動呢?只有當sshd服務啟動的時候才會殺掉進程,這裡的關鍵是pidfileofproc,這個函數在function中定義。

pidfileofproc() {

local base=${1##*/}

# Test syntax.

if [ "$#" = 0 ] ; then

echo $"Usage: pidfileofproc {program}"

return 1

fi

# First try "/var/run/*.pid" files

if [ -f /var/run/$base.pid ] ; then

local line p pid=

read line < /var/run/$base.pid

for p in $line ; do

[ -z "${p//[0-9]/}" -a -d /proc/$p ] && pid="$pid $p"

done

if [ -n "$pid" ]; then

echo $pid

return 0

fi

fi

}

#目的只有一個就是返回sshd的pid號。 local base=${1##*/} local為定義局部變量,${1##*/}截取變量$1中最後一個"/"後的內容。

reload()

{

echo -n $"Reloading $prog:"

if [ -n "`pidfileofproc $SSHD`" ] ; then

killproc $SSHD -HUP

else

failure $"Reloading $prog"

fi

RETVAL=$?

echo

}

case "$1" in

start)

start

;;

stop)

stop

;;

restart)

stop

start

;;

reload)

reload

;;

condrestart)

if [ -f /var/lock/subsys/sshd ] ; then

do_restart_sanity_check

if [ "$RETVAL" = 0 ] ; then

stop

# avoid race

sleep 3

start

fi

fi

;;

status)

status $SSHD

RETVAL=$?

;;

*)

echo $"Usage: $0 {start|stop|restart|reload|condrestart|status}"

RETVAL=1

esac

exit $RETVAL

#這部分內容在之前的《httpd系統自帶啟動腳本詳解》中有所介紹,在這就不再贅述。

本文出自 “林肯” 博客,請務必保留此出處http://president.blog.51cto.com/4990508/865927

Copyright © Linux教程網 All Rights Reserved