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.
To generate an SSH key on your Linux server run the command ssh-keygen . The command can take flags if you would like to customize the type of key that is generated as well as the signing algorithms used to generate the key. This example generates a standard 2048-bit RSA key without a passphrase.
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"
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.
You can use a new "Run Command" feature of Azure Portal.
Paste the following command:
for f in /etc/ssh/ssh_host_*_key; do ssh-keygen -l -f "$f"; done
You will get an output like:
Enable succeeded:
[stdout]
256 SHA256:bKKCom8yh5gOuBNWaHHJ3rrnRXmCOAyPN/WximYEPAU /etc/ssh/ssh_host_ecdsa_key.pub (ECDSA)
256 SHA256:IYeDl+gseYk46Acg4g2mcXGvCr7Z8FqOd+pCJz/KLHg /etc/ssh/ssh_host_ed25519_key.pub (ED25519)
2048 SHA256:rA0lIXvHqFq7VHKQCqHwjsj28kw+tO0g/X4KnPpEjMk root@myazurevm (RSA)
[stderr]
(the set of key types will vary with your VM image)
The feature can also be used via Azure CLI, what is shown in the link above and also in the answer by @mwik.
Check also my complete guide to Connecting securely to Microsoft Azure service with SFTP.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With