Centos7升级openssh到最新版亲测最详细步骤

0. 注意事项

整个过程不需要卸载原先的 openssl 包和 openssh 的 rpm 包、不影响我们的操作。

本文的环境都是系统自带的 openssh,没有经历过手动编译安装方式。如果之前有手动编译安装过 openssh,请参照本文自行测试是否能成功。

1. 原系统信息(未升级前)

1.1 系统版本

  1. # cat /etc/redhat-release
  2. CentOS Linux release 7.7.1908 (Core)

1.2 原 openssl 版本

  1. # openssl version
  2. OpenSSL 1.0.2k-fips 26 Jan 2017

1.3 原 openssh 版本

  1. # ssh -V
  2. OpenSSH_7.4p1, OpenSSL 1.0.2k-fips 26 Jan 2017

2. 配置更新环境

2.1 yum 更新 openssh

yum update openssh 升级下到目前 yum 仓库默认的 openssh7.4p1 版本。(这里准备统一 openssh 版本为 7.4p1 之后再统一编译安装升级到 openssh8.0p1)

  1. # openssl version
  2. OpenSSL 1.0.2k-fips 26 Jan 2017
  3. # ssh -V
  4. OpenSSH_7.4p1, OpenSSL 1.0.2k-fips 26 Jan 2017

2.2 安装 telnet-server 以及 xinetd

因为我们现在是远程更新 openssh 所以需要先使用另一种连接方式连接到服务器进行相关操作。

  1. yum install xinetd telnet-server -y
  2. 已加载插件:fastestmirror, langpacks
  3. Loading mirror speeds from cached hostfile
  4. * base: mirrors.tuna.tsinghua.edu.cn
  5. * extras: mirrors.tuna.tsinghua.edu.cn
  6. * nux-dextop: mirror.li.nux.ro
  7. * updates: mirrors.tuna.tsinghua.edu.cn

2.3 配置 telnet

现在很多 centos7 版本安装 telnet-server 以及 xinetd 之后没有一个叫 telnet 的配置文件了。

如果下面 telnet 文件不存在的话,可以跳过这部分的更改(我所测试的时候并没有此项文件)。

  1. # ll /etc/xinetd.d/telnet
  2. ls: 无法访问/etc/xinetd.d/telnet: 没有那个文件或目录

如果下面文件存在,请更改配置 telnet 可以 root 登录,把 disable = no 改成 disable = yes

  1. # cat /etc/xinetd.d/telnet
  2. # default: on
  3. # description: The telnet server serves telnet sessions; it uses \
  4. # unencrypted username/password pairs for authentication.
  5. service telnet
  6. {
  7. disable = no
  8. flags = REUSE
  9. socket_type = stream
  10. wait = no
  11. user = root
  12. server = /usr/sbin/in.telnetd
  13. log_on_failure += USERID
  14. }
  15. [root@rhel yum.repos.d]# vim /etc/xinetd.d/telnet
  16. [root@rhel yum.repos.d]# cat /etc/xinetd.d/telnet
  17. # default: on
  18. # description: The telnet server serves telnet sessions; it uses \
  19. # unencrypted username/password pairs for authentication.
  20. service telnet
  21. {
  22. disable = yes
  23. flags = REUSE
  24. socket_type = stream
  25. wait = no
  26. user = root
  27. server = /usr/sbin/in.telnetd
  28. log_on_failure += USERID
  29. }

2.4 配置 telnet 登录的终端类型

/etc/securetty 文件末尾增加一些 pts 终端,如下

  1. pts/0
  2. pts/1
  3. pts/2
  4. pts/3

