ssh改变导致无法连接ubuntu
·
问题
如题目所示
报错如下:
systemctl status sshd
● ssh.service - OpenBSD Secure Shell server
Loaded: loaded (/lib/systemd/system/ssh.service; enabled; vendor preset: enab
Active: active (running) since Thu 2025-06-19 14:12:42 CST; 2h 7min ago
Process: 48523 ExecStartPre=/usr/sbin/sshd -t (code=exited, status=0/SUCCESS)
Main PID: 48524 (sshd)
Tasks: 1 (limit: 4591)
CGroup: /system.slice/ssh.service
└─48524 /usr/sbin/sshd -D
6月 19 16:14:44 ubuntu sshd[53013]: error: Could not load host key: /etc/ssh/ssh
6月 19 16:14:44 ubuntu sshd[53013]: error: @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
6月 19 16:14:44 ubuntu sshd[53013]: error: @ WARNING: UNPROTECTED PRIVAT
6月 19 16:14:44 ubuntu sshd[53013]: error: @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
6月 19 16:14:44 ubuntu sshd[53013]: error: Permissions 0777 for '/etc/ssh/ssh_ho
6月 19 16:14:44 ubuntu sshd[53013]: error: It is required that your private key
6月 19 16:14:44 ubuntu sshd[53013]: error: This private key will be ignored.
6月 19 16:14:44 ubuntu sshd[53013]: error: key_load_private: bad permissions
6月 19 16:14:44 ubuntu sshd[53013]: error: Could not load host key: /etc/ssh/ssh
6月 19 16:14:44 ubuntu sshd[53013]: fatal: No supported key exchange algorithms
原因
ssh密钥发生了改变
解决办法
分为ubuntu和主机两个方面,(sudo apt-get remove 然后install 的方法没有效果)
1.ubuntu
重新生成密钥
$ ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key
Generating public/private rsa key pair.
/etc/ssh/ssh_host_rsa_key already exists.
Overwrite (y/n)? y
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /etc/ssh/ssh_host_rsa_key.
Your public key has been saved in /etc/ssh/ssh_host_rsa_key.pub.
The key fingerprint is:
SHA256:q0Qz+dqDtBIFX79tbV0iRrIIfIyxUTsgx6wLjaGN0eg zghy@ubuntu
The key's randomart image is:
+---[RSA 2048]----+
| o .+==. |
|o o .o*++.. . |
|.= + +.+oo + |
|oE+ o o...o o . .|
| . o= S + o o.|
| o..+ .. o o .|
| o.oo . . |
| ..o+. |
| .o .. |
+----[SHA256]-----+
接着执行
$ ssh-keygen -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key
Generating public/private ecdsa key pair.
/etc/ssh/ssh_host_ecdsa_key already exists.
Overwrite (y/n)?
$ ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key
Generating public/private ed25519 key pair.
/etc/ssh/ssh_host_ed25519_key already exists.
Overwrite (y/n)?
$ ll /etc/ssh/
总用量 604
drwxr-xr-x 2 root root 4096 6月 19 16:18 ./
drwxr-xr-x 136 root root 12288 6月 18 10:42 ../
-rwxrwxrwx 1 root root 553122 8月 12 2021 moduli*
-rwxrwxrwx 1 root root 1580 8月 12 2021 ssh_config*
-rwxrwxrwx 1 root root 3246 6月 19 14:12 sshd_config*
-rw------- 1 root root 668 6月 19 16:18 ssh_host_dsa_key
-rw-r--r-- 1 root root 601 6月 19 16:18 ssh_host_dsa_key.pub
-rwxrwxrwx 1 root root 227 6月 16 13:53 ssh_host_ecdsa_key*
-rwxrwxrwx 1 root root 173 6月 16 13:53 ssh_host_ecdsa_key.pub*
-rwxrwxrwx 1 root root 399 6月 16 13:53 ssh_host_ed25519_key*
-rwxrwxrwx 1 root root 93 6月 16 13:53 ssh_host_ed25519_key.pub*
-rwxrwxrwx 1 root root 1679 6月 19 16:21 ssh_host_rsa_key*
-rwxrwxrwx 1 root root 393 6月 19 16:21 ssh_host_rsa_key.pub*
-rwxrwxrwx 1 root root 338 6月 16 13:53 ssh_import_id*
修改权限
$ sudo chmod 600 /etc/ssh/ssh_host_*_key
$ sudo chmod 644 /etc/ssh/ssh_host_*_key.pub
$ sudo chown root:root /etc/ssh/ssh_host_*_key*
重新生成密钥
sudo rm /etc/ssh/ssh_host_*
dpkg-reconfigure openssh-server
# 或者
ssh-keygen -A
查看
$ grep "HostKey" /etc/ssh/sshd_config
HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_ecdsa_key
HostKey /etc/ssh/ssh_host_ed25519_key
zghy@ubuntu:~$ systemctl restart ssh
zghy@ubuntu:~$ systemctl status ssh
● ssh.service - OpenBSD Secure Shell server
Loaded: loaded (/lib/systemd/system/ssh.service; enabled; vendor preset: enab
Active: active (running) since Thu 2025-06-19 16:25:51 CST; 7s ago
Process: 53434 ExecStartPre=/usr/sbin/sshd -t (code=exited, status=0/SUCCESS)
Main PID: 53435 (sshd)
Tasks: 1 (limit: 4591)
CGroup: /system.slice/ssh.service
└─53435 /usr/sbin/sshd -D
6月 19 16:25:51 ubuntu systemd[1]: Starting OpenBSD Secure Shell server...
6月 19 16:25:51 ubuntu sshd[53435]: Server listening on 0.0.0.0 port 22.
6月 19 16:25:51 ubuntu sshd[53435]: Server listening on :: port 22.
6月 19 16:25:51 ubuntu systemd[1]: Started OpenBSD Secure Shell server.
2. cmd
打开powershell
notepad C:\Users\Administrator\.ssh\known_hosts
# 此时会弹出来txt文件
不用该,关掉就行(就是这个文件有错误)
执行下面的指令自动删除
ssh-keygen -R 192.168.218.128
然后重新连接
ssh 用户名@ubuntu地址ip
输入密码就连接成功了
3.vscode
通过上面两步主机和ubuntu可以ssh了,但是vscode还不可以(数据没更新)
简单方法
在ubuntu 的~目录下删除vscode的服务
$ rm -rf .vscode-server/
然后在vscode的ssh(.ssh\config)中把主机名字与ip都删了,刷新一下,重新添加,再连接就好了
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)