CentOS 7 ssh的版本是7.4p1,版本比较老,经常被通知有安全风险,需要修复,只好升级了。

1. 安装并启动telnet server ,防止SSH不可以用,还可以用telnet 连接服务器

yum install xinetd telnet-server -y
# 设置telnet 登陆的终端类型
cat >> /etc/securetty <<EOF
pts/0
pts/1
pts/2
pts/3
EOF
# 启动telnet 服务
systemctl enable xinetd
systemctl enable telnet.socket
systemctl start telnet.socket
systemctl start xinetd

2. 下载openssh 及openssl安装包

mkdir -p /opt/openssh && cd /opt/openssh
wget https://cdn.openbsd.org/pub/OpenBSD/OpenSSH/portable/openssh-8.6p1.tar.gz
# 下载openssl
wget https://www.openssl.org/source/openssl-1.1.1k.tar.gz

3. 安装需要的依赖服务

yum install -y gcc zlib-devel openssl-devel pam-devel systemd-devel perl

4. 升级openssl

openssh 8.6 要求openssl 版本,先升级openssl

tar xzvf openssl-1.1.1k.tar.gz && cd openssl-1.1.1k && cd openssl-1.1.1k
# 备份原来的openssl
mv /usr/bin/openssl /usr/bin/openssl_bak
mv /usr/include/openssl /usr/include/openssl_bak
# 编译安装openssl
./config --prefix=/usr/local/openssl --shared
make && make install
ln -s /usr/local/openssl/bin/openssl /usr/bin/openssl
ln -s /usr/local/openssl/include/openssl /usr/include/openssl
# 添加动态库
echo "/usr/local/openssl/lib" >> /etc/ld.so.conf
# 重新加载动态库
ldconfig

5. 备份 ssh配置

mkdir /etc/sshold
mv /etc/ssh/* /etc/sshold

6. 安装openssh

tar xzvf openssh-8.6p1.tar.gz  && cd openssh-8.6p1

openssh支持systemd启动,需要修改sshd.c这个主函数文件,找到调用server_accept_loop这个函数的行,注意这个函数的定义也在这个文件,不要找错了,不然安装后sshd会一直重启(大概在2058行)

# 添加的内容 
/* Signal systemd that we are ready to accept connections */
sd_notify(0, "READY=1");
# 下面为已有内容
/* Accept a connection and return in a forked child */
server_accept_loop(&sock_in, &sock_out,&newsock, config_s);

文件开关添加引用 (44行)

#include <systemd/sd-daemon.h>

编译 openssh

./configure --prefix=/usr --sysconfdir=/etc/ssh --with-pam  --with-md5-passwords --mandir=/usr/share/man --with-ssl-dir=/usr/local/openssl

修改Makefile以支持systemd ,编译时还需要在makefile中指明,编辑文件:Makefile ,找到变量 LIBS,修改如下(50行)

LIBS=-lcrypto -ldl -lutil -lz -lcrypt -lresolv -lsystemd

安装 openssh

make 
make install

7. 修改新生成的sshd_config配置文件

cat >> /etc/ssh/sshd_config<<EOF
HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_ecdsa_key
HostKey /etc/ssh/ssh_host_ed25519_key
PubkeyAuthentication yes
LoginGraceTime 120
PermitRootLogin yes
StrictModes yes

UsePAM yes
EOF

8. 重启sshd服务

systemctl restart sshd

9. 验证ssh服务正常后,记得停止telnet server 服务,并删除telnet

参考

其中openssh 不停重启,参考 :

​http://www.heycode.com/a15802.html​