自动升级OPENSSH shell脚本
由于管理着两百多台Linux服务器,一个人搞这么多机器的安全加固比较累,因此在学习了shell脚本之后果断的写了一些常用脚本做一些系统日常维护,本文OPENSSH的升级是博主本人几乎每两三个月就要做一次升级的,没办法绿盟安全扫描系统总是扫描到相关的高危漏洞,再就是OPENSSH版本更新的也比较频繁,因此不偷懒几乎没法活了。废话不说了,在这里贴下脚本,已经在线上服务器上执行了上百次了,应该不会有什么问题。 #!/bin/bash ################################################################# ######updateopensslopensshscirpt######### #####Author:kl##### ######Date:2014/07/13##### ######LastModified:2016/06/02####### ####Warning:starttelnetservicebeforeusethescript##### ################################################################# #################################################################################### # updateopensshandopenssl ######### ##### ## #################################################################################### #Determinewhetherthecurrentsysteminstalledgcccompilertools zlib_version="zlib-1.2.8" openssl_version="openssl-1.0.2g" openssh_version="openssh-7.2p2" gcc_path=`whichgcc` #gcc_name=`basename$gcc_path` DATE=$(date+%Y%m%d) #OSTYPE #Distributor_ID=$(lsb_release-i) Distributor=`lsb_release-i|cut-c17-` #Determinewhethertherootuser userid=`id-u` if["$userid"-ne0];then echo"sorry,onlyrootcanexecutethescript." exit fi #SETSELINUX=disabled if["$Distributor"!="SUSELINUX"];then sed-i'/SELINUX/s/enforcing/disabled/'/etc/selinux/config setenforce0 fi #pam-devel,tcp_wrappers-develneedbeinstalled,Otherwise,thesoftwarewillinstallfailure #Supportfortcpwrappers/libwraphasbeenremovedinopenssh6.7 if!rpm-qa|greppam-devel&>/dev/null;then echo"pam-develisnotinstalled"&&exit fi #if!rpm-qa|greptcp_wrappers-devel&>/dev/null;then # echo"tcp_wrappers-develnotinstalled"&&exit #fi #Checkwhethertoopenthetelnetservice netstat-tnlp|grep-w23 RETVAL3=$? if[$RETVAL3-eq0];then echo"telnetserviceisrunning------------[yes]" else echo"telnetserviceisnotrunning--------[no]" exit fi #Determinewhethertoinstallgccpackage if[-e"$gcc_path"];then echo"gccisinstalled----------------[yes]" else echo"gccisnotinstalled------------[no]" exit fi #stopsshdservice netstat-tnlp|grep-w22 RETVAL4=$? if[$RETVAL4-eq0];then servicesshdstop echo"stopsshdservice--------------[yes]" fi if[-e/etc/init.d/sshd];then cp/etc/init.d/sshd/root fi #removeopenssh*.rpmifexists ifrpm-qa|grepopenssh&>/dev/null; then rpm-qa|grepopenssh>openssh_list.txt whilereadline do rpm-e$line--nodeps echo"remove$linesuccess------------[yes]" done<openssh_list.txt fi ###########installzlib################## tar-zxvf"${zlib_version}.tar.gz">/dev/null cd$zlib_version ./configure RETVAL5=$? if[$RETVAL5-ne0];then echo"Configurezlibhasencounteredanerror" exit fi make RETVAL6=$? if[$RETVAL6-ne0];then echo"makezlibhasencounteredanerror" exit fi makeinstall cd.. echo"#########################################################" echo"#################################" echo"################zlibinstallsuccess#################" echo"#################################" echo"#########################################################" sleep2 ##########installopenssl############# tar-zxvf"${openssl_version}.tar.gz">/dev/null cd$openssl_version ./configsharedzlib RETVAL7=$? if[$RETVAL7-ne0];then echo"Configureopensslhasencounteredanerror" exit fi make RETVAL8=$? if[$RETVAL8-ne0];then echo"makeopensslhasencounteredanerror" exit fi makeinstall if[-e/usr/bin/openssl];then mv/usr/bin/openssl/usr/bin/openssl.OFF&&ln-s/usr/local/ssl/bin/openssl/usr/bin/openssl else ln-s/usr/local/ssl/bin/openssl/usr/bin/openssl fi if[-e/usr/include/openssl];then mv/usr/include/openssl/usr/include/openssl.OFF&&ln-s/usr/local/ssl/include/openssl/usr/include/openssl else ln-s/usr/local/ssl/include/openssl/usr/include/openssl fi ##Add"/usr/local/ssl/lib"to/etc/ld.so.conf ssl_lib=`grep-w"/usr/local/ssl/lib"/etc/ld.so.conf` if[!-e"$ssl_lib"];then echo"/usr/local/ssl/lib">>/etc/ld.so.conf fi ldconfig-v cd.. echo"#########################################################" echo"#################################" echo"################opensslinstallsucess################" echo"#################################" echo"#########################################################" sleep2 #############installopenssh############## if[-e/etc/ssh];then mv/etc/ssh/etc/ssh_$DATE fi tar-zxvf"${openssh_version}.tar.gz">/dev/null cd$openssh_version ./configure--prefix=/usr--sysconfdir=/etc/ssh--with-zlib--with-pam--with-ssl-dir=/usr/local/ssl--with-md5-passwords RETVAL9=$? if[$RETVAL9-ne0];then echo"Configureopensshhasencounteredanerror" exit fi make RETVAL10=$? if[$RETVAL10-ne0-a$RETVAL10-ne0];then echo"makeopensshhasencounteredanerror" exit fi makeinstall if["$Distributor"=="SUSELINUX"];then cdcontrib/suse cprc.sshd/etc/init.d/sshd chmod+x/etc/init.d/sshd chkconfig--addsshd else cdcontrib/redhat cpsshd.init/etc/init.d/sshd chmod+x/etc/init.d/sshd chkconfig--addsshd fi #AgenericPAMconfigurationisincludedas"contrib/sshd.pam.generic", #youmayneedtoedititbeforeusingitonyoursystem. cd.. cpsshd.pam.generic/etc/pam.d/sshd sed-i's/\/lib\/security\///g'/etc/pam.d/sshd #Modify/etc/ssh/sshd_config #Backup/etc/ssh/sshd_config cp-p/etc/ssh/sshd_config/etc/ssh/sshd_config_bak #ThedefaultsetofciphersandMACshasbeenalteredto #removeunsafealgorithms.Inparticular,CBCciphersandarcfour* #aredisabledbydefault. #ChangessinceOpenSSH6.6 echo"KexAlgorithmsdiffie-hellman-group1-sha1,diffie-hellman-group14-sha1,diffie-hellman-group-exchange-sha1,diffie-hellman-group-exchange-sha256,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group1-sha1,curve25519-sha256@libssh.org">>/etc/ssh/sshd_config echo"Ciphersaes128-cbc,aes192-cbc,aes256-cbc,aes128-ctr,aes192-ctr,aes256-ctr,3des-cbc,arcfour128,arcfour256,arcfour,blowfish-cbc,cast128-cbc">>/etc/ssh/sshd_config echo"MACshmac-md5,hmac-sha1,umac-64@openssh.com,hmac-ripemd160,hmac-sha1-96,hmac-md5-96">>/etc/ssh/sshd_config #Disablerootaccessviasshtoserver #*Thedefaultforthesshd_config(5)PermitRootLoginoptionhaschangedfrom"yes"to"prohibit-password". #*PermitRootLogin=without-password/prohibit-passwordnowbansall #interactiveauthenticationmethods,allowingonlypublic-key, #hostbasedandGSSAPIauthentication(previouslyitpermitted #keyboard-interactiveandpassword-lessauthenticationifthose #wereenabled). #PermitRootLoginprohibit-passwordisthedefaultsinceversion7.0p1 sed-i's/^#PermitRootLogin/PermitRootLogin/'/etc/ssh/sshd_config #sed-i'/PermitRootLogin/s/yes/no/'/etc/ssh/sshd_config sed-i'/PermitRootLogin/s/prohibit-password/no/'/etc/ssh/sshd_config #Set'UsePAMno'to'UsePAMyes'toenablePAMauthentication,accountprocessing, #andsessionprocessing sed-i'/^#UsePAMno/aUsePAMyes'/etc/ssh/sshd_config #Startsshdprocess servicesshdstart #Disabletelnetservice ifnetstat-tnlp|grep-w22&>/dev/null;then sed-i'/disable/s/no/yes/'/etc/xinetd.d/telnet servicexinetdrestart fi echo"#########################################################" echo"#################################" echo"################opensshinstallsucess################" echo"#################################" echo"#########################################################" echo"###############sshversion#################################################" echo"###################################################################################" sshd-v echo"####################################################################################" echo"####################################################################################"