文章最后更新时间:2025年03月05日
记录一次Linux CentOS8重新安装SSH
卸载现有版本
查看当前安装版本
rpm -qa | grep ssh
结果:
openssh-clients-8.0p1-4.el8_1.x86_64 libssh-0.8.5-2.el8.x86_64 openssh-8.0p1-4.el8_1.x86_64 openssh-server-8.0p1-4.el8_1.x86_64
我们分别卸载上面的:openssh-server-8.0p1-4.el8_1.x86_64、openssh-clients-8.0p1-4.el8_1.x86_64、openssh-8.0p1-4.el8_1.x86_64
停止SSH服务
Linux关闭SSH的方法有两种:
1.使用systemctl命令
#查看ssh服务的状态: systemctl status sshd #关闭ssh服务: sudo systemctl stop sshd #禁止ssh服务开机自启动: sudo systemctl disable sshd
2.使用service命令
#查看ssh服务的状态: service ssh status #关闭ssh服务: sudo service ssh stop #禁止ssh服务开机自启动: sudo update-rc.d ssh disable
按需选择其中关闭服务命令即可。
卸载
rpm -e openssh-server-8.0p1-4.el8_1.x86_64 #上面它返回了一个警告信息,不用管 #warning: /etc/ssh/sshd_config saved as /etc/ssh/sshd_config.rpmsave rpm -e openssh-clients-8.0p1-4.el8_1.x86_64 rpm -e openssh-8.0p1-4.el8_1.x86_64
如果报错,可以使用yum remove替换上面的rpm -e来进行卸载。
yum remove openssh-server-8.0p1-4.el8_1.x86_64 yum remove openssh-clients-8.0p1-4.el8_1.x86_64 yum remove openssh-8.0p1-4.el8_1.x86_64
查看状态
service sshd status
返回如下结果“找不到sshd.service单元”表示卸载成功
Redirecting to /bin/systemctl status sshd.service Unit sshd.service could not be found.
安装openssh服务
安装openssh
检查安装依赖包
sudo yum install -y openssl openssl-devel zlib-devel libffi-devel
运行安装命令
yum install -y openssh-server
下载过程:
Last metadata expiration check: 0:02:57 ago on Wed 03 Mar 2025 05:59:08 PM CST. Dependencies resolved. =========================================================================================================================================================================================================================================================== Package Arch Version Repository Size =========================================================================================================================================================================================================================================================== Installing: openssh-server x86_64 8.0p1-5.el8 BaseOS 484 k Installing dependencies: openssh x86_64 8.0p1-5.el8 BaseOS 520 k Transaction Summary =========================================================================================================================================================================================================================================================== Install 2 Packages Total download size: 1.0 M Installed size: 2.8 M Downloading Packages: (1/2): openssh-8.0p1-5.el8.x86_64.rpm 395 kB/s | 520 kB 00:01 (2/2): openssh-server-8.0p1-5.el8.x86_64.rpm 366 kB/s | 484 kB 00:01 ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Total 759 kB/s | 1.0 MB 00:01 Running transaction check Transaction check succeeded. Running transaction test Transaction test succeeded. Running transaction Preparing : 1/1 Running scriptlet: openssh-8.0p1-5.el8.x86_64 1/2 Installing : openssh-8.0p1-5.el8.x86_64 1/2 Running scriptlet: openssh-server-8.0p1-5.el8.x86_64 2/2 Installing : openssh-server-8.0p1-5.el8.x86_64 2/2 Running scriptlet: openssh-server-8.0p1-5.el8.x86_64 2/2 Verifying : openssh-8.0p1-5.el8.x86_64 1/2 Verifying : openssh-server-8.0p1-5.el8.x86_64 2/2 Installed: openssh-server-8.0p1-5.el8.x86_64 openssh-8.0p1-5.el8.x86_64 Complete!
查看状态
service sshd status
状态信息:
Redirecting to /bin/systemctl status sshd.service sshd.service - OpenSSH server daemon Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled) Active: inactive (dead) since Wed 2025-03-03 17:53:07 CST; 9min ago Docs: man:sshd(8) man:sshd_config(5) Main PID: 1061640 (code=exited, status=0/SUCCESS) .......
显示“SUCCESS”即为成功,提示:“Active: inactive (dead)” ,即服务是dead(死亡)状态,后面重启即可。
安装openssh客户端
其实安装了服务端后就可以链接了,但是如果我们使用的某些工具依赖于客户端就不得不安装了,比如scp命令是属于openssh-clients的。
安装命令
yum install openssh-clients
重启服务
systemctl restart sshd.service
查看状态
service sshd status
状态信息:
Redirecting to /bin/systemctl status sshd.service sshd.service - OpenSSH server daemon Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled) Active: active (running) since Wed 2025-03-03 18:03:06 CST; 52min ago Docs: man:sshd(8) man:sshd_config(5) Main PID: 1087117 (sshd) Tasks: 1 (limit: 11540) Memory: 3.6M CGroup: /system.slice/sshd.service └─1087117 /usr/sbin/sshd -D -oCiphers=aes256-gcm@openssh.com,chacha20-poly1305@openssh.com,aes256-ctr,aes256-cbc,aes128-gcm@openssh.com,aes128-ctr,aes128-cbc -oMACs=hmac-sha2-256-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-512-etm@o>
至此OpenSSH安装完成,假如出现重启报错可以运行以下命令排查问题:
进入ssh文件夹
cd /etc/ssh/
执行排错命令
sshd -T
如果提示:“无法加载主机密钥”,可以参考:https://o0310o.com/kb/556.html
免责声明:本文来自LANGKYE#,不代表0oD三一o0的观点和立场,如有侵权请联系本平台处理。
还没有评论,来说两句吧...