CentOS7以上更新OpenSSH、OpenSSL

目前最新的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 

安装OpenSSH**********注意,一定要有备用的连接方式,安装过程很可能ssh掉线,防止无法远程连接到服务器。telnet是个很好的选择。我的是虚拟机,有VNC,所以没有配置telnet***********

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值