OpenSSH源码离线一键升级脚本分享(含9.9安装包)

一粒云标准网盘

查看当前的版本

[yliyun@linux ~]#ssh -V

相关安装包下载地址

openssh下载地址:http://ftp.openbsd.org/pub/OpenBSD/OpenSSH/portable/

openssl下载地址:https://www.openssl.org/source/

zlib下载地址:http://www.zlib.net/

今天演示从7.4升级到9.9,其他的大家可以自行替换安装包和脚本中的名称即可。

一、脚本内容:

#!/bin/bash
# OpenSSH 离线升级一键脚本(v9.9p1)


# 检查执行权限
if [[ "$(whoami)" != "root" ]]; then
    echo -e "\033[31m错误:必须使用 root 用户执行此脚本!\033[0m" >&2
    exit 1
fi

# 环境检查
check_environment() {
    echo -e "\n\033[34m[1/7] 正在检查系统环境...\033[0m"
    if ! grep -q "CentOS Linux 7.9" /etc/redhat-release; then
        echo -e "\033[31m错误:仅支持 CentOS 7 操作系统!\033[0m"
       # exit 1
    fi
    
    if [ "$(uname -m)" != "x86_64" ]; then
        echo -e "\033[31m错误:仅支持 64 位系统!\033[0m"
        exit 1
    fi
    echo -e "\033[32m环境检查通过\033[0m"
}

# 安装依赖包
install_dependencies() {
    echo -e "\n\033[34m[3/7] 安装基础依赖...\033[0m"
    cd /opt
    tar -xvf yilai.tar.gz
    cd yilai
    rpm -ivh *.rpm --nodeps --force
    echo -e "\033[32m依赖包安装完成\033[0m"
}

# 编译安装 zlib
build_zlib() {
    echo -e "\n\033[34m[4/7] 编译安装 zlib...\033[0m"
    cd /opt
    tar -xvf zlib-1.3.1.tar.gz
    cd zlib-1.3.1
    ./configure --prefix=/usr/local/zlib
    make && make install
    echo'/usr/local/zlib/lib' >> /etc/ld.so.conf
    ldconfig -v
}

# 编译安装 OpenSSL
build_openssl() {
    echo -e "\n\033[34m[5/7] 编译安装 OpenSSL...\033[0m"
    cd /opt
    tar -xvf openssl-1.1.1o.tar.gz
    cd openssl-1.1.1o
    ./config --prefix=/usr/local/ssl -d shared
    make && make install
    echo'/usr/local/ssl/lib' >> /etc/ld.so.conf
    ldconfig -v
}

# 安装 OpenSSH
install_openssh() {
    echo -e "\n\033[34m[6/7] 升级 OpenSSH 到 v9.9p1...\033[0m"
    # 卸载旧版本
    rpm -e --nodeps openssh-server openssh openssh-clients 2>/dev/null
    
    # 编译安装
    cd /opt
    tar -xvf openssh-9.9p1.tar.gz
    cd openssh-9.9p1
    ./configure --prefix=/usr/local/openssh \
        --with-zlib=/usr/local/zlib \
        --with-ssl-dir=/usr/local/ssl
    make && make install

    # 配置文件
    echo'PermitRootLogin yes' >> /usr/local/openssh/etc/sshd_config
    echo'PubkeyAuthentication yes' >> /usr/local/openssh/etc/sshd_config
    echo'PasswordAuthentication yes' >> /usr/local/openssh/etc/sshd_config
    cp /usr/local/openssh/etc/sshd_config /etc/ssh/sshd_config
    echo'HostKeyAlgorithms ssh-rsa,ssh-dss ' >> /etc/ssh/sshd_config
    # 替换系统命令(修复关键点)
    if [ -f /usr/sbin/sshd ]; then
        mv /usr/sbin/sshd /usr/sbin/sshd.bak
    fi
    cp -f /usr/local/openssh/sbin/sshd /usr/sbin/sshd  # 使用新编译的二进制文件

    # 修复权限
    chmod 755 /usr/sbin/sshd
    cp /usr/local/openssh/bin/ssh-keygen /usr/bin/ssh-keygen

    # 复制 ssh 命令
    cp /usr/local/openssh/bin/ssh /usr/bin/ssh
    chmod 755 /usr/bin/ssh

    # 启动脚本
    cp -p contrib/redhat/sshd.init /etc/init.d/sshd
    chmod +x /etc/init.d/sshd
    chkconfig --add sshd
    chkconfig sshd on
}

# 最终验证
final_check() {
    echo -e "\n\033[34m[7/7] 执行最终检查...\033[0m"
    systemctl daemon-reload
    systemctl restart sshd
    ssh -V 2>&1 | grep -q "OpenSSH_9.9p1"
    
    if [ $? -eq 0 ]; then
        echo -e "\033[32m升级成功!当前SSH版本:$(ssh -V 2>&1)\033[0m"
        echo -e "\033[33m警告:请通过新SSH端口连接确认无误后,再关闭Telnet服务!\033[0m"
    else
        echo -e "\033[31m错误:升级失败,请检查日志!\033[0m"
        exit 1
    fi
}

# 主执行流程
main() {
    check_environment
    install_dependencies
    build_zlib
    build_openssl
    install_openssh
    final_check
}

# 执行主函数
main

二、使用说明

文件准备:
将以下文件放在 /opt 目录:

/opt/
├── yilai.tar.gz # 包含所有依赖的RPM包
├── openssh-9.9p1.tar.gz
├── openssl-1.1.1o.tar.gz
├── zlib-1.3.1.tar.gz

将以上脚本编写为upgrade_openssh.sh

赋予权限执行即可。

[yliyun@linux ~]#chmod +x upgrade_openssh.sh
[yliyun@linux ~]#./upgrade_openssh.sh

验证升级

[yliyun@linux ~]#ssh -V  # 应显示 "OpenSSH_9.9p1"
[yliyun@linux ~]#systemctl status sshd

重启验证

[yliyun@linux ~]#reboot

三、离线安装包下载

通过一粒云网盘分享的文件:openssh
链接: http://130.ylicloud.com/links.html?si=2q9mc6 
  【密码:z9ch】