首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >BigCloud Enterprise Linux 8和Rocky Linux 8升级OpenSSH步骤

BigCloud Enterprise Linux 8和Rocky Linux 8升级OpenSSH步骤

原创
作者头像
雪人
修改2025-03-02 10:41:28
修改2025-03-02 10:41:28
7961
举报
文章被收录于专栏:DataOpsDataOps

之前陆续整理过好几次升级openssh的文章,随着时间的推移,也该更新了,正好当下这个升级需求比较多,尤其是安全扫描时往往大部分高危漏洞来自于低版本的openssh,升级后可以解决大部分漏洞。

根据最近的实践,以下操作适应于如下linux版本:

BigCloud Enterprise Linux 8.2/8.6

bclinux/bc-linux/bc linux

Rocky Linux 8.10

一、配置telnet服务、gcc编译器

目的:确保不会因为升级ssh失误导致无法远程登陆主机

■ 确认telnet服务是否已经安装

yum info telnet*

■ 如果没有安装telnet-server

yum install telnet-server -y

■ linux7

安装了telnet-server后,可直接启动telnet-server,不必配置xinetd

systemctl start telnet.socket

■ 此时测试能否远程telnet登陆服务器,确认telnet服务生效后,就可以进行下一步

■ 安装openssh需gcc编译器,确认是否安装

yum info gcc

如没有安装,则安装:

yum install gcc

二、安装新版本zlib【可选】

一般linux8版本的zlib库版本不需要升级

三、安装新版本openssl【可选】

一般linux8版本的openssl库版本不需要升级

四、备份老版本openssh

代码语言:shell
复制
tar cvf /root/sshd20250210.tar /usr/lib/systemd/system/sshd*
mv /usr/lib/systemd/system/sshd-keygen.target{,.ori}
mv /usr/lib/systemd/system/sshd.service{,.ori}
mv /usr/lib/systemd/system/sshd.socket{,.ori}
mv /usr/lib/systemd/system/sshd-keygen@.service{,.ori}
mv /usr/lib/systemd/system/sshd@.service{,.ori}
mv /etc/ssh{,.ori}

五、安装新版本openssh

1、官网下载最新版本源码

【版本9.9p1】

www.openssh.com

最新版本国内镜像地址见下一步。

2、配置、编译、安装
代码语言:shell
复制
wget https://mirrors.aliyun.com/pub/OpenBSD/OpenSSH/portable/openssh-9.9p1.tar.gz
tar xvfz openssh-*.tar.gz
cd openssh-9.9p1
./configure --prefix=/usr/local --sysconfdir=/etc/ssh --with-pam --with-zlib --with-md5-passwords
此时可能会报错:
checking for openssl... /usr/bin/openssl
configure: error: *** working libcrypto not found, check config.log
则需安装 openssl-devel:yum install -y openssl-devel
或者报错:找不到pam文件头,则安装pam-devel包即可:
yum install pam-devel -y
make
make install

注:prefix指定安装路径/usr/local;sysconfdir指定配置文件路径;with-zlib指定新版本zlib路径;with-pam指定启用pam支持;with-ssl-dir指定openssl路径

六、配置新的sshd系统服务

1、生成新的服务配置文件,3个
代码语言:shell
复制
cat > /usr/lib/systemd/system/sshd-keygen.service << EOF
[Unit]
Description=OpenSSH Server Key Generation
ConditionPathExists=|!/etc/ssh/ssh_host_rsa_key
ConditionPathExists=|!/etc/ssh/ssh_host_ecdsa_key
ConditionPathExists=|!/etc/ssh/ssh_host_ed25519_key
ConditionPathExists=|!/etc/ssh/ssh_host_dsa_key
PartOf=sshd.service sshd.socket

[Service]
ExecStart=/usr/local/bin/ssh-keygen
Type=oneshot
RemainAfterExit=yes
EOF

---

cat > /usr/lib/systemd/system/sshd.service << EOF
[Unit]
Description=OpenSSH server daemon
After=network.target sshd-keygen.service
Wants=sshd-keygen.service

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

[Install]
WantedBy=multi-user.target
EOF

---

cat > /usr/lib/systemd/system/sshd.socket << EOF
[Unit]
Description=OpenSSH Server Socket
Conflicts=sshd.service

[Socket]
ListenStream=22
Accept=yes

[Install]
WantedBy=sockets.target
EOF

将3个服务配置文件属性改为644

chmod 644 /usr/lib/systemd/system/sshd*

2、修改sshd配置

【make install时自动生成新sshd配置文件】

参考原sshd配置/etc/ssh/sshd_config,修改新sshd配置

如下命令行操作,确认端口号,打开PAM支持:

代码语言:shell
复制
sed -i "s/#UsePAM no/UsePAM yes/g" /etc/ssh/sshd_config
打开root登录:
sed -i "s/#PermitRootLogin prohibit-password/PermitRootLogin yes/g" /etc/ssh/sshd_config
3、启动sshd服务

systemctl daemon-reload

systemctl restart sshd

systemctl enable sshd

4、其他修改

■ 确认升级后的版本

telnet localhost 22

■ 验证可以远程ssh登陆以后,可以关闭telnet服务

systemctl stop telnet.socket

systemctl disable telnet.socket

如果不能远程登录,一般是因为防火墙和SELINUX设置问题,可以关闭防火墙和关闭SELINUX:

systemctl stop firewalld

systemctl disable firewalld

setenforce 0

永久关闭selinux,修改/etc/selinux/config 文件:

sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config

■ ssh客户端程序更新

mv /usr/bin/ssh /usr/bin/ssh.old

ln -s /usr/local/bin/ssh /usr/bin/ssh

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、配置telnet服务、gcc编译器
  • 二、安装新版本zlib【可选】
  • 三、安装新版本openssl【可选】
  • 四、备份老版本openssh
  • 五、安装新版本openssh
    • 1、官网下载最新版本源码
    • 2、配置、编译、安装
  • 六、配置新的sshd系统服务
    • 1、生成新的服务配置文件,3个
    • 2、修改sshd配置
    • 3、启动sshd服务
    • 4、其他修改
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档