2.5 启动 telnet 服务,并设置开机自动启动

  1. # systemctl start telnet.socket
  2. # systemctl enable telnet.socket
  3. Created symlink from /etc/systemd/system/sockets.target.wants/telnet.socket to /usr/lib/systemd/system/telnet.socket.
  4. # systemctl restart telnet.socket
  5. # systemctl status telnet.socket
  6. telnet.socket - Telnet Server Activation Socket
  7. Loaded: loaded (/usr/lib/systemd/system/telnet.socket; disabled; vendor preset: disabled)
  8. Active: active (listening) since 2019-09-19 19:33:58 CST; 14s ago
  9. Docs: man:telnetd(8)
  10. Listen: [::]:23 (Stream)
  11. Accepted: 0; Connected: 0
  12. 9 19 19:33:58 sz-lab-centos7-gitlab-nginx-proxy-192.168.178.46 systemd[1]: Closed Telnet Server Activation Socket.
  13. 9 19 19:33:58 sz-lab-centos7-gitlab-nginx-proxy-192.168.178.46 systemd[1]: Stopping Telnet Server Activation Socket.
  14. 9 19 19:33:58 sz-lab-centos7-gitlab-nginx-proxy-192.168.178.46 systemd[1]: Listening on Telnet Server Activation Socket.

2.6 查看端口监听状态、确认 telnet 工作状态

  1. # netstat -lntp|grep 23
  2. tcp 0 0 127.0.0.1:9236 0.0.0.0:* LISTEN 4858/gitaly
  3. tcp 0 0 192.168.122.1:53 0.0.0.0:* LISTEN 2334/dnsmasq
  4. tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 29723/cupsd
  5. tcp6 0 0 :::23 :::* LISTEN 1/systemd
  6. tcp6 0 0 ::1:631 :::* LISTEN 29723/cupsd

2.7 添加防火墙规则

  1. # firewall-cmd --list-all
  2. public (active)
  3. target: default
  4. icmp-block-inversion: no
  5. interfaces: enp0s31f6
  6. sources:
  7. services: dhcpv6-client ftp ssh zabbix-agent
  8. ports:
  9. protocols:
  10. masquerade: no
  11. forward-ports:
  12. source-ports:
  13. icmp-blocks:
  14. rich rules:
  15. # firewall-cmd --add-service=telnet --permanent
  16. success
  17. # firewall-cmd --reload
  18. success
  19. # firewall-cmd --list-all
  20. public (active)
  21. target: default
  22. icmp-block-inversion: no
  23. interfaces: enp0s31f6
  24. sources:
  25. services:dhcpv6-client ftp ssh telnet zabbix-agent
  26. ports:
  27. protocols:
  28. masquerade: no
  29. forward-ports:
  30. source-ports:
  31. icmp-blocks:
  32. rich rules:

2.8 telnet 连接目标服务器

  1. # telnet 192.168.1.2
  2. # 输入用户名密码
  3. # 回车登录

2.9 安装依赖包

  1. # yum install -y gcc gcc-c++ glibc make autoconf openssl openssl-devel pcre-devel pam-devel
  2. Loading mirror speeds from cached hostfile
  3. * base: mirrors.tuna.tsinghua.edu.cn
  4. * extras: mirrors.tuna.tsinghua.edu.cn
  5. * nux-dextop: mirror.li.nux.ro
  6. * updates: mirrors.tuna.tsinghua.edu.cn

2.10 安装 pam 和 zlib 等

  1. # yum install -y pam* zlib*
  2. 已加载插件:fastestmirror, langpacks
  3. Loading mirror speeds from cached hostfile
  4. * base: mirrors.tuna.tsinghua.edu.cn
  5. * extras: mirrors.tuna.tsinghua.edu.cn
  6. * nux-dextop: mirror.li.nux.ro
  7. * updates: mirrors.tuna.tsinghua.edu.cn

3. 下载所需文件

选择一个你所喜欢的文件夹保存下面你所下载的文件。

3.1 下载最新版本的 openssh

https://openbsd.hk/pub/OpenBSD/OpenSSH/portable/
https://mirrors.aliyun.com/pub/OpenBSD/OpenSSH/portable/

3.2 下载最新版本的 openssl

Index of /source

  1. # ls
  2. openssh-8.0p1.tar.gz openssl-1.1.1d.tar.gz

4. 开始安装

4.1 安装 openssl

4.1.1 解压缩

  1. # tar xfz openssh-8.0p1.tar.gz
  2. # ls
  3. openssh-8.0p1 openssh-8.0p1.tar.gz openssl-1.1.1d.tar.gz
  4. # tar xfz openssl-1.1.1d.tar.gz
  5. # ls
  6. openssh-8.0p1 openssh-8.0p1.tar.gz openssl-1.1.1d openssl-1.1.1d.tar.gz
  7. #

