The goal
Your team shares one admin SSH key per VM, nobody knows who has a copy, and removing a leaver means editing authorized_keys everywhere. With Entra ID login, users SSH in with their own Entra identity. Access is an Azure role assignment, Conditional Access applies, and removing access takes one command.
What you need:
- A supported Linux distribution. The list changes, so check the current Microsoft docs.
- A system-assigned managed identity on the VM.
- The
AADSSHLoginForLinuxVM extension. - A role assignment for each user or group.
- Azure CLI 2.22.1 or later with the
sshextension on the client.
Step 1: enable the managed identity and install the extension
The extension fails without a system-assigned identity. A missing identity shows up as exit code 22 on the extension.
RG=rg-app-prod
VM=vm-app-01
az vm identity assign -g $RG -n $VM
az vm extension set \
--publisher Microsoft.Azure.ActiveDirectory \
--name AADSSHLoginForLinux \
-g $RG --vm-name $VM
az vm extension show -g $RG --vm-name $VM -n AADSSHLoginForLinux \
--query provisioningState -o tsv
You want Succeeded. The VM agent must be running, and the VM needs at least 1 GB of memory.
For a scale set, use az vmss identity assign --name <vmss> -g $RG and az vmss extension set ... --vmss-name <vmss>. Then roll the model out to the instances.
Warning
The older extension
AADLoginForLinuxconflicts with the new one. If it's still installed, the new extension fails with exit code 23. Remove the old one first:az vm extension delete -g $RG --vm-name $VM -n AADLoginForLinux.
Step 2: check outbound access from the VM
The VM must reach these endpoints over TCP 443 (Azure public cloud):
| Endpoint | Used for |
|---|---|
packages.microsoft.com | Installing the extension packages |
169.254.169.254 (IMDS, HTTP) | Managed identity and VM metadata |
login.microsoftonline.com | Sign-in through PAM |
pas.windows.net | Checking Azure RBAC |
Sovereign clouds use different hostnames. Azure Government uses login.microsoftonline.us and pasff.usgovcloudapi.net. If outbound traffic goes through Azure Firewall or a proxy, allow these endpoints before you install.
Proxy users: IMDS must bypass the proxy. Add the proxy settings and no_proxy=169.254.169.254 to the agent's systemd unit, then restart it:
sudo systemctl edit walinuxagent
# [Service]
# Environment="http_proxy=http://proxy:3128"
# Environment="https_proxy=http://proxy:3128"
# Environment="no_proxy=169.254.169.254"
sudo systemctl restart walinuxagent
Step 3: grant the login roles
Owner and Contributor do not give SSH access. Users need one of these data-plane roles:
| Role | What the user gets |
|---|---|
| Virtual Machine Administrator Login | SSH access plus sudo |
| Virtual Machine User Login | SSH access, no sudo |
Assign the roles to groups, at resource group scope:
RG_ID=$(az group show -n $RG --query id -o tsv)
OPS_GROUP=$(az ad group show --group "sg-linux-admins" --query id -o tsv)
az role assignment create \
--role "Virtual Machine Administrator Login" \
--assignee-object-id $OPS_GROUP \
--assignee-principal-type Group \
--scope $RG_ID
To create these role assignments you need Microsoft.Authorization/roleAssignments/write. The Virtual Machine Data Access Administrator role has it and can be limited to these roles. Role changes can take a few minutes to apply.
Tip
Put the Administrator Login role behind PIM, as an eligible assignment. Engineers then activate
sudo-level access only for the change window, and you get an audit trail of who activated it.
Step 4: connect
On the client:
az extension add --name ssh # once
az login
az ssh vm -g $RG -n $VM # uses the public IP
az ssh vm --ip 10.20.1.4 # private IP, e.g. over VPN or ExpressRoute
az ssh vm gets a short-lived OpenSSH certificate from Entra ID and uses it to open the session. You sign in as your UPN, for example jane@contoso.com.
To use a plain ssh, scp or VS Code Remote-SSH client, generate a config file:
az ssh config --file ~/.ssh/config.azure -g $RG -n $VM
ssh -F ~/.ssh/config.azure $VM
The certificate in that config expires. When it does, run az ssh config again.
Automation can sign in as a service principal (az login --service-principal ...) and then run az ssh vm. Assign the role with --assignee-principal-type ServicePrincipal.
Through Azure Bastion
If the VM has no public IP and there's no private path to it, use Bastion's native client support. This needs the Standard SKU or higher, with native client support enabled:
az network bastion ssh --name bas-hub -g rg-hub \
--target-resource-id $(az vm show -g $RG -n $VM --query id -o tsv) \
--auth-type AAD
Conditional Access
Sign-ins go through the enterprise application Azure Linux VM Sign-In. Target that app with your policy, for example to require MFA for admin access. Device-compliance and hybrid-join conditions only work when the CLI runs on Windows or macOS. They fail from Linux clients and Cloud Shell.
Fixing first-login errors
| Symptom | Likely cause | Fix |
|---|---|---|
Azure role not assigned at the prompt | No login role at the VM's scope | Assign Administrator or User Login, wait a few minutes |
Connection closed by <ip> port 22 | Missing role, or the system-assigned identity was disabled | Check the role. Re-enable the identity with az vm identity assign, wait, retry |
Couldn't retrieve token from local cache | Stale CLI session | Run az login again |
KeyError: 'access_token' | Old Azure CLI | Upgrade the CLI |
| Extension stuck in Transitioning, exit code 22 or 23 | No managed identity (22) or old extension present (23) | Step 1 |
Permission denied (publickey) on RHEL/Oracle Linux 7 | Old OpenSSH rejects the certificate type | Add PubkeyAcceptedKeyTypes +ssh-rsa-cert-v01@openssh.com to sshd_config |
Permission denied (publickey) after an in-place RHEL upgrade | Extension packages are from the old release | Reinstall the extension |
| First login always refused | AllowGroups/DenyGroups in sshd_config | Remove them, or scope them in a Match block that excludes Entra users |
sudo refused | User has User Login only | Grant Administrator Login (via PIM) |
To check what Azure sees, list the role assignments that apply to the VM:
az role assignment list --all --include-inherited \
--scope $(az vm show -g $RG -n $VM --query id -o tsv) \
--query "[?contains(roleDefinitionName,'Virtual Machine')].{role:roleDefinitionName, who:principalName, scope:scope}" -o table
Prevent it next time
- Deploy the identity and the extension from your VM template or with Azure Policy. Microsoft ships built-in policies for this, so new VMs get Entra login without anyone remembering to add it.
- Keep one break-glass local admin, with its key stored in Key Vault. You still need a way in if Entra ID or the network path is down. Use VMAccess to rotate that key.
- Remove the shared keys from
authorized_keysonce Entra login works. Otherwise you've added a second way in rather than replaced the first. - Grant access to groups, not individuals, and review those groups on the same schedule as other privileged access.
Get the weekly HubDatAI email
One email a week with everything new: posts, learning sessions and videos. Free, no spam, unsubscribe in one click. What you get