如何获取使用PowerShell创建的新Azure Linux VM的ssh主机密钥?

问题描述:

如果使用PowerShell创建Azure Linux VM,如何获得其新的ssh主机密钥,以便可以将其安装在本地ssh/putty中?最好的解决方案也是PowerShell代码.

If I create an Azure Linux VM using PowerShell, how can I get its new ssh host key, so that I can install it in my local ssh/putty? Preferably the solution is also PowerShell code.

旧问题,但是对于新来者来说,如今可以通过在Azure CLI中使用运行命令来获得另一种选择. PowerShell可能也有一个等效物,但是我还没有对此进行研究.

Old question, but for newcomers there is nowadays an alternative available by using run-command in Azure CLI. There is probably an equivalent for PowerShell too, but I have not investigated that.

az vm run-command invoke --name <your-vm-name> --command-id RunShellScript --scripts "cat /etc/ssh/ssh_host_ecdsa_key.pub"

将输出一个json文档,您可以从中提取公钥.请注意,尽管此过程非常慢(每个主机约30秒),但是您只需要运行一次即可.请参阅此要点,以获取有关如何使用Ansible更新known_hosts文件的示例.

will output a json document from which you can extract the public key. Beware though that this process is incredibly slow (~30 seconds per host), but you only need to run it once. See this gist for an example of how to update the known_hosts file with Ansible.