升级openssl和openssh版本ssh9.8p1、ssl3.3.3。 脚本升级和报错处理

#升级脚本

#!/bin/bash
set -e

# 版本变量定义
OPENSSH_VERSION="9.8p1"
OPENSSL_VERSION="3.3.3"

# 检测系统类型
if [ -f /etc/redhat-release ]; then
    SYSTEM="centos"
elif [ -f /etc/lsb-release ]; then
    SYSTEM="ubuntu"
else
    echo "Unsupported system!"
    exit 1
fi

# 安装编译依赖
install_dependencies() {
    if [ "$SYSTEM" = "centos" ]; then
        yum groupinstall -y "Development Tools"
        yum install -y wget pam-devel zlib-devel
    elif [ "$SYSTEM" = "ubuntu" ]; then
        apt-get update
        apt-get install -y build-essential libpam0g-dev zlib1g-dev wget
    fi
}

# 备份SSH配置
backup_ssh() {
    cp -r /etc/ssh /etc/ssh_backup
    cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak
}

# 编译安装OpenSSL
install_openssl() {
    local version=$1
    
    # 检测本地源码包
    if [ -f "openssl-${version}.tar.gz" ]; then
        echo "检测到本地openssl-${version}.tar.gz,跳过下载"
    else
        wget https://www.openssl.org/source/openssl-${version}.tar.gz
    fi

    # 修正原始脚本的语法错误
    tar xzf openssl-${version}.tar.gz
    [ -f /usr/bin/openssl ] && mv /usr/bin/openssl /usr/bin/openssl-bak
    
    cd openssl-${version}
    ./config --prefix=/usr/local/openssl --openssldir=/usr/local/openssl
    make -j$(nproc)
    make install
    echo "/usr/local/openssl/lib64" > /etc/ld.so.conf.d/openssl.conf
    ldconfig
    cd ..
}

# 编译安装OpenSSH
install_openssh() {
    local version=$1
    
    # 检测本地源码包
    if [ -f "openssh-${version}.tar.gz" ]; then
        echo "检测到本地openssh-${version}.tar.gz,跳过下载"
    else
        wget https://cdn.openbsd.org/pub/OpenBSD/OpenSSH/portable/openssh-${version}.tar.gz
    fi

    tar xzf openssh-${version}.tar.gz
    [ -f /usr/bin/ssh ] && mv /usr/bin/ssh /usr/bin/ssh-bak
    
    cd openssh-${version}
    ./configure --prefix=/usr  --with-ssl-dir=/usr/local/openssl --sysconfdir=/etc/ssh --with-pam --with-zlib --with-md5-passwords  
    make -j$(nproc)
    make install
    cd ..
}

# 配置SSH服务(仅CentOS需要)
configure_ssh() {
#    cp contrib/sshd.init /etc/init.d/sshd
#    chmod +x /etc/init.d/sshd
    systemctl daemon-reload
}

main() {
    if [ "$(id -u)" != "0" ]; then
        echo "This script must be run as root" 1>&2
        exit 1
    fi

    install_dependencies
    backup_ssh

    # 安装组件(自动检测本地源码)
    install_openssl $OPENSSL_VERSION
    install_openssh $OPENSSH_VERSION

    # 服务配置
    if [ "$SYSTEM" = "centos" ]; then
        configure_ssh
        systemctl restart sshd
    elif [ "$SYSTEM" = "ubuntu" ]; then
        systemctl restart ssh
    fi

    # 验证版本
    echo -e "\n\033[32m升级完成,当前版本:\033[0m"
    ssh -V 2>&1 | awk '{print $1,$2}'
	echo "export PATH=/usr/local/openssl/bin:$PATH"  >> /etc/profile
	source /etc/profile
    openssl version
}

main

#常见报错

#报错
检测到本地openssl-3.3.3.tar.gz,跳过下载
Can't locate IPC/Cmd.pm in @INC (@INC contains: /home/cmcc/openssl-3.3.3/util/perl /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 . /home/cmcc/openssl-3.3.3/external/perl/Text-Template-1.56/lib) at /home/cmcc/openssl-3.3.3/util/perl/OpenSSL/config.pm line 19.
BEGIN failed--compilation aborted at /home/cmcc/openssl-3.3.3/util/perl/OpenSSL/config.pm line 19.
Compilation failed in require at /home/cmcc/openssl-3.3.3/Configure line 23.
BEGIN failed--compilation aborted at /home/cmcc/openssl-3.3.3/Configure line 23.

#解决
sudo yum install perl-IPC-Cmd

#报错
/etc/ssh/sshd_config line 118: Bad key types 'ssh-rsa,ssh-dss'.
make: [check-config] Error 255 (ignored)
Job for sshd.service failed because the control process exited with error code. See "systemctl status sshd.service" and "journalctl -xe" for details.


#解决
注释掉 HostKeyAlgorithms ssh-rsa,ssh-dss  (加密方式已弃用)
HostKeyAlgorithms rsa-sha2-512,rsa-sha2-256,ecdsa-sha2-nistp256,ssh-ed25519

sudo /usr/sbin/sshd -t -f /etc/ssh/sshd_config
sudo systemctl restart sshd
sudo systemctl status sshd.service
sudo journalctl -xe


#报错
checking whether OpenSSL's headers match the library... no
configure: error: Your OpenSSL headers do not match your
	library. Check config.log for details.
	If you are sure your installation is consistent, you can disable the check
	by running "./configure --without-openssl-header-check".
	Also see contrib/findssl.sh for help identifying header/library mismatches.

#解决



#报错
Failed to start ssh.service: Unit ssh.service is masked.

systemctl status ssh.service --type=masked
sudo systemctl unmask ssh.service
sudo systemctl daemon-reload
sudo systemctl restart ssh.service


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

人生不过大梦一场

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值