Review administrator SSH keys for Azure VMs

Configure approved administrator authentication and test key access before restricting password authentication.

Description

If admin_ssh_key is missing, check how administrators actually sign in to the Azure Linux VM. Another tool may already install their public keys.

Potential impact

An exposed administrator password can allow sign-in. Disabling passwords before testing a key can also lock administrators out.

Remediation

For key access, set the administrator username and an approved public key (Ed25519, or RSA with at least 2,048 bits) in admin_ssh_key. Test a new session with the key before disabling password authentication. Protect each user’s private key and remove public keys that are no longer authorized.

Examples

The example uses adminuser and a public key file. Supply the OS disk and image, and replace the path with your actual public key file. Changing admin_ssh_key on an existing VM through Terraform replaces the VM. Check the plan before applying it.

Before

hcl
resource "azurerm_linux_virtual_machine" "example" {
  name                            = "example-vm"
  resource_group_name             = azurerm_resource_group.example.name
  location                        = azurerm_resource_group.example.location
  size                            = "Standard_F2"
  admin_username                  = "adminuser"
  admin_password                  = var.admin_password
  disable_password_authentication = false
  network_interface_ids           = [azurerm_network_interface.example.id]
}

After

hcl
resource "azurerm_linux_virtual_machine" "example" {
  name                            = "example-vm"
  resource_group_name             = azurerm_resource_group.example.name
  location                        = azurerm_resource_group.example.location
  size                            = "Standard_F2"
  admin_username                  = "adminuser"
  disable_password_authentication = true
  network_interface_ids           = [azurerm_network_interface.example.id]

  admin_ssh_key {
    username   = "adminuser"
    public_key = file("/secure/path/id_ed25519.pub")
  }
}

References