OpenSSH 源码安装、升级

源码仓库

说明

  1. 原则上 仅安装必要的软件最小化安装所需软件对系统影响最小
  • 源码文档
  • 根据仓库源码中的文档可知,需要使用以下命令才能安装
    autoreconf
    ./configure
    make && make install
    

CentOS/AnolisOS 系列编译安装

  • 本示例以 CentOS 7.9.2009OpenSSH V_9_3_P2 为例
  1. CentOS 7.9.2009 的安装源中,截止 2025-12-10 只能下载到 OpenSSL 1.0.2k-fips
  2. OpenSSH V_9_3_P2 以后的版本(不包含)所需 OpenSSL 最低要求 1.1.1
  3. 如果要安装 OpenSSH V_9_3_P2 以后的版本(不包含),可根据 openssl.md 安装 OpenSSL 1.1.1w
    • 注意:CentOS 7.9.2009 不支持 OpenSSH V_9_9_P1 及之后的版本
    • 注意:CentOS 8.0.1905 支持 OpenSSH V_9_9_P1

下载源码

  • 下载所需安装版本的标签即可

解压

# 解压
# yum -y install tar
tar -zxvf openssh-portable-V_9_3_P2.tar.gz
# 进入解压后的目录
cd openssh-portable-V_9_3_P2

可选安装 autoconf

# 执行 autoreconf 时报错
[root@centos7-9 openssh-portable-V_9_3_P2]# autoreconf
-bash: autoreconf: command not found
[root@centos7-9 openssh-portable-V_9_3_P2]# 
# 安装 autoreconf 命令所需依赖
yum -y install autoconf

# 安装完成,即可正常执行 autoreconf 命令

可选安装 automake

# 执行 autoreconf 时报错
[root@centos7-9 openssh-portable-V_9_3_P2]# autoreconf
Can't exec "aclocal": No such file or directory at /usr/share/autoconf/Autom4te/FileUtils.pm line 326.
autoreconf: failed to run aclocal: No such file or directory
[root@centos7-9 openssh-portable-V_9_3_P2]# 
# 安装 autoreconf 命令所需依赖
yum -y install automake

# 安装完成,即可正常执行 autoreconf 命令

可选安装 gcc

[root@centos7-9 openssh-portable-V_9_3_P2]# ./configure 
checking for cc... no
checking for gcc... no
checking for clang... no
configure: error: in `/root/openssh-portable-V_9_3_P2':
configure: error: no acceptable C compiler found in $PATH
See `config.log' for more details
[root@centos7-9 openssh-portable-V_9_3_P2]# 
# 安装 ./configure 命令所需依赖
yum -y install gcc

# 安装完成,即可正常执行 ./configure 命令

可选安装 zlib-devel

[root@centos7-9 openssh-portable-V_9_3_P2]# ./configure 

......

configure: error: *** zlib.h missing - please install first or check config.log ***
[root@centos7-9 openssh-portable-V_9_3_P2]# 
# 安装 ./configure 命令所需依赖
yum -y install zlib-devel

# 安装完成,即可正常执行 ./configure 命令

可选安装 openssl-devel

[root@centos7-9 openssh-portable-V_9_3_P2]# ./configure 

......

checking for openssl... /usr/bin/openssl
configure: error: *** working libcrypto not found, check config.log
[root@centos7-9 openssh-portable-V_9_3_P2]# 
# 安装 ./configure 命令所需依赖
yum -y install openssl-devel

# 安装完成,即可正常执行 ./configure 命令

可选安装 make

[root@centos7-9 openssh-portable-V_9_3_P2]# make
-bash: make: command not found
[root@centos7-9 openssh-portable-V_9_3_P2]# 
# 安装 make 命令所需依赖
yum -y install make

# 安装完成,即可正常执行 make 命令

完整安装命令

# 不包含测试:耗时短
yum -y install autoconf automake gcc zlib-devel openssl-devel make
autoreconf
./configure
make && make install
source /etc/profile
ssh -V
# 包含测试:耗时长
yum -y install autoconf automake gcc zlib-devel openssl-devel make
autoreconf
./configure
make && make tests && make install
source /etc/profile
ssh -V

修改本地编译的配置文件

本地编译完成后,ssh 配置文件目录:/usr/local/etc/sshd_config

本地编译完成后,ssh 可执行程序目录:/usr/local/sbin/sshd

允许 root 用户远程连接,如果不操作该步骤,更新了可执行程序后,无法使用 root 用户远程连接

允许 root 用户远程连接,如果不操作该步骤,更新了可执行程序后,无法使用 root 用户远程连接

允许 root 用户远程连接,如果不操作该步骤,更新了可执行程序后,无法使用 root 用户远程连接

echo 'PermitRootLogin yes' >> /usr/local/etc/sshd_config

更新 ssh 可执行程序

先执行上一步,允许 root 用户远程连接,否则无法使用 root 用户远程连接

先执行上一步,允许 root 用户远程连接,否则无法使用 root 用户远程连接

先执行上一步,允许 root 用户远程连接,否则无法使用 root 用户远程连接

或者修改 ssh Service:/usr/lib/systemd/system/sshd.service

sudo mv /usr/sbin/sshd /usr/sbin/sshd.bak
sudo cp /usr/local/sbin/sshd /usr/sbin/sshd

重启 ssh

systemctl restart sshd
# systemctl restart ssh

验证 ssh 是否升级成功

使用 telnet 远程连接一次即可

ssh -V 已经显示最新版本了,为什么还要替换可执行程序?

环境变量 $PATH 中,/usr/local/sbin 优先级高于 /usr/sbin

[root@anolis ~]# echo $PATH
/root/.local/bin:/root/bin:/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
[root@anolis ~]# 

ssh Service 使用的可执行程序的路径是 /usr/sbin/sshd

[root@anolis ~]# systemctl status sshd
● sshd.service - OpenSSH server daemon
     Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; preset: enabled)
     Active: active (running) since Fri 2026-06-26 14:59:43 CST; 13min ago
......
[root@anolis ~]#
[root@anolis ~]# cat /usr/lib/systemd/system/sshd.service
[Unit]
Description=OpenSSH server daemon
Documentation=man:sshd(8) man:sshd_config(5)
After=network.target sshd-keygen.target
Wants=sshd-keygen.target
# Migration for Fedora 38 change to remove group ownership for standard host keys
# See https://fedoraproject.org/wiki/Changes/SSHKeySignSuidBit
Wants=ssh-host-keys-migration.service

[Service]
Type=notify
EnvironmentFile=-/etc/sysconfig/sshd
ExecStart=/usr/sbin/sshd -D $OPTIONS
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
RestartSec=42s

[Install]
WantedBy=multi-user.target
[root@anolis ~]# 

本地编译完成的 ssh 配置文件路径是 /usr/local/etc/sshd_config