sshd系统自带启动脚本详解推荐

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=0prog=”sshd”KEYGEN=/usr/bin/ssh-keygenSSHD=/usr/sbin/sshdRSA1_KEY=/etc/ssh/ssh_host_keyRSA_KEY=/etc/ssh/ssh_host_rsa_keyDSA_KEY=/etc/ssh/ssh_host_dsa_keyPID_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 invalidstart(){ # 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=1esacexit $RETVAL#这部分内容在之前的《httpd系统自带启动脚本详解》中有所介绍,在这就不再赘述。 选择逃避,选择被动的去面对生活

sshd系统自带启动脚本详解推荐

相关文章:

你感兴趣的文章:

标签云: