参考出处
链接: https://blog.csdn.net/gsj4719896/article/details/135122636
服务器/opt目录,新增离线安装的rpm,在文章顶部资源中下载
将openssh-9.6p1.tar.gz、openssl-1.1.1q.tar.gz、zlib-1.3.tar.gz、sshupdate2.sh,上传至服务器/opt目录。然后执行脚本sshupdate2.sh,命令如下:sh sshupdate2.sh。等待执行结果即可。查看SSH版本命令:ssh -V
脚本如下,也可在资源中直接下载(centos直接可以用,不用转换)
#!/bin/bash########################################################### Function :openssh-9.6p1 update ## Platform :Centos7.X ## Version :2.0 ## Date :2023-01-03 # #########################################################clearexport LANG="en_US.UTF-8"#修改1:此处的zlib原来的版本zlib-1.2提示下载失败,改完zlib-1.3可正常下载,openssl和openssh下载不同的版本在此处修改版本号即可zlib_version="zlib-1.3"openssl_version="openssl-1.1.1q"openssh_version="openssh-9.6p1" #安装包地址,这里存在一个问题,如果文件夹没有提前建好,后续执行脚本将报错file="/opt"#默认编译路径default="/usr/local"date_time=`date +%Y-%m-%d—%H:%M`#安装目录file_install="$file/openssh_install"file_backup="$file/openssh_backup"file_log="$file/openssh_log" #修改2:为了解决文件夹不存在的问题,我这里添加了创建文件夹的脚本---startif [ ! -d "$file_install" ]; then mkdir "$file_install"fi if [ ! -d "$file_backup" ]; then mkdir "$file_backup"fi if [ ! -d "$file_log" ]; then mkdir "$file_log"fi if [ ! -d "$file_install/zlib" ]; then mkdir "$file_install/zlib"fi#修改2:为了解决文件夹不存在的问题,我这里添加了创建文件夹的脚本---end #源码包链接zlib_download="https://www.zlib.net/$zlib_version.tar.gz"openssl_download="https://www.openssl.org/source/$openssl_version.tar.gz"openssh_download="https://cdn.openbsd.org/pub/OpenBSD/OpenSSH/portable/$openssh_version.tar.gz"Install_make(){# Check if user is rootif [ $(id -u) != "0" ]; thenecho -e "\033[33m--------------------------------------------------------------- \033[0m"echo -e " 当前用户为普通用户,必须使用root用户运行,脚本退出中......" "\033[31m Error\033[0m"echo -e "\033[33m--------------------------------------------------------------- \033[0m"echo ""sleep 4exitfi#判断是否安装wgetecho -e "\033[33m 正在安装Wget...... \033[0m"sleep 2echo ""if ! type wget >/dev/null 2>&1; thenyum install -y wgetelseecho -e "\033[33m--------------------------------------------------------------- \033[0m"echo -e " wget已经安装了:" "\033[32m Please continue\033[0m"echo -e "\033[33m--------------------------------------------------------------- \033[0m"echo ""fi#判断是否安装tarecho -e "\033[33m 正在安装TAR...... \033[0m"sleep 2echo ""if ! type tar >/dev/null 2>&1; thenyum install -y tarelseecho ""echo -e "\033[33m--------------------------------------------------------------- \033[0m"echo -e " tar已经安装了:" "\033[32m Please continue\033[0m"echo -e "\033[33m--------------------------------------------------------------- \033[0m"fiecho ""#安装相关依赖包echo -e "\033[33m 正在安装依赖包...... \033[0m"sleep 3echo ""#yum install gcc gcc-c++ glibc make autoconf openssl openssl-devel pcre-devel pam-devel zlib-devel tcp_wrappers-devel tcp_wrappersif [ $? -eq 0 ];thenecho ""echo -e "\033[33m--------------------------------------------------------------- \033[0m" echo -e " 安装软件依赖包成功 " "\033[32m Success\033[0m"echo -e "\033[33m--------------------------------------------------------------- \033[0m"else echo -e "\033[33m--------------------------------------------------------------- \033[0m" echo -e " 解压源码包失败,脚本退出中......" "\033[31m Error\033[0m"echo -e "\033[33m--------------------------------------------------------------- \033[0m"sleep 4exitfiecho ""}Install_backup(){#创建文件(可修改)mkdir -p $file_installmkdir -p $file_backupmkdir -p $file_logmkdir -p $file_backup/zlibmkdir -p $file_backup/sslmkdir -p $file_backup/sshmkdir -p $file_log/zlibmkdir -p $file_log/sslmkdir -p $file_log/ssh#备份文件(可修改)cp -rf /usr/bin/openssl $file_backup/ssl/openssl_$date_time.bak > /dev/nullcp -rf /etc/init.d/sshd $file_backup/ssh/sshd_$date_time.bak > /dev/nullcp -rf /etc/ssh $file_backup/ssh/ssh_$date_time.bak > /dev/nullcp -rf /usr/lib/systemd/system/sshd.service $file_backup/ssh/sshd_$date_time.service.bak > /dev/nullcp -rf /etc/pam.d/sshd.pam $file_backup/ssh/sshd_$date_time.pam.bak > /dev/null}Remove_openssh(){##并卸载原有的openssh(可修改)rpm -e --nodeps `rpm -qa | grep openssh`}Install_tar(){#下载的源码包,检查是否解压(可修改)#if [ -e $file/$zlib_version.tar.gz ] && [ -e $file/$openssl_version.tar.gz ] && [ -e /$file/$openssh_version.tar.gz ];then#echo -e " 下载软件源码包已存在 " "\033[32m Please continue\033[0m"#else#echo -e "\033[33m 未发现本地源码包,链接检查获取中........... \033[0m "#echo ""#cd $file#wget --no-check-certificate $zlib_download#wget --no-check-certificate $openssl_download#wget --no-check-certificate $openssh_download#echo ""#fi#zlibecho -e "\033[33m 正在下载Zlib软件包...... \033[0m"sleep 3echo ""if [ -e $file/$zlib_version.tar.gz ] ;thenecho -e " 下载软件源码包已存在 " "\033[32m Please continue\033[0m"elseecho -e "\033[33m 未发现zlib本地源码包,链接检查获取中........... \033[0m "sleep 1echo ""cd $filewget --no-check-certificate $zlib_downloadecho ""fi#opensslecho -e "\033[33m 正在下载Openssl软件包...... \033[0m"sleep 3echo ""if [ -e $file/$openssl_version.tar.gz ] ;thenecho -e " 下载软件源码包已存在 " "\033[32m Please continue\033[0m"elseecho -e "\033[33m 未发现openssl本地源码包,链接检查获取中........... \033[0m "echo ""sleep 1cd $filewget --no-check-certificate $openssl_downloadecho ""fi#opensshecho -e "\033[33m 正在下载Openssh软件包...... \033[0m"sleep 3echo ""if [ -e /$file/$openssh_version.tar.gz ];thenecho -e " 下载软件源码包已存在 " "\033[32m Please continue\033[0m"elseecho -e "\033[33m 未发现openssh本地源码包,链接检查获取中........... \033[0m "echo ""sleep 1cd $filewget --no-check-certificate $openssh_downloadfi}echo ""echo ""#安装zlibInstall_zlib(){echo -e "\033[33m 1.1-正在解压Zlib软件包...... \033[0m"sleep 3echo "" cd $file && mkdir -p $file_install && tar -xzf zlib*.tar.gz -C $file_install > /dev/null if [ -d $file_install/$zilb_version ];thenecho -e "\033[33m--------------------------------------------------------------- \033[0m" echo -e " zilb解压源码包成功" "\033[32m Success\033[0m"echo -e "\033[33m--------------------------------------------------------------- \033[0m"echo "" elseecho -e "\033[33m--------------------------------------------------------------- \033[0m" echo -e " zilb解压源码包失败,脚本退出中......" "\033[31m Error\033[0m"echo -e "\033[33m--------------------------------------------------------------- \033[0m" echo "" sleep 4 exit fiecho -e "\033[33m 1.2-正在编译安装Zlib服务.............. \033[0m"sleep 3echo "" #这里原脚本写法:cd $file_install/zlib*,我遇到了cd目录失败问题,将此处修改如下 cd $file_install/$zlib_versionecho -e "$pwd"./configure --prefix=$default/$zlib_version > $file_log/zlib/zlib_configure_$date_time.txt #> /dev/null 2>&1if [ $? -eq 0 ];thenecho -e "\033[33m make... \033[0m"make > /dev/null 2>&1echo $?echo -e "\033[33m make test... \033[0m"make test > /dev/null 2>&1echo $?echo -e "\033[33m make install... \033[0m"make install > /dev/null 2>&1echo $?elseecho -e "\033[33m--------------------------------------------------------------- \033[0m"echo -e " 编译安装压缩库失败,脚本退出中..." "\033[31m Error\033[0m"echo -e "\033[33m--------------------------------------------------------------- \033[0m"echo ""sleep 4exitfi if [ -e $default/$zlib_version/lib/libz.so ];thensed -i '/zlib/'d /etc/ld.so.confecho "$default/$zlib_version/lib" >> /etc/ld.so.confecho "$default/$zlib_version/lib" >> /etc/ld.so.conf.d/zlib.confldconfig -v > $file_log/zlib/zlib_ldconfig_$date_time.txt > /dev/null 2>&1/sbin/ldconfigfi}echo ""echo ""Install_openssl(){echo -e "\033[33m 2.1-正在解压Openssl...... \033[0m"sleep 3echo "" cd $file && tar -xvzf openssl*.tar.gz -C $file_install > /dev/nullif [ -d $file_install/$openssl_version ];thenecho -e "\033[33m--------------------------------------------------------------- \033[0m" echo -e " OpenSSL解压源码包成功" "\033[32m Success\033[0m"echo -e "\033[33m--------------------------------------------------------------- \033[0m" elseecho -e "\033[33m--------------------------------------------------------------- \033[0m" echo -e " OpenSSL解压源码包失败,脚本退出中......" "\033[31m Error\033[0m"echo -e "\033[33m--------------------------------------------------------------- \033[0m" echo "" sleep 4 exit fiecho ""echo -e "\033[33m 2.2-正在编译安装Openssl服务...... \033[0m"sleep 3echo ""cd $file_install/$openssl_version ./config shared zlib --prefix=$default/$openssl_version > $file_log/ssl/ssl_config_$date_time.txt #> /dev/null 2>&1if [ $? -eq 0 ];thenecho -e "\033[33m make clean... \033[0m"make clean > /dev/null 2>&1echo $?echo -e "\033[33m make -j 4... \033[0m"make -j 4 > /dev/null 2>&1echo $?echo -e "\033[33m make install... \033[0m"make install > /dev/null 2>&1echo $?elseecho -e "\033[33m--------------------------------------------------------------- \033[0m"echo -e " 编译安装OpenSSL失败,脚本退出中..." "\033[31m Error\033[0m"echo -e "\033[33m--------------------------------------------------------------- \033[0m"echo ""sleep 4exitfimv /usr/bin/openssl /usr/bin/openssl_$date_time.bak #先备份if [ -e $default/$openssl_version/bin/openssl ];thensed -i '/openssl/'d /etc/ld.so.confecho "$default/$openssl_version/lib" >> /etc/ld.so.confln -s $default/$openssl_version/bin/openssl /usr/bin/opensslln -s $default/$openssl_version/lib/libssl.so.1.1 /usr/lib64/libssl.so.1.1 ln -s $default/$openssl_version/lib/libcrypto.so.1.1 /usr/lib64/libcrypto.so.1.1 ldconfig -v > $file_log/ssl/ssl_ldconfig_$date_time.txt > /dev/null 2>&1/sbin/ldconfigecho -e "\033[33m--------------------------------------------------------------- \033[0m"echo -e " 编译安装OpenSSL " "\033[32m Success\033[0m"echo -e "\033[33m--------------------------------------------------------------- \033[0m"echo ""echo -e "\033[33m 2.3-正在输出 OpenSSL 版本状态.............. \033[0m"sleep 3echo ""echo -e "\033[32m====================== OpenSSL veriosn ===================== \033[0m"echo ""openssl version -aecho ""echo -e "\033[32m======================================================= \033[0m"sleep 2elseecho ""echo -e "\033[33m--------------------------------------------------------------- \033[0m"echo -e " OpenSSL软连接失败,脚本退出中..." "\033[31m Error\033[0m"echo -e "\033[33m--------------------------------------------------------------- \033[0m"fi}echo ""echo ""Install_openssh(){echo -e "\033[33m 3.1-正在解压OpenSSH...... \033[0m"sleep 3echo ""cd $file && tar -xvzf openssh*.tar.gz -C $file_install > /dev/nullif [ -d $file_install/$openssh_version ];thenecho -e "\033[33m--------------------------------------------------------------- \033[0m" echo -e " OpenSSh解压源码包成功" "\033[32m Success\033[0m"echo -e "\033[33m--------------------------------------------------------------- \033[0m" elseecho -e "\033[33m--------------------------------------------------------------- \033[0m" echo -e " OpenSSh解压源码包失败,脚本退出中......" "\033[31m Error\033[0m"echo -e "\033[33m--------------------------------------------------------------- \033[0m" echo "" sleep 4 exit fiecho ""echo -e "\033[33m 3.2-正在编译安装OpenSSH服务...... \033[0m"sleep 3echo ""mv /etc/ssh /etc/ssh_$date_time.bak #先备份cd $file_install/$openssh_version./configure --prefix=$default/$openssh_version --sysconfdir=/etc/ssh --with-ssl-dir=$default/$openssl_version --with-zlib=$default/$zlib_version > $file_log/ssh/ssh_configure_$date_time.txt #> /dev/null 2>&1if [ $? -eq 0 ];thenecho -e "\033[33m make -j 4... \033[0m"make -j 4 > /dev/null 2>&1echo $?echo -e "\033[33m make install... \033[0m"make install > /dev/null 2>&1echo $?elseecho -e "\033[33m--------------------------------------------------------------- \033[0m"echo -e " 编译安装OpenSSH失败,脚本退出中......" "\033[31m Error\033[0m"echo -e "\033[33m--------------------------------------------------------------- \033[0m"echo ""sleep 4exitfiecho ""echo -e "\033[33m--------------------------------------------------------------- \033[0m"echo -e " 编译安装OpenSSH " "\033[32m Success\033[0m"echo -e "\033[33m--------------------------------------------------------------- \033[0m"echo ""sleep 2echo -e "\033[32m==================== OpenSSH—file veriosn =================== \033[0m"echo ""/usr/local/$openssh_version/bin/ssh -Vecho ""echo -e "\033[32m======================================================= \033[0m"sleep 3echo ""echo -e "\033[33m 3.3-正在迁移OpenSSH配置文件...... \033[0m"sleep 3echo ""#迁移sshdif [ -f "/etc/init.d/sshd" ];thenmv /etc/init.d/sshd /etc/init.d/sshd_$date_time.bakelseecho -e " /etc/init.d/sshd不存在 " "\033[31m Not backed up(可忽略)\033[0m"ficp -rf $file_install/$openssh_version/contrib/redhat/sshd.init /etc/init.d/sshd;chmod u+x /etc/init.d/sshd;chkconfig --add sshd ##自启动chkconfig --list |grep sshd;chkconfig sshd on#备份启动脚本,不一定有if [ -f "/usr/lib/systemd/system/sshd.service" ];thenmv /usr/lib/systemd/system/sshd.service /usr/lib/systemd/system/sshd.service_bakelseecho -e " sshd.service不存在" "\033[31m Not backed up(可忽略)\033[0m"fi#备份复制sshd.pam文件if [ -f "/etc/pam.d/sshd.pam" ];thenmv /etc/pam.d/sshd.pam /etc/pam.d/sshd.pam_$date_time.bak else echo -e " sshd.pam不存在" "\033[31m Not backed up(可忽略)\033[0m"ficp -rf $file_install/$openssh_version/contrib/redhat/sshd.pam /etc/pam.d/sshd.pam#迁移ssh_configcp -rf $file_install/$openssh_version/sshd_config /etc/ssh/sshd_configsed -i 's/Subsystem/#Subsystem/g' /etc/ssh/sshd_configecho "Subsystem sftp $default/$openssh_version/libexec/sftp-server" >> /etc/ssh/sshd_configcp -rf $default/$openssh_version/sbin/sshd /usr/sbin/sshdcp -rf /$default/$openssh_version/bin/ssh /usr/bin/sshcp -rf $default/$openssh_version/bin/ssh-keygen /usr/bin/ssh-keygensed -i 's/#PasswordAuthentication\ yes/PasswordAuthentication\ yes/g' /etc/ssh/sshd_config#grep -v "[[:space:]]*#" /etc/ssh/sshd_config |grep "PubkeyAuthentication yes"echo 'PermitRootLogin yes' >> /etc/ssh/sshd_config#重启sshdservice sshd start > /dev/null 2>&1if [ $? -eq 0 ];thenecho -e "\033[33m--------------------------------------------------------------- \033[0m"echo -e " 启动OpenSSH服务成功" "\033[32m Success\033[0m"echo -e "\033[33m--------------------------------------------------------------- \033[0m"echo ""sleep 2#删除源码包(可修改)rm -rf $file/*$zlib_version.tar.gzrm -rf $file/*$openssl_version.tar.gzrm -rf $file/*$openssh_version.tar.gz#rm -rf $file_installecho -e "\033[33m 3.4-正在输出 OpenSSH 版本...... \033[0m"sleep 3echo ""echo -e "\033[32m==================== OpenSSH veriosn =================== \033[0m"echo ""ssh -Vecho ""echo -e "\033[32m======================================================== \033[0m"elseecho -e "\033[33m--------------------------------------------------------------- \033[0m"echo -e " 启动OpenSSH服务失败,脚本退出中......" "\033[31m Error\033[0m"echo -e "\033[33m--------------------------------------------------------------- \033[0m"sleep 4exitfiecho ""}End_install(){##sshd状态echo ""echo -e "\033[33m 输出sshd服务状态: \033[33m"sleep 2echo ""systemctl status sshd.serviceecho ""echo ""echo ""sleep 1echo -e "\033[33m==================== OpenSSH file =================== \033[0m"echo ""echo -e " Openssh升级安装目录请前往: "cd $file_install && pwdcd ~echo ""echo -e " Openssh升级备份目录请前往: " cd $file_backup && pwdcd ~echo ""echo -e " Openssh升级日志目录请前往: "cd $file_log && pwdcd ~echo ""echo -e "\033[33m======================================================= \033[0m"}Install_makeInstall_backupRemove_opensshInstall_tarInstall_zlibInstall_opensslInstall_opensshEnd_install
升级成功别急着退出连接,先检查下配置文件,不然华为云堡垒机可能会出现连接不上的问题!!!
我的服务器配置文件地址:/etc/ssh/sshd_config
需要保证该配置文件有如下内容:HostKeyAlgorithms ssh-rsa,ssh-dss。若没有的话,手动加上在末尾。