简体   繁体   中英

How to use existing SSH key stored for Virtual Machine creation in azure using ARM template

I have a created SSH key on Azure cloud. Now I want to create a Virtual Machine using the exiting key to use that public key source. Now I want to do this operation using ARM template. I was trying something like this

"osProfile" : { "computerName": "myComputerName", "adminUsername": "adminUser", "linuxConfiguration": { "disablePasswordAuthentication": true, "ssh": { "publicKeys": [ { "path": "[concat('/home/', 'adminUser', '/.ssh/authorized_keys')]", "keyData": "how I get public key for the existing ssh key" } ] } } }

But I'm not sure how to fetch the public key of the created SSH Key.

If you have generated keys with ssh-keygen on Azure cloud shell. SSH key pairs are by default kept in the /home/azureuser/.ssh directory.

To see your public key by running cat as follows:

cat ~/.ssh/id_rsa.pub

在此处输入图像描述

Then copy and paste the contents of the public key file into your Resource Manager template, make sure you don't copy any additional whitespace or introduce additional line breaks.

Read more details steps: Create and manage SSH keys for authentication to a Linux VM in Azure and How to create a Linux virtual machine with Azure Resource Manager templates

First build the resource id for your existing ssh key storage on Azure:

"variables": {
    "sshKeyResourceId": "[resourceId(variables('sshKeyRg'), 'Microsoft.Compute/sshPublicKeys', variables('sshKeyName'))]",
}

Next extract the ssh "keyData" from this resource:

"osProfile": {
    "linuxConfiguration": {
        "disablePasswordAuthentication": true,
            "ssh": {
                "publicKeys": [
                    {
                        "keyData": "[reference(variables('sshKeyResourceId'), '2019-12-01').publicKey]",
                        "path": "[concat('/home/', variables('adminUser'), '/.ssh/authorized_keys')]"
                    }
                ]
             }
         }
     }
}

See more detailed example here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM