Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the ssh host key for a new Azure Linux VM created using PowerShell?

Tags:

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.

like image 561
Oliver Bock Avatar asked Aug 31 '15 04:08

Oliver Bock


People also ask

How do I generate an SSH host key?

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.


2 Answers

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.

like image 97
mwik Avatar answered Oct 04 '22 01:10

mwik


You can use a new "Run Command" feature of Azure Portal.

  • In your Virtual Machine page, go to "Run command" in "Operations" section of VM menu.
  • Select "RunShellScript" command.
  • 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.

like image 26
Martin Prikryl Avatar answered Oct 04 '22 02:10

Martin Prikryl