How to solve : Gcloud compute ssh SFATAL ERROR: No supported authentication methods available (server sent: publickey)

How to solve : Gcloud compute ssh SFATAL ERROR: No supported authentication methods available (server sent: publickey)

我遇到了这个著名的错误,经过多次研究,我可以使用 :

为一个 VM 修复它
print('ssh front-end begin')
host = "frontend-lab1"
    
cmd= f'gcloud compute ssh {host} --force-key-file-overwrite'
res = subprocess.check_output(cmd, shell=True)
    
cmd = f'gcloud compute ssh {host} --zone=europe-west1-b --command="cd /home/jeromevialhes/mse-tsm-cloudsys-lab1/app/ && sudo echo \'NEXT_PUBLIC_API_URL=http://{ip_back}:4000\' | sudo tee /home/jeromevialhes/mse-tsm-cloudsys-lab1/app/.env && sudo npm install pm2 -g && sudo npm run build && sudo pm2 --name counter-app start npm -- start"'
print(cmd)
res = subprocess.check_output(cmd, shell=True)
print(res)
print('ssh front-end end')

它会打开一个 window 并显示三个按钮,我可以在其中单击“是”并且它有效。

但是当我尝试另一个 VM(完全相同的配置,Ubuntu 20.04 lts VM)

print('ssh back-end begin')
host = "backend-lab1"
    
cmd= f'gcloud compute ssh {host} --force-key-file-overwrite'
res = subprocess.check_output(cmd, shell=True)
    
cmd = f'gcloud compute ssh {host} --zone=europe-west1-b --command="cd /home/jeromevialhes/mse-tsm-cloudsys-lab1/app/ && sudo echo \'DATABASE_HOST=http://{ip_db}:3000\' | sudo tee /home/jeromevialhes/mse-tsm-cloudsys-lab1/app/.env && sudo echo \'DATABASE_USER=postgres\' | sudo tee -a /home/jeromevialhes/mse-tsm-cloudsys-lab1/app/.env && sudo echo \'DATABASE_USER=postgres\' | sudo tee -a /home/jeromevialhes/mse-tsm-cloudsys-lab1/app/.env && sudo echo \'DATABASE_PASS=postgres\' | sudo tee -a /home/jeromevialhes/mse-tsm-cloudsys-lab1/app/.env && sudo echo \'DATABASE_NAME=counter\' | sudo tee -a /home/jeromevialhes/mse-tsm-cloudsys-lab1/app/.env && sudo npm install pm2 -g &&  pm2 --name counter-api start npm -- start"'
print(cmd)
res = subprocess.check_output(cmd, shell=True)
print(res)
print('ssh back-end end')   

我不断收到以下错误:

WARNING - POTENTIAL SECURITY BREACH!
The server's host key does not match the one PuTTY has
cached in the registry. This means that either the
server administrator has changed the host key, or you
have actually connected to another computer pretending
to be the server.
The new ssh-ed25519 key fingerprint is:
ssh-ed25519 255 99:f3:06:93:57:2d:8e:10:2b:4d:c3:18:90:aa:bc:00
If you were expecting this change and trust the new key,
enter "y" to update PuTTY's cache and continue connecting.
If you want to carry on connecting but without updating
the cache, enter "n".
If you want to abandon the connection completely, press
Return to cancel. Pressing Return is the ONLY guaranteed
safe choice.
Update cached key? (y/n, Return cancels connection) SFATAL ERROR: No supported authentication methods available (server sent: publickey)

我该怎么做才能解决这个问题?如果我可以在代码中设置自动使用“是”选项,那就太好了。

在Google云中,您在创建、删除然后创建新的虚拟机实例时经常会获得相同的IP 地址。这意味着同一 IP 地址的主机密钥将不同。

文件 ~/.ssh/known_hosts 包含按 IP 地址列出的主机列表。编辑文件,找到匹配 IP 地址的行并删除该行。然后重试 SSH。

在后台,CLI gcloud 调用 SSH (Linux) 或 Putty (Windows)。对于Linux,可以执行命令ssh-keygen -r host.example.com(或指定IP地址)在执行SSH连接尝试前更新主机指纹.