4.1.2 备份原文件

  1. # ll /usr/bin/openssl
  2. -rwxr-xr-x 1 root root 555288 8 9 09:38 /usr/bin/openssl
  3. # mv /usr/bin/openssl /usr/bin/openssl_bak
  4. # mv /usr/include/openssl /usr/include/openssl_bak
  5. # ll /usr/include/openssl_bak/
  6. 总用量 1864
  7. -rw-r--r-- 1 root root 6146 8 9 09:38 aes.h
  8. -rw-r--r-- 1 root root 63204 8 9 09:38 asn1.h
  9. -rw-r--r-- 1 root root 24435 8 9 09:38 asn1_mac.h
  10. -rw-r--r-- 1 root root 34475 8 9 09:38 asn1t.h
  11. -rw-r--r-- 1 root root 38742 8 9 09:38 bio.h
  12. ...

4.1.3 开始编译安装

  1. # cd openssl-1.1.1d/
  2. # ./config shared && make && make install

4.1.4 后续配置

查看编译安装后的 openssl 的目录结构、默认安装到 /usr/local 目录下

  1. # ls /usr/local/
  2. bin/ etc/ games/ include/ lib/ lib64/ libexec/ sbin/ ServerStatus/ share/ src/ ssl/
  3. # tree -L 2
  4. .
  5. ├── bin
  6. ├── c_rehash
  7. └── openssl
  8. ├── include
  9. └── openssl
  10. ├── aes.h
  11. ├── asn1err.h
  12. ....
  13. ├── lib64
  14. ├── engines-1.1
  15. ├── libcrypto.a
  16. ├── libcrypto.so -> libcrypto.so.1.1
  17. ├── libcrypto.so.1.1
  18. ├── libssl.a
  19. ├── libssl.so -> libssl.so.1.1
  20. ├── libssl.so.1.1
  21. └── pkgconfig
  22. └── ssl
  23. ├── certs
  24. ├── ct_log_list.cnf
  25. ├── ct_log_list.cnf.dist
  26. ├── misc
  27. ├── openssl.cnf
  28. ├── openssl.cnf.dist
  29. └── private

4.1.4 软连接 openssl 目录

  1. # ln -s /usr/local/bin/openssl /usr/bin/openssl
  2. # ln -s /usr/local/include/openssl/ /usr/include/openssl
  3. # ll /usr/bin/openssl
  4. lrwxrwxrwx 1 root root 22 9 19 20:14 /usr/bin/openssl -> /usr/local/bin/openssl
  5. # ll /usr/include/openssl -ld
  6. lrwxrwxrwx 1 root root 27 9 19 20:14 /usr/include/openssl -> /usr/local/include/openssl/

4.1.5 加载新配置

  1. echo "/usr/local/lib64" >> /etc/ld.so.conf
  2. /sbin/ldconfig

4.1.6 查看确认版本。没问题

  1. # openssl version
  2. OpenSSL 1.1.1d 10 Sep 2019

4.2 安装 openssh

4.2.1 解压并设置权限

  1. # tar xfz openssh-8.0p1.tar.gz
  2. # cd openssh-8.0p1
  3. # 可能文件默认显示uid和gid数组都是1000,这里重新授权下。不授权可能也不影响安装(请自行测试)
  4. # chown -R root.root /data/tools/openssh-8.0p1

4.2.2 备份原 ssh 的配置文件和目录

  1. # mv /etc/ssh/* your_backup_dir

