由于最新版本的openssh 8.5p1没有rpm安装包,所以需要源码安装

openssh源码包地址: https://cdn.openbsd.org/pub/OpenBSD/OpenSSH/portable/openssh-8.5p1.tar.gz
升级脚本

  1. #! /bin/bash
  2. yum install -y make gcc gcc-c++ openssl-devel zlib zlib-devel pam-devel
  3. cd /usr/local/src/updatessh && tar -zxvf openssh-8.5p1.tar.gz && cd openssh-8.5p1
  4. ./configure --prefix=/usr --sysconfdir=/etc/ssh --with-md5-passwords --with-pam --with-tcp-wrappers --with-ssl-dir=/usr/local/ssl --without-hardening
  5. rm -rf /etc/ssh/ssh_host_*
  6. make && make install
  7. sed -i 's/GSSAPIAuthentication no/#GSSAPIAuthentication no/g' /etc/ssh/sshd_config
  8. sed -i 's/GSSAPIAuthentication yes/#GSSAPIAuthentication no/g' /etc/ssh/sshd_config
  9. sed -i 's/GSSAPICleanupCredentials no/#GSSAPICleanupCredentials no/g' /etc/ssh/sshd_config
  10. sed -i 's/GSSAPICleanupCredentials yes/#GSSAPICleanupCredentials no/g' /etc/ssh/sshd_config
  11. sed -i 's/^#PermitRootLogin/PermitRootLogin/g' /etc/ssh/sshd_config
  12. /usr/sbin/sshd -t -f /etc/ssh/sshd_config 2>sshd_config_check
  13. if `cat sshd_config_check | grep 'Unsupported' wc -l` -ne 0
  14. then
  15. echo "配置文件有问题"
  16. echo "终止操作"
  17. exit 1
  18. fi
  19. cp /usr/local/src/updatessh/openssh-8.5p1/opensshd.init /etc/init.d/sshd.init && chmod u+x /etc/init.d/sshd.init
  20. /etc/init.d/sshd.init start
  21. mv /usr/lib/systemd/system/sshd.service /usr/local/src
  22. touch /usr/lib/systemd/system/sshd.service
  23. echo '# Automatically generated by systemd-sysv-generator
  24. [Unit]
  25. Documentation=man:systemd-sysv-generator(8)
  26. SourcePath=/etc/rc.d/init.d/sshd.init
  27. Description=SYSV: OpenSSH server daemon
  28. [Service]
  29. Type=forking
  30. Restart=no
  31. TimeoutSec=5min
  32. IgnoreSIGPIPE=no
  33. KillMode=process
  34. GuessMainPID=no
  35. RemainAfterExit=no
  36. PIDFile=/var/run/sshd.pid
  37. ExecStart=/etc/rc.d/init.d/sshd.init start
  38. ExecStop=/etc/rc.d/init.d/sshd.init stop
  39. ExecReload=/etc/rc.d/init.d/sshd.init reload' >/usr/lib/systemd/system/sshd.service
  40. systemctl daemon-reload
  41. systemctl restart sshd
  42. ssh -V

ansible-role脚本

  1. mkdir -p role_updatessh/{default,files,handlers,meta,tasks,templates,vars}
  2. vim role_updatessh/tasks/main.yml
  3. - include: UploadFiles.yml
  4. - include: RemoteExcuteScript.yml
  5. vim role_updatessh/UploadFiles.yml
  6. - name: CopyFiles
  7. copy:
  8. src: updatessh.tar.gz
  9. dest: /usr/local/src/
  10. - name: Unarchive
  11. unarchive:
  12. remote_src: yes
  13. src: /usr/local/src/updatessh.tar.gz
  14. dest: /usr/local/src/
  15. vim role_updatessh/RemoteExcuteScript.yml
  16. - name: ExcuteRemoteScript
  17. shell: cd /usr/local/src/updatessh && sh -x updatessh.sh
  18. vim role_updatessh.yml
  19. ---
  20. - name: updatessh
  21. hosts: 172.16.0.11
  22. remote_user: root
  23. roles:
  24. - role_updatessh

注意openssh的源码包和脚本文件压缩后需要放到ansible的files目录下(updatessh.tar.gz)

注意事项

新版openssh已经取消了对GSS验证的支持,如果老的sshd配置文件中有相关开启的选项则sshd服务会无法启动,一定要使用#验证版本sshd -V;sshd -t -f /etc/ssh/sshd_config命令来验证sshd配置文件。验证通过后再使用systemctl restart sshd 替换老的sshd进程为新版sshd进程,否则如果是实体机,就要跑机房了。(本脚本会使用ansible更新sshd,如果出错也会跑机房,慎重使用)

文档更新时间: 2021-05-18 15:54   作者:张尚