openssl&&openssh平滑升级

Bertha 。 2022-09-21 01:28 435阅读 0赞

系统安装完成后,默认安装的openssl跟openssh版本较低,有安全隐患,于是对其进行升级,加固安全,首先升级openssl至1.0.2g版本,升级步骤如下

  1. #!/bin/bash
  2. yum install zlib zlib-devel -y
  3. yum remove openssl-devel
  4. cd /data
  5. wget https://openssl.org/source/openssl-1.0.2g.tar.gz
  6. tar zvxf openssl-1.0.2g.tar.gz
  7. cd openssl-1.0.2g
  8. ./config shared zlib
  9. make depend
  10. make && make install
  11. mkdir -pv /tmp/usr/{bin,include}
  12. mv /usr/bin/openssl /tmp/usr/bin/
  13. mv /usr/include/openssl /tmp/usr/include/
  14. ln -sv /usr/local/ssl/bin/openssl /usr/bin/openssl
  15. ln -sv /usr/local/ssl/include/openssl/ /usr/include/openssl
  16. echo "/usr/local/ssl/lib" >> /etc/ld.so.conf
  17. ldconfig -v | grep openssl
  18. openssl version -a

升级openssl完成后再升级openssh,首先添加普通用户并加入wheel组

  1. groupadd test
  2. useradd tide -g test
  3. usermod -G wheel test
  4. echo "截取以下随机数设置test用户密码,用于升级后登录服务器"
  5. openssl rand -base64 30
  6. passwd tide

只允许wheel用户组的用户su切换,其他用户切换root,即使输对密码也会提示 incorrect password

  1. vim /etc/pam.d/su
  2. auth required pam_wheel.so use_uid取消注释

下载安装openssh7.2版本,该版本成功升级后默认不允许root登录

  1. cd /data
  2. mv /etc/ssh /etc/ssh.bak
  3. yum remove openssh
  4. yum install pam-devel
  5. wget http://ftp.openbsd.org/pub/OpenBSD/OpenSSH/portable/openssh-7.2p2.tar.gz
  6. tar zxf openssh-7.2p2.tar.gz
  7. cd openssh-7.2p2
  8. ./configure --prefix=/usr --sysconfdir=/etc/ssh --with-pam --with-zlib --with-md5-passwordsmake
  9. make && make install
  10. chkconfig --add sshd
  11. chkconfig sshd --list
  12. ssh -V
  13. #远程连接服务器操作不可执行restart或者reload,否则会断开连接
  14. service sshd start

安装过程如果服务器启动不了,可能是缺少sshd服务启动脚本

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

接下来配置只能使用密钥文件登录

  1. ssh-keygen -t rsa -P "%fg8PY4DQg=" #密码使用随机openssl生成随机字符串,默认路径,回车
  2. mv id_rsa.pub authorized_keys
  3. chmod 600 authorized_keys

而后下载私钥文件 id_rsa 到本地(可重命名为IP_user_id_rsa),安全保存。
接下来编辑sshd配置文件,取消注释

  1. vim /etc/ssh/sshd_config
  2. RSAAuthentication yes
  3. PubkeyAuthentication yes
  4. AuthorizedKeysFile .ssh/authorized_keys
  5. #关闭密码认证,建议测试完秘钥可登录再开启此项,否则远程连接端口,只能去机房操作服务器
  6. PasswordAuthentication no
  7. PermitEmptyPasswords no
  8. #重启sshd服务,生效配置
  9. service sshd restart

以后登录这台主机就必须以 test用户使用私钥,配合密码短语来登录
其他安全设置

  • 限制登录IP
    vim /etc/hosts.deny sshd:all vim /etc/hosts.allow sshd:192.168.1.1
  • 在/etc/profile中添加:
    登录超时,用户在线5分钟无操作则超时断开连接,
    export TMOUT=300 readonly TMOUT
    减少history命令记录
    HISTSIZE=1000 将该值调小
    每次退出时清理history
    history -c
  • 增强特殊文件权限
    chattr +i /etc/passwd /etc/shadow /etc/ssh/sshd_config lsattr /etc/passwd /etc/shadow /etc/ssh/sshd_config
    修改配置需要先取消特殊权限chattr -i filename
  • 禁ping
    vim /etc/rc.d/rc.local echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_all 或者通过iptables规则限制 ping本机 iptables -A INPUT -p icmp --icmp-type 0 -s 0/0 -j DROP ping其他主机 iptables -A OUTPUT -p icmp --icmp-type 8 -j DROP

参考博文
https://segmentfault.com/a/1190000002532945

发表评论

表情:
评论列表 (有 0 条评论,435人围观)

还没有评论,来说两句吧...

相关阅读

    相关 nginx 的平滑升级

    1、为什么要对 nginx 平滑升级 随着 `nginx` 越来越流行,并且 `nginx` 的优势也越来越明显,`nginx` 的版本迭代也来时加速模式,1.9.0版本

    相关 Nginx平滑升级

    一、环境准备 在业务不关闭情况下进行升级,并且不要版本差距太大,不然很多东西不支持容易崩溃 环境:centos7.3一台 版本:旧的1.8,新的1.10 部署目