How to provide public SSH key during VM deployment for username existing on image?

Jason Haury 1 Reputation point
2021-10-28T16:38:30.427+00:00

I am using Python to create a VM using a resource like the below in the template. Let's say adminUsername is "bob". I can use the same code to create a 1-off VM using a vanilla Ubuntu image where Bob is not a user. However, as seen in the template below, we use a custom image which already has an account for Bob. Even though we place a public key in the template keyData property (in the form "ssh-rsa aslflkasdfkladflkj..."). Again, this tactic works when using a vanilla Ubuntu image, but on our custom image, /home/bob/.ssh/authorized_keys is not being updated - the only keys in that file are the ones from the original base image. I expect the deployment to add a public key to that file so that I can ssh in using the new public key.

Furthermore, if I use the same deployment template by change adminUsername to "alice", the deployment fails to make the new Alice user account.

I am verifying all this by using the "Reset Password" feature on the Azure VM dashboard where I'll add yet another username "charlie". I can then ssh in just fine using the corresponding private key and then look at the authorized_keys file for bob (which exists, and only has entries from the original image, but no new entries), but not alice because her account didn't even get made by the deployment template.

My goal is to make a deployment using a template like the below and using the image in the imageReference paramater, and be able to SSH in as Bob using the the private key associated with the public key provided in keyData parameter. How can I do this?

{
          "apiVersion": "2021-03-01",
          "dependsOn": [
            "[concat(variables('vmNameWorker'), 0)]",
            "[variables('nicNameMaster')]"
          ],
          "location": "[resourceGroup().location]",
          "name": "[variables('vmNameMaster')]",
          "properties": {
            "hardwareProfile": {
              "vmSize": "[parameters('vmMasterSize')]"
            },
            "networkProfile": {
              "networkInterfaces": [
                {
                  "id": "[resourceId('Microsoft.Network/networkInterfaces', variables('nicNameMaster'))]"
                }
              ]
            },
            "osProfile": {
              "adminUsername": "[parameters('adminUsername')]",
              "computerName": "[variables('vmNameMaster')]",
              "linuxConfiguration": {
                "ssh": {
                  "publicKeys": [
                    {
                      "keyData": "[parameters('sshKeyData')]",
                      "path": "[concat('/home/', parameters('adminUsername'), '/.ssh/authorized_keys')]"
                    }
                  ]
                }
              }
            },
            "storageProfile": {
              "imageReference": {
                "id": "[parameters('imageName')]"
              }
            }
          },
          "resources": [
              {
                "apiVersion": "2016-03-30",
                "type": "extensions",
                "name": "Installation",
                "location": "[resourceGroup().location]",
                "dependsOn": [
                  "[variables('vmNameMaster')]"
                ],
                "properties": {
                  "publisher": "Microsoft.Azure.Extensions",
                  "type": "CustomScript",
                  "typeHandlerVersion": "2.0",
                  "autoUpgradeMinorVersion": false,
                  "settings": {
                    "fileUris": [
                      "[concat(parameters('_artifactsLocation'), parameters('_artifactsLocationSasToken'))]"
                    ]
                  },
                  "protectedSettings": {
                    "commandToExecute": "[variables('installationCLI')]"
                  }
                }
              }
            ],
          "type": "Microsoft.Compute/virtualMachines"
        },
Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
7,873 questions
Azure Lab Services
Azure Lab Services
An Azure service that is used to set up labs for classrooms, trials, development and testing, and other scenarios.
297 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Praveen Prabhakaran 546 Reputation points Microsoft Employee
    2021-11-10T16:12:48.03+00:00

    Hello @Jason Haury

    if you have not deprovisioned the user from the existing image it will be just a clone copy of the VM so the private keys are already available in the OS.
    you can use "Attach" option instead of fromimage and create VM and the existing public keys will work for the user that already exist.

    "storageProfile": {
    "osDisk": {
    "osType": "[parameters('osType')]",
    "createOption": "Attach",
    "managedDisk": {
    "id": "[resourceId('Microsoft.Compute/disks', variables('diskName'))]"
    }
    }
    },

    deprovisioning user --> https://video2.skills-academy.com/en-us/azure/virtual-machines/linux/capture-image#step-1-deprovision-the-vm

    Template--> https://github.com/Azure/azure-quickstart-templates/tree/master/quickstarts/microsoft.compute/vm-specialized-vhd-new-or-existing-vnet

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.