Password authentication is allowed for an Azure Linux VM

Prepare SSH-key access before disabling password logins that are not needed.

Description

With disable_password_authentication = false, an Azure Linux VM can accept both SSH keys and passwords. Its addresses and network rules determine whether it is reachable from the internet.

Potential impact

Someone who can reach SSH can sign in with an exposed or reused administrator password.

Remediation

Register the administrator’s admin_ssh_key. On an existing VM, test key access in a separate session before disabling password logins. Changing the key or disable_password_authentication in Terraform replaces the VM, so check the plan before applying it. Limit management sources and remove unused keys.

Examples

Both excerpts use the same NIC and omit the OS disk and image. Supply var.admin_password securely for the password example. For the key example, place the approved public key file where Terraform runs and keep the private key with the administrator.

Before

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

After

hcl
resource "azurerm_linux_virtual_machine" "example" {
  name                  = "${var.prefix}-vm"
  location              = azurerm_resource_group.main.location
  resource_group_name   = azurerm_resource_group.main.name
  network_interface_ids = [azurerm_network_interface.main.id]
  size               = "Standard_DS1_v2"

  admin_username                  = "adminuser"
  disable_password_authentication = true

  admin_ssh_key {
    username   = "adminuser"
    public_key = file("~/.ssh/id_rsa.pub")
  }
}

References