BigCloud Enterprise Linux 8和Rocky Linux 8升级OpenSSH步骤
之前陆续整理过好几次升级openssh的文章,随着时间的推移,也该更新了,正好当下这个升级需求比较多,尤其是安全扫描时往往大部分高危漏洞来自于低版本的openssh,升级后可以解决大部分漏洞。
根据最近的实践,以下操作适应于如下linux版本:
BigCloud Enterprise Linux 8.2/8.6
Rocky Linux 8.10
bclinux/bc-linux/bc linux
一、配置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
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、配置、编译、安装
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个
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支持:
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
作者:likingzi
出处:https://www.cnblogs.com/likingzi/p/18737705
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库