升级openssl和openssh脚本
阅读原文时间:2023年07月08日阅读:3
#!/bin/bash

# 原先的版本号信息
# openssl version
#OpenSSL 1.0.2k-fips  26 Jan 2017

# ssh -V
#OpenSSH_7.4p1, OpenSSL 1.0.2k-fips  26 Jan 2017

echo "安装必备软件"
yum -y install unzip wget

echo "下载软件到指定目录并解压"
cd /usr/local/src/
wget https://mirrors.aliyun.com/pub/OpenBSD/OpenSSH/portable/openssh-9.0p1.tar.gz
wget --no-check-certificate https://ftp.openssl.org/source/openssl-1.1.1n.tar.gz
tar -zxvf openssl-1.1.1n.tar.gz
tar -zxvf openssh-9.0p1.tar.gz

echo "安装依赖包"
yum -y install perl perl-devel gcc gcc-c++

echo "编译安装openssl-1.1.1n"
cd /usr/local/src/openssl-1.1.1n
./config
make && make install

echo "解决一些链接库的问题"
ln -s /usr/local/lib64/libssl.so.1.1 /usr/lib64/
ln -s /usr/local/lib64/libcrypto.so.1.1 /usr/lib64/
cp -a libssl.a /usr/local/lib/
cp -a libcrypto.a /usr/local/lib/

echo "查看版本,应该显示的还是原来的版本"
openssl version

echo "查看新安装版本文件的二进制路径"
which openssl

echo "若版本不是新版,需要替换旧版"
cd /usr/bin/
./openssl version
mv openssl openssl102
ln -s /usr/local/bin/openssl /usr/bin/openssl
ls -al openssl*

echo "再次查看版本,应该显示的是新版本"
openssl version

echo "卸载旧版本"
yum erase -y openssl openssl-devel

echo "openssl更新版本完成"
echo "openssl更新版本完成"
echo "openssl更新版本完成"
echo "openssl更新版本完成"
echo "openssl更新版本完成"

echo "查看当前sshd服务"
systemctl status sshd

echo "删除原有配置文件"
cd /etc/
rm -fr ssh

echo "卸载旧版本,不会导致当前远程连接断开"
rpm -qa | grep openssh-*
systemctl stop sshd
yum erase -y openssh-clients openssh-server openssh

echo "查看版本号,完成卸载"
ssh -V

echo "安装依赖包"
yum install -y pam-devel zlib zlib-devel

echo "编译安装新版本ssh"
cd /usr/local/src/openssh-9.0p1
./configure --prefix=/usr/ --sysconfdir=/etc/ssh --with-openssl-includes=/usr/local/include --with-ssl-dir=/usr/local/bin --with-zlib --with-md5-passwords --with-pam

make && make install

echo "查看版本号,完成安装"
ssh -V

echo "设置启动服务"
ls contrib/redhat/sshd.init

cp -a contrib/redhat/sshd.init /etc/init.d/sshd

echo "查看是否有执行权限,若没有,需要 chmod +x 来赋权"
ls -l /etc/init.d/sshd && chmod a+x /etc/init.d/sshd

echo "设置开机启动"
chkconfig --add sshd
/sbin/chkconfig sshd on

echo "启动服务并查看状态"
systemctl start sshd && systemctl status sshd

echo "验证开机启动"
##下面显示2、3、4、5是on就可以,其数字代表启动级别
chkconfig --list sshd

echo "为用户设置登录的权限,结尾添加即可"
echo "permitRootlogin yes" >> /etc/ssh/sshd_config

echo "查看openssl版本会提示找不到文件,再次设置ln软连接"
ln -s /usr/local/bin/openssl /usr/bin/openssl

echo "重启服务"
systemctl restart sshd

echo "sshd更新版本完成"
echo "sshd更新版本完成"
echo "sshd更新版本完成"
echo "sshd更新版本完成"
echo "sshd更新版本完成"

# ssh -V
#OpenSSH_9.0p1, OpenSSL 1.1.1n  15 Mar 2022
# openssl version
#OpenSSL 1.1.1n  15 Mar 2022