4.2.3 配置、编译、安装

  1. 查看编译参数、根据需要选择
  1. # ./configure -h
  2. `configure' configures OpenSSH Portable to adapt to many kinds of systems.
  3. Usage: ./configure [OPTION]... [VAR=VALUE]...
  4. To assign environment variables (e.g., CC, CFLAGS...), specify them as
  5. VAR=VALUE. See below for descriptions of some of the useful variables.
  6. Defaults for the options are specified in brackets.
  7. Configuration:
  8. -h, --help display this help and exit
  9. --help=short display options specific to this package
  10. --help=recursive display the short help of all the included packages
  11. -V, --version display version information and exit
  12. -q, --quiet, --silent do not print `checking ...' messages
  13. --cache-file=FILE cache test results in FILE [disabled]
  14. -C, --config-cache alias for `--cache-file=config.cache'
  15. -n, --no-create do not create output files
  16. --srcdir=DIR find the sources in DIR [configure dir or `..']
  17. Installation directories:
  18. --prefix=PREFIX install architecture-independent files in PREFIX
  19. [/usr/local]
  20. --exec-prefix=EPREFIX install architecture-dependent files in EPREFIX
  21. [PREFIX]
  22. By default, `make install' will install all the files in
  23. `/usr/local/bin', `/usr/local/lib' etc. You can specify
  24. an installation prefix other than `/usr/local' using `--prefix',
  25. for instance `--prefix=$HOME'.
  26. For better control, use the options below.
  27. Fine tuning of the installation directories:
  28. --bindir=DIR user executables [EPREFIX/bin]
  29. --sbindir=DIR system admin executables [EPREFIX/sbin]
  30. --libexecdir=DIR program executables [EPREFIX/libexec]
  31. --sysconfdir=DIR read-only single-machine data [PREFIX/etc]
  32. --sharedstatedir=DIR modifiable architecture-independent data [PREFIX/com]
  33. --localstatedir=DIR modifiable single-machine data [PREFIX/var]
  34. --libdir=DIR object code libraries [EPREFIX/lib]
  35. --includedir=DIR C header files [PREFIX/include]
  36. --oldincludedir=DIR C header files for non-gcc [/usr/include]
  37. --datarootdir=DIR read-only arch.-independent data root [PREFIX/share]
  38. --datadir=DIR read-only architecture-independent data [DATAROOTDIR]
  39. --infodir=DIR info documentation [DATAROOTDIR/info]
  40. --localedir=DIR locale-dependent data [DATAROOTDIR/locale]
  41. --mandir=DIR man documentation [DATAROOTDIR/man]
  42. --docdir=DIR documentation root [DATAROOTDIR/doc/openssh]
  43. --htmldir=DIR html documentation [DOCDIR]
  44. --dvidir=DIR dvi documentation [DOCDIR]
  45. --pdfdir=DIR pdf documentation [DOCDIR]
  46. --psdir=DIR ps documentation [DOCDIR]
  47. System types:
  48. --build=BUILD configure for building on BUILD [guessed]
  49. --host=HOST cross-compile to build programs to run on HOST [BUILD]
  50. Optional Features:
  51. --disable-option-checking ignore unrecognized --enable/--with options
  52. --disable-FEATURE do not include FEATURE (same as --enable-FEATURE=no)
  53. --enable-FEATURE[=ARG] include FEATURE [ARG=yes]
  54. --disable-largefile omit support for large files
  55. --disable-pkcs11 disable PKCS#11 support code [no]
  56. --disable-strip Disable calling strip(1) on install
  57. --disable-etc-default-login Disable using PATH from /etc/default/login no
  58. --disable-lastlog disable use of lastlog even if detected no
  59. --disable-utmp disable use of utmp even if detected no
  60. --disable-utmpx disable use of utmpx even if detected no
  61. --disable-wtmp disable use of wtmp even if detected no
  62. --disable-wtmpx disable use of wtmpx even if detected no
  63. --disable-libutil disable use of libutil (login() etc.) no
  64. --disable-pututline disable use of pututline() etc. (uwtmp) no
  65. --disable-pututxline disable use of pututxline() etc. (uwtmpx) no
  66. Optional Packages:
  67. --with-PACKAGE[=ARG] use PACKAGE [ARG=yes]
  68. --without-PACKAGE do not use PACKAGE (same as --with-PACKAGE=no)
  69. --without-openssl Disable use of OpenSSL; use only limited internal crypto **EXPERIMENTAL**
  70. --without-stackprotect Don't use compiler's stack protection
  71. --without-hardening Don't use toolchain hardening flags
  72. --without-rpath Disable auto-added -R linker paths
  73. --with-cflags Specify additional flags to pass to compiler
  74. --with-cflags-after Specify additional flags to pass to compiler after configure
  75. --with-cppflags Specify additional flags to pass to preprocessor
  76. --with-ldflags Specify additional flags to pass to linker
  77. --with-ldflags-after Specify additional flags to pass to linker after configure
  78. --with-libs Specify additional libraries to link with
  79. --with-Werror Build main code with -Werror
  80. --with-solaris-contracts Enable Solaris process contracts (experimental)
  81. --with-solaris-projects Enable Solaris projects (experimental)
  82. --with-solaris-privs Enable Solaris/Illumos privileges (experimental)
  83. --with-osfsia Enable Digital Unix SIA
  84. --with-zlib=PATH Use zlib in PATH
  85. --without-zlib-version-check Disable zlib version check
  86. --with-ldns[=PATH] Use ldns for DNSSEC support (optionally in PATH)
  87. --with-libedit[=PATH] Enable libedit support for sftp
  88. --with-audit=module Enable audit support (modules=debug,bsm,linux)
  89. --with-pie Build Position Independent Executables if possible
  90. --with-ssl-dir=PATH Specify path to OpenSSL installation
  91. --without-openssl-header-check Disable OpenSSL version consistency check
  92. --with-ssl-engine Enable OpenSSL (hardware) ENGINE support
  93. --with-prngd-port=PORT read entropy from PRNGD/EGD TCP localhost:PORT
  94. --with-prngd-socket=FILE read entropy from PRNGD/EGD socket FILE (default=/var/run/egd-pool)
  95. --with-pam Enable PAM support
  96. --with-pam-service=name Specify PAM service name
  97. --with-privsep-user=user Specify non-privileged user for privilege separation
  98. --with-sandbox=style Specify privilege separation sandbox (no, capsicum, darwin, rlimit, seccomp_filter, systrace, pledge)
  99. --with-selinux Enable SELinux support
  100. --with-kerberos5=PATH Enable Kerberos 5 support
  101. --with-privsep-path=xxx Path for privilege separation chroot (default=/var/empty)
  102. --with-xauth=PATH Specify path to xauth program
  103. --with-maildir=/path/to/mail Specify your system mail directory
  104. --with-mantype=man|cat|doc Set man page type
  105. --with-md5-passwords Enable use of MD5 passwords
  106. --without-shadow Disable shadow password support
  107. --with-ipaddr-display Use ip address instead of hostname in $DISPLAY
  108. --with-default-path= Specify default $PATH environment for server
  109. --with-superuser-path= Specify different path for super-user
  110. --with-4in6 Check for and convert IPv4 in IPv6 mapped addresses
  111. --with-bsd-auth Enable BSD auth support
  112. --with-pid-dir=PATH Specify location of sshd.pid file
  113. --with-lastlog=FILE|DIR specify lastlog location common locations
  114. Some influential environment variables:
  115. CC C compiler command
  116. CFLAGS C compiler flags
  117. LDFLAGS linker flags, e.g. -L<lib dir> if you have libraries in a
  118. nonstandard directory <lib dir>
  119. LIBS libraries to pass to the linker, e.g. -l<library>
  120. CPPFLAGS (Objective) C/C++ preprocessor flags, e.g. -I<include dir> if
  121. you have headers in a nonstandard directory <include dir>
  122. CPP C preprocessor
  123. Use these variables to override the choices made by `configure' or to help
  124. it to find libraries and programs with nonstandard names/locations.
  125. Report bugs to <openssh-unix-dev@mindrot.org>.

