1. 给服务器分组

在/etc/ansible/hosts增加节点dbtest,包含若干服务器ip

  1. 安装包分发

去官网找下载:
http://www.openssh.com/portable.html
https://openbsd.hk/pub/OpenBSD/OpenSSH/portable/
最新是: https://openbsd.hk/pub/OpenBSD/OpenSSH/portable/openssh-8.3p1.tar.gz

wget https://openbsd.hk/pub/OpenBSD/OpenSSH/portable/openssh-8.3p1.tar.gz -o /tmp/openssh-8.3p1.tar.gz

分发:

ansible dbtest-m copy -a “src=/tmp/openssh-8.3p1.tar.gz dest=/tmp/openssh-8.3p1.tar.gz”

  1. 安装基础组件

    ansible dbtest -f6 -m shell -a “yum install -y zlib openssl openssl-dev”

    shell模块可以支持管道符和分号,比command模块更好用。但是awk支持得还不是很好。

    -f6同时6个并发

  2. 编译安装

    ansible dbtest -f6 -m shell -a “cd /tmp/;tar -zxvf openssh-8.3p1.tar.gz” ansible dbtest -f6 -m shell -a “cd /tmp/openssh-8.3p1;./configure && make && make install”

  3. 修改sshd配置文件

关注影响登录的设置,如root是否可以登录,端口号等;备份;修改配置。

[root@deployer ~]# cat /usr/local/etc/sshd_config |grep -Ei “(root|dns|gss|port)”

观察结果,这里我们只修改root登录

ansible dbtest -f6 -mshell -a “cp /usr/local/etc/sshd_config /usr/local/etc/sshd_config.bak” ansible dbtest -f6 -mshell -a “echo \”PermitRootLogin yes\” >> /usr/local/etc/sshd_config;cat /usr/local/etc/sshd_config |grep -Ei \”(root|dns|gss|port)\””

  1. 修改sshd服务文件

    ansible dbtest -f6 -mshell -a “cp /etc/systemd/system/multi-user.target.wants/sshd.service /etc/systemd/system/multi-user.target.wants/sshd.service.bak” ansible dbtest -f6 -mshell -a “sed -i \”s/\/usr\/sbin\/sshd/\/usr\/local\/sbin\/sshd/g\” /etc/systemd/system/multi-user.target.wants/sshd.service” ansible dbtest -f6 -mshell -a “cat /etc/systemd/system/multi-user.target.wants/sshd.service”

如果使用的是openssh目录里/contrib/redhat/sshd.init作为服务启停脚本,这个要改。具体见附件部分。

  1. 重启服务

    先单点测试,先登录上这台服务器,再远程执行,验证可登录后继续,如果不可登录,可以用已登录窗口恢复

    ansible 192.168.36.35 -f6 -mshell -a “systemctl restart sshd”

    验证可登录后继续

    ansible dbtest -f6 -mshell -a “systemctl restart sshd”

  2. 验证

附件:
放于/etc/init.d的sshd文件,放后执行chkconfig on

  1. #!/bin/bash
  2. #
  3. # Init file for OpenSSH server daemon
  4. #
  5. # chkconfig: 2345 55 25
  6. # description: OpenSSH server daemon
  7. #
  8. # processname: sshd
  9. # config: /usr/local/etc/ssh/ssh_host_key
  10. # config: /usr/local/etc/ssh/ssh_host_key.pub
  11. # config: /usr/local/etc/ssh/ssh_random_seed
  12. # config: /usr/local/etc/ssh/sshd_config
  13. # pidfile: /var/run/sshd.pid
  14. SSHD_CONFIG_DIR=/usr/local/etc
  15. SSHD_BIN_DIR=/usr/local/sbin
  16. # source function library
  17. . /etc/rc.d/init.d/functions
  18. # pull in sysconfig settings
  19. [ -f /etc/sysconfig/sshd ] && . /etc/sysconfig/sshd
  20. RETVAL=0
  21. prog="sshd"
  22. # Some functions to make the below more readable
  23. SSHD=${SSHD_BIN_DIR}/sshd
  24. PID_FILE=/var/run/sshd.pid
  25. do_restart_sanity_check()
  26. {
  27. $SSHD -t
  28. RETVAL=$?
  29. if [ $RETVAL -ne 0 ]; then
  30. failure $"Configuration file or keys are invalid"
  31. echo
  32. fi
  33. }
  34. start()
  35. {
  36. # Create keys if necessary
  37. /usr/bin/ssh-keygen -A
  38. if [ -x /sbin/restorecon ]; then
  39. /sbin/restorecon ${SSHD_CONFIG_DIR}/ssh_host_rsa_key.pub
  40. /sbin/restorecon ${SSHD_CONFIG_DIR}/ssh_host_dsa_key.pub
  41. /sbin/restorecon ${SSHD_CONFIG_DIR}/ssh_host_ecdsa_key.pub
  42. fi
  43. echo -n $"Starting $prog:"
  44. $SSHD $OPTIONS && success || failure
  45. RETVAL=$?
  46. [ $RETVAL -eq 0 ] && touch /var/lock/subsys/sshd
  47. echo
  48. }
  49. stop()
  50. {
  51. echo -n $"Stopping $prog:"
  52. killproc $SSHD -TERM
  53. RETVAL=$?
  54. [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/sshd
  55. echo
  56. }
  57. reload()
  58. {
  59. echo -n $"Reloading $prog:"
  60. killproc $SSHD -HUP
  61. RETVAL=$?
  62. echo
  63. }
  64. case "$1" in
  65. start)
  66. start
  67. ;;
  68. stop)
  69. stop
  70. ;;
  71. restart)
  72. stop
  73. start
  74. ;;
  75. reload)
  76. reload
  77. ;;
  78. condrestart)
  79. if [ -f /var/lock/subsys/sshd ] ; then
  80. do_restart_sanity_check
  81. if [ $RETVAL -eq 0 ] ; then
  82. stop
  83. # avoid race
  84. sleep 3
  85. start
  86. fi
  87. fi
  88. ;;
  89. status)
  90. status $SSHD
  91. RETVAL=$?
  92. ;;
  93. *)
  94. echo $"Usage: $0 {start|stop|restart|reload|condrestart|status}"
  95. RETVAL=1
  96. esac
  97. exit $RETVAL