首页 > 解决方案 > 如何解决: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')

它打开一个窗口并显示三个按钮,我可以在其中单击“是”并且它可以工作。

但是当我尝试使用其他虚拟机时(完全相同的配置,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)

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

标签: pythongoogle-cloud-platformsshgoogle-cloud-python

解决方案


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

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

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


推荐阅读