linux在线yum安装openssh
前言
远程SSH服务器配置为使用arcfour流密码或无任何密码。RFC4253不建议使用arcfour弱算法。https://tools.ietf.org/html/rfc4253#section-6.3
由于安装openssh最新版本,则需要先更新openssl版本,否则低版本openssl会导致openssh更新失败
一、依赖包和前期工作准备
1.1查看版本
[root@localhost ~]# ssh -V
OpenSSH_9.1p1, OpenSSL 1.1.1s 1 Nov 2022
1.2备份ssh
使用-r选项,cp指令对递归复制及其内容,包括子目录和文件,通过ls查看没有备份成功,也可以通过目录形式查看
[root@localhost ~]# cp -r /etc/ssh /etc/ssh_bak
[root@localhost ~]# ls /etc
1.3安装依赖包
通过yum install -y pam* zlib*安装
[root@localhost ~]# yum install -y pam* zlib*
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
通过yum -y install gcc automake autoconf libtool make 安装gcc
[root@localhost ~]# yum -y install gcc automake autoconf libtool make
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
1.4总结
如果不安装依赖包,安装openssh进行 ./configgure会进行编译错误,刚开始报错gcc,g++,即使安装这两个也会报错别的信息。
二、下载openssl及安装
这里下载就默认放在根目录,可以进入opt目录再执行下载,这样安装包就在opt目录下
2.1下载
通过wget https://www.openssl.org/source/openssl-3.3.1.tar.gz --no-check-certificate可以指定版本下载
[root@localhost ~]# wget https://www.openssl.org/source/openssl-3.3.1.tar.gz --no-check-certificate
--2024-07-02 11:16:23-- https://www.openssl.org/source/openssl-3.3.1.tar.gz
正在解析主机 www.openssl.org (www.openssl.org)... 34.36.58.177, 2600:1901:0:1812::
正在连接 www.openssl.org (www.openssl.org)|34.36.58.177|:443... 已连接。
2.2解压以及进入目录
[root@localhost ~]# tar zxf openssl-3.3.1.tar.gz
[root@localhost ~]# cd openssl-3.3.1
2.3安装版本及编译
通过预编译指导路径./config --prefix=/usr/ --openssldir=/usr/ shared,执行报错缺少IPC/Cmd.pm模块
[root@localhost openssl-3.3.1]# ./config --prefix=/usr/ --openssldir=/usr/ shared
Can't locate IPC/Cmd.pm in @INC (@INC contains: /root/openssl-3.3.1/util/perl /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5
安装IPC模块解决该问题,再执行./config --prefix=/usr/ --openssldir=/usr/ shared发现成功
yum -y install perl-IPC-Cmd
[root@localhost openssl-3.3.1]# ./config --prefix=/usr/ --openssldir=/usr/ shared
Configuring OpenSSL version 3.3.1 for target linux-x86_64
Using os-specific seed configuration
Created configdata.pm
Running configdata.pm
Created Makefile.in
Created Makefile
Created include/openssl/configuration.h
**********************************************************************
*** ***
*** OpenSSL has been successfully configured ***
*** ***
*** If you encounter a problem while building, please open an ***
*** issue on GitHub <https://github.com/openssl/openssl/issues> ***
*** and include the output from the following command: ***
*** ***
*** perl configdata.pm --dump ***
*** ***
*** (If you are new to OpenSSL, you might want to consult the ***
*** 'Troubleshooting' section in the INSTALL.md file first) ***
*** ***
**********************************************************************
通过执行make && make install,过程中等待比较久,大概5分钟,执行完之后,最后一行没有报错则成功
[root@localhost openssl-3.3.1]#make && make install
install doc/html/man7/provider-signature.html -> /usr//share/doc/openssl/html/man7/provider-signature.html
install doc/html/man7/provider-storemgmt.html -> /usr//share/doc/openssl/html/man7/provider-storemgmt.html
install doc/html/man7/provider.html -> /usr//share/doc/openssl/html/man7/provider.html
install doc/html/man7/proxy-certificates.html -> /usr//share/doc/openssl/html/man7/proxy-certificates.html
install doc/html/man7/x509.html -> /usr//share/doc/openssl/html/man7/x509.html
[root@localhost openssl-3.3.1]#
2.4查看版本
[root@localhost openssl-3.3.1]# openssl version
OpenSSL 3.3.1 4 Jun 2024 (Library: OpenSSL 3.3.1 4 Jun 2024)
三、下载openssh及安装
3.1下载
通过wget https://cdn.openbsd.org/pub/OpenBSD/OpenSSH/portable/openssh-9.8p1.tar.gz --no-check-certificate可以指定版本下载
[root@localhost ~]# wget https://cdn.openbsd.org/pub/OpenBSD/OpenSSH/portable/openssh-9.8p1.tar.gz --no-check-certificate
--2024-07-02 15:55:41-- https://cdn.openbsd.org/pub/OpenBSD/OpenSSH/portable/openssh-9.8p1.tar.gz
正在解析主机 cdn.openbsd.org (cdn.openbsd.org)... 146.75.115.52, 2a04:4e42:1a::820
正在连接 cdn.openbsd.org (cdn.openbsd.org)|146.75.115.52|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:1910393 (1.8M) [application/octet-stream]
正在保存至: “openssh-9.8p1.tar.gz”
3.2解压以及进入目录
[root@localhost ~]# tar zxf openssh-9.8p1.tar.gz
[root@localhost ~]# cd openssh-9.8p1
3.3安装版本及编译
通过预编译指导路径./configure --with-zlib --with-ssl-dir --with-pam --bindir=/usr/bin --sbindir=/usr/sbin --sysconfdir=/etc/ssh,执行完成之后最后一行出现 subdirectory内容,则成功
[root@localhost openssh-9.8p1]# ./configure --with-zlib --with-ssl-dir --with-pam --bindir=/usr/bin --sbindir=/usr/sbin --sysconfdir=/etc/ssh
PAM is enabled. You may need to install a PAM control file
for sshd, otherwise password authentication may fail.
Example PAM control files can be found in the contrib/
subdirectory
[root@localhost openssh-9.8p1]#
通过执行make && make install,执行完之后,最后一行没有报错则成功
[root@localhost openssh-9.8p1]]# make && make install
/usr/bin/mkdir -p /etc/ssh
/etc/ssh/ssh_config already exists, install will not overwrite
/etc/ssh/sshd_config already exists, install will not overwrite
/etc/ssh/moduli already exists, install will not overwrite
/usr/sbin/sshd -t -f /etc/ssh/sshd_config
[root@localhost openssh-9.8p1]#
3.4查看版本
[root@localhost openssh-9.8p1]# ssh -V
OpenSSH_9.8p1, OpenSSL 3.3.1 4 Jun 2024
如果通过本窗口查询,还是显示之前的旧版本,可以通过点击红框内开启另外一个窗口查询版本
注意:升级完之后,不要关闭连接,需要测试是否能远程
3.5重启ssh服务
[root@localhost ~]# systemctl restart sshd
注意:升级完之后,不要关闭连接,需要测试是否能远程
四、openssh安装完之后需要配置的信息
新建窗口远程发现无法远程
解决方法及确认相关信息
4.1未关闭 SELinux 导致 ssh 无法登录
通过sestatus查看selinux状态,若系统返回的参数信息SELinux status显示为disabled,则表示SELinux已关闭,不做修改。
[root@localhost ~]# sestatus
SELinux status: disabled
返回的是SELINUX=enforcing,则通过vi /etc/selinux/config,将修改成SELINUX=enforcing,退出保存
4.2未开放允许roor登录导致无法远程
通过grep -E “PermitRootLogin|PubkeyAuthentication|^PasswordAuthentication” /etc/ssh/sshd_config查看这三个是否为yes,只返回一个,或者没有,需要修改配置
[root@localhost ~]# grep -E "^PermitRootLogin|^PubkeyAuthentication|^PasswordAuthentication" /etc/ssh/sshd_config
PermitRootLogin yes
PubkeyAuthentication yes
PasswordAuthentication yes
没有返会这三个参数,需要通过vim /etc/ssh/sshd_config进入里面填加这三个参数,退出保存
4.3以上两个确认之后,重启ssh服务
[root@localhost ~]# systemctl restart sshd
4.4 新建窗体发现可以远程
五、其它常见问题
5.1查看防火墙状态,是否开启
关闭的情况下,则不管,开启则要确认下是否开通远程端口这些,具体参考下列链接
[root@localhost ~]# systemctl status firewalld
5.2查看之前备份ssh,ssh_config端口
由于升级云服务器,端口默认22,有些服务器远程端口不同,则需要查看之前配置,或者提前看,以免出现断掉远程之后不能远程,只能通过主机的进入
5.2.1没备份之前查看
[root@localhost ~]# vim /etc/ssh/sshd_config
发现port远程的端口号2222,如果前面#port 22 则不管
5.2.2备份之后查看
[root@localhost ~]# vim /etc/ssh_bak/sshd_config
发现port远程的端口号2222,如果前面#port 22 则不管
5.2.3查看未升级之前的配置,端口2222或者其他。需要我们手动修改升级之后sshd_config配置端口跟未升级之前的一致
[root@localhost ~]# vim /etc/ssh/sshd_config
5.3yum安装时报错,提示/var/run/yum.pid 已被锁定,删除pid进程,再执行yum
[root@localhost ~]# rm -f /var/run/yum.pid
5.4启动ssh或者重启卡主
[root@localhost ~]# systemctl restart sshd
Job for sshd.service failed because a timeout was exceeded.
See “systemctl status sshd.service” and “journalctl -xe” for details.
修改sshd的启动方式即可解决
[root@localhost ~]# vim /usr/lib/systemd/system/sshd.service
将原有的Type=notify改为Type=simple
保存退出
执行systemctl daemon-reload
再次执行systemctl restart sshd即可成功
配置文件有就需要改,没有这个Type=参数则不需要处理
六、知识链接
防火墙知识: https://blog.csdn.net/suixinfeixiangfei/article/details/126119895?
SELinux知识: https://help.aliyun.com/zh/ecs/use-cases/enable-or-disable-selinux#85e51831e5vaq
SSH重启/启动长时间没翻译,没报错:https://blog.csdn.net/qq_49081692/article/details/136566683
七、常用命令
查看ssh版本号:
ssh -V
查看linux服务器上支持的ssh对称秘钥:
ssh -Q cipher
启动ssh服务:
systemctl start sshd.service
重启ssh服务:
systemctl restart sshd.service
查看ssh服务启动状态:
systemctl status sshd.service
设置服务开启自启
systemctl enable sshd.service