2.configure 参数

  1. # ./configure --prefix=/usr/ --sysconfdir=/etc/ssh --with-ssl-dir=/usr/local/lib64 --with-zlib --with-md5-passwords --with-pam --with-ssl-engine --with-selinux --with-ipaddr-display

3.安装

  1. # make && make install

4.3 配置验证(最后)

4.3.1 查看相应的配置文件

  1. # ls /etc/ssh/
  2. moduli ssh_config sshd_config ssh_host_dsa_key ssh_host_dsa_key.pub ssh_host_ecdsa_key ssh_host_ecdsa_key.pub ssh_host_ed25519_key ssh_host_ed25519_key.pub ssh_host_rsa_key ssh_host_rsa_key.pub

4.3.2 修改 sshd 配置文件

  1. # vim /etc/ssh/sshd_config

4.3.3 配置启动文件

从原先的解压的包中拷贝一些文件到目标位置(如果目标目录存在就覆盖)

  1. # cp -a contrib/redhat/sshd.init /etc/init.d/sshd
  2. # cp -a contrib/redhat/sshd.pam /etc/pam.d/sshd.pam
  3. # chmod +x /etc/init.d/sshd
  4. # chkconfig --add sshd
  5. # systemctl enable sshd

把原先的 systemd 管理的 sshd 文件删除或者移走或者删除,不移走的话影响我们重启 sshd 服务

  1. # mv /usr/lib/systemd/system/sshd.service your_backup_dir

4.3.4 配置开机启动

  1. # chkconfig sshd on
  2. Note: Forwarding request to 'systemctl enable sshd.socket'.
  3. Created symlink from /etc/systemd/system/sockets.target.wants/sshd.socket to /usr/lib/systemd/system/sshd.socket.

4.3.5 接下来测试启停服务

  1. # /etc/init.d/sshd restart
  2. Restarting sshd (via systemctl): [ 确定 ]
  3. # netstat -lntp | grep 22
  4. tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 26069/sshd
  5. tcp6 0 0 :::22 :::* LISTEN 26069/sshd
  6. # /etc/init.d/sshd stop
  7. Stopping sshd (via systemctl): [ 确定 ]
  8. # netstat -lntp | grep 22
  9. # /etc/init.d/sshd start
  10. Starting sshd (via systemctl): [ 确定 ]
  11. # systemctl status sshd
  12. sshd.service - SYSV: OpenSSH server daemon
  13. Loaded: loaded (/etc/rc.d/init.d/sshd; bad; vendor preset: enabled)
  14. Active: active (running) since 2019-09-19 20:39:57 CST; 11s ago
  15. Docs: man:systemd-sysv-generator(8)
  16. Process: 26229 ExecStop=/etc/rc.d/init.d/sshd stop (code=exited, status=0/SUCCESS)
  17. Process: 26310 ExecStart=/etc/rc.d/init.d/sshd start (code=exited, status=0/SUCCESS)
  18. Main PID: 26320 (sshd)
  19. Tasks: 1
  20. Memory: 608.0K
  21. CGroup: /system.slice/sshd.service
  22. └─26320 /usr/sbin/sshd
  23. 9 19 20:39:57 sz-lab-centos7-gitlab-nginx-proxy-192.168.178.46 systemd[1]: Starting SYSV: OpenSSH server daemon...
  24. 9 19 20:39:57 sz-lab-centos7-gitlab-nginx-proxy-192.168.178.46 sshd[26320]: Server listening on 0.0.0.0 port 22.
  25. 9 19 20:39:57 sz-lab-centos7-gitlab-nginx-proxy-192.168.178.46 sshd[26320]: Server listening on :: port 22.
  26. 9 19 20:39:57 sz-lab-centos7-gitlab-nginx-proxy-192.168.178.46 sshd[26310]: Starting sshd:[ 确定 ]
  27. 9 19 20:39:57 sz-lab-centos7-gitlab-nginx-proxy-192.168.178.46 systemd[1]: Started SYSV: OpenSSH server daemon.

4.4 验证版本

  1. # ssh -V
  2. OpenSSH_8.0p1, OpenSSL 1.1.1d 10 Sep 2019

4.5 SSH 连接测试

  1. # ssh you_username@your_server_ip

4.6 重启测试

  1. # sync
  2. # reboot now
声明: 本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
Linux

教你如何在Centos7下安装bind9域名解析服务

2024-1-15 14:57:55

Linux

Linux下路由配置教程

2024-1-25 17:45:44

0 条回复 A文章作者 M管理员
欢迎您,新朋友,感谢参与互动!
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
私信列表
搜索