Linux下ssh版本升级步骤
由于项目运维中经常会遇到需要升级openssh版本的问题,并且在次过程中踩过几次坑,特此在这记录一下升级过程用于以后查阅
1. 查看原来的openssh版本
sshd -V
2. 先准备好gcc、zlib、openssl的环境
若是当前服务器能够联网,那么就可以采用yum或apt等命令去下载所需要的各个依赖。
yum -y install gcc pam-devel zlib-devel openssl-devel
麒麟系统手动下载包
首先根据 nkvers确定系统版本,如
Kylin Linux Advanced Server
release V10 (SP1) /(Tercel)-aarch64-Build04/20200711
则系统版本为V10 SP1,进入https://update.cs2c.com.cn/NS/ 找到V10/V10对应版本目录下的/os/adv/lic/base/aarch64/Packages/
如: https://update.cs2c.com.cn/NS/V10/V10SP1/os/adv/lic/base/aarch64/Packages/
然后搜索对应包名下载其中openssl-devel依赖包如下
- e2fsprogs-devel
- keyutils-libs-devel
- libverto-devel
- krb5-devel
3. 对原来的openssh相关的文件进行备份
cp -r -a /etc/ssh/ /etc/ssh.bak
cp -r -a /etc/pam.d/ /etc/pam.d.bak
mv /usr/sbin/sshd /usr/sbin/sshd.bak
mv /usr/bin/ssh /usr/bin/ssh.bak
mv /usr/bin/ssh-keygen /usr/bin/ssh-keygen.bak
4. 将openssh文件上传并解压
tar -zxvf openssh-10.5p1.tar.gz
openssh最新版本发布链接: https://mirrors.aliyun.com/pub/OpenBSD/OpenSSH/portable/
5. 卸载原系统的openssh包
rpm -e --nodeps `rpm -qa | grep openssh`
此命令会将找到的openssh相关的rpm包强制删除。
6. 编译安装openssh
cd /root/openssh-10.5p1
./configure --prefix=/usr/local/openssh-10.5p1 --sysconfdir=/etc/ssh --with-pam --with-ssl-dir=/usr/local/openssl --with-zlib
此处注意检查openssl的路径,因为有的服务器由于不规范的升级过openssl导致存在两套openssl,务必认真鉴别
如果遇到提示ssl版本过低,则升级openssl版本。
升级openssl
参考: https://blog.csdn.net/qq_43127208/article/details/137783403
注意:添加软连接时,因为操作系统架构不一样,文件夹名称可能有出入(lib或者lib64)
配置完成后执行命令
make && make install
7. 复制配置文件
cp /usr/local/openssh-10.5p1/etc/sshd_config /etc/ssh/sshd_config #如被覆盖文件已存在或不存在都可忽略
cp /usr/local/openssh-10.5p1/sbin/sshd /usr/sbin/sshd
cp /usr/local/openssh-10.5p1/bin/ssh /usr/bin/ssh
cp /usr/local/openssh-10.5p1/bin/ssh-keygen /usr/bin/ssh-keygen
cp -p contrib/redhat/sshd.init /etc/init.d/sshd
8. 修改sshd_config文件(重要)
vim /etc/ssh/sshd_config
修改配置项PermitRootLogin 、PubkeyAuthentication、PasswordAuthentication 改为yes.
说明:
- PermitRootLogin yes:允许root用户通过SSH登录到系统
- PubkeyAuthentication yes:启用公钥身份验证
- PasswordAuthentication yes:启用密码身份验证
如果 /etc/ssh/sshd_config文件无法正常保存使用检查文件是否被添加了chattr +i 写保护
[root@app ~]# ls -la /etc/ssh/sshd_config
-rw------- 1 root root 5190 Jun 28 2021 /etc/ssh/sshd_config
[root@app ~]# lsattr /etc/ssh/sshd_config
----i--------------- /etc/ssh/sshd_config
[root@app ~]# chattr -i /etc/ssh/sshd_config
[root@app ~]# vim /etc/ssh/sshd_config
[root@app ~]# sshd -t
[root@app ~]# systemctl restart sshd
[root@app ~]# chattr +i /etc/ssh/sshd_config
9. 修改配置文件权限
chmod 600 /etc/ssh/ssh_host_*_key
chown root:root /etc/ssh/ssh_host_*
10. 如没有sshd服务,则创建服务
vim /etc/systemd/system/sshd.service
并在文件中输入以下内容
[Unit]
Description=OpenSSH 10.5p1 Server Daemon
After=network.target
[Service]
Type=notify
ExecStart=/usr/local/openssh-10.5p1/sbin/sshd -D
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
RestartSec=5s
[Install]
WantedBy=multi-user.target
最后重新加载服务并启动sshd服务
systemctl daemon-reload
systemctl start sshd
systemctl enable sshd
systemctl status sshd
输入命令查看版本:sshd -V
确认pam配置
cat /etc/pam.d/sshd
如果不存在则执行
cat > /etc/pam.d/sshd << 'EOF'
#%PAM-1.0
auth required pam_sepermit.so
auth substack password-auth
auth include postlogin
account required pam_nologin.so
account include password-auth
password include password-auth
# pam_selinux.so close should be the first session rule
session required pam_selinux.so close
session required pam_loginuid.so
session optional pam_keyinit.so force revoke
session include password-auth
session include postlogin
# pam_selinux.so open should only be followed by sessions to be executed in the user context
session required pam_selinux.so open
EOF
重启sshd服务
systemctl restart sshd


浙公网安备 33010602011771号