赞
踩
目前最新的OpenSSH是openssh9.1p1,至于为什么不升级到最新版本,是因为openssh8.9版本以上不支持key转发了。详情可以看一看这个文章:
https://editor.csdn.net/md/?articleId=128041801
CentOS7以上版本一般不需要更新OpenSSl就可以成功更新Openssh,大家可以根据自己的需求,也可以不更新OpenSSL直接安装OpenSSH。
安装Perl、OpenSSL
1. 安装编译需要的环境
yum -y install gcc gcc-c++ glibc make zlib zlib-devel pam-devel openssl-devel rpm-build
2. 下载安装Perl
wget https://www.cpan.org/src/5.0/perl-5.36.0.tar.gz
tar zxf perl-5.36.0.tar.gz
cd perl-5.36.0
./Configure -des -Dprefix=/usr/local/perl
make && echo $?
make install
mv /usr/bin/perl /usr/bin/perl.bak
ln -s /usr/local/perl/bin/perl /usr/bin/perl
perl -v
3. 下载、解压、编译安装OpenSSL
wget https://www.openssl.org/source/openssl-3.0.7.tar.gz
tar zxf openssl-3.0.7.tar.gz
cd openssl-3.0.7
./config --prefix=/usr/local/ssl shared zlib
make && make install
mv /usr/bin/openssl /usr/bin/openssl.bak
ln -s /usr/local/ssl/bin/openssl /usr/bin/openssl
echo "/usr/local/ssl/lib64">> //etc/ld.so.conf.d/ssl.conf
/sbin/ldconfig
openssl version
1. 安装编译需要的环境
yum -y install gcc gcc-c++ glibc make zlib zlib-devel pam-devel openssl-devel rpm-build
2. 备份旧版本的配置文件、卸载旧版本openssh,我这旧版本是之前rpm安装的,源码安装的可以自行寻找安装位置。
mv -f /etc/ssh /etc/ssh.bak
for i in `rpm -qa |grep openssh-`; do rpm -e --nodeps $i; done
3. 下载并安装
wget http://ftp.openbsd.org/pub/OpenBSD/OpenSSH/portable/openssh-8.8p1.tar.gz
tar zxvf openssh-8.8p1.tar.gz
如果编译安装了OpenSSL,就要更改ssh的编译参数,需要添加 --without-openssl-header-check --with-ssl-dir=/usr/local/ssl 这个/usr/loca/ssl是你安装SSL的位置,你安装到了那就写哪
cd openssh-8.8p1
#根据情况选一个运行
#不指定SSL位置
./configure --prefix=/usr/local/openssh --sysconfdir=/etc/ssh --with-pam --with-zlib --with-md5-passwords
#指定SSL位置
./configure --prefix=/usr/local/openssh --sysconfdir=/etc/ssh --with-pam --with-zlib --with-md5-passwords --with-tcp-wrappers --without-openssl-header-check --with-ssl-dir=/usr/local/ssl
编译后安装
make
echo $?
make install
4. 创建软连接,备份旧的启动文件
ln -sf /usr/local/openssh/bin/* /usr/bin
ln -sf /usr/local/openssh/sbin/sshd /usr/sbin
cd /usr/lib/systemd/system/
mv sshd.service{,.bak}
mv sshd.keygen.service{,.bak}
mv sshd.socket{,.bak}
5. 更改sshd配置文件
我这里是要求key登陆的,大家可以根据自己的需求修改下面的参数。
PermitRootLogin yes #是否允许root登陆
PasswordAuthentication no #是否允许密码登陆
PubkeyAuthentication yes #是否允许密钥登陆
X11Forwarding yes #是否允许X11转发,用不到的可以不开这个,默认是关闭状态
echo -e "PermitRootLogin yes\nPasswordAuthentication no\nPubkeyAuthentication yes\nX11Forwarding yes">>/etc/ssh/sshd_config
6. 新建启动文件
写入sshd-keygen.service
cat << __EOF__ >> /usr/lib/systemd/system/sshd-keygen.service
[Unit]
Description=OpenSSH Server Key Generation
Conditionsshd_service_FILENotEmpty=|!/etc/ssh/ssh_host_rsa_key
Conditionsshd_service_FILENotEmpty=|!/etc/ssh/ssh_host_ecdsa_key
Conditionsshd_service_FILENotEmpty=|!/etc/ssh/ssh_host_ed25519_key
PartOf=sshd.service sshd.socket
[Service]
ExecStart=/usr/sbin/sshd-keygen
Type=oneshot
RemainAfterExit=yes
__EOF__
写入sshd.socket
cat << __EOF1__ >> /usr/lib/systemd/system/sshd.socket
[Unit]
Description=OpenSSH Server Socket
Documentation=man:sshd(8) man:sshd_config(5)
Conflicts=sshd.service
[Socket]
ListenStream=22
Accept=yes
[Install]
WantedBy=sockets.target
__EOF1__
写入sshd.service
cat << __EOF2__ >> /usr/lib/systemd/system/sshd.service [Unit] Description=OpenSSH server daemon Documentation=man:sshd(8) man:sshd_config(5) After=network.target sshd-keygen.service Wants=sshd-keygen.service [Service] # 这里的type只能是simple,notify会获取不到satus Type=simple Environmentsshd_service_FILE=/etc/sysconfig/sshd #如果手动复制文办请把转义符"\"去掉 ExecStart=/usr/sbin/sshd -D \$OPTIONS #如果手动复制文办请把转义符"\"去掉 ExecReload=/bin/kill -HUP \$MAINPID KillMode=process Restart=on-failure RestartSec=42s [Install] WantedBy=multi-user.target __EOF2__
7. 重启服务
systemctl daemon-reload
systemctl enable --now sshd
systemctl restart sshd
systemctl status sshd
8. 验证
ssh –V
sshd -V
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。