Why Your Secrets Don’t Belong in Code or Environment Files
Every Linux server administrator has seen it: an API key hard-coded in a deployment script, a database password sitting in a .env file on disk, or a private key pasted into a Slack channel “temporarily.” These habits are among the most common causes of credential leaks. Once a secret lands in source control or a plain-text file, it tends to live forever — copied into backups, shared in screenshots, and exposed by every log rotation accident. Azure Key Vault exists to solve exactly this problem: it is a managed service that stores passwords, API keys, certificates, and other sensitive values in a hardened vault, and hands them out only to identities that are explicitly authorized.

For sysadmins running Linux virtual machines on Azure, using Azure Key Vault means your application servers never need a local copy of a database password or a TLS private key. The VM retrieves the secret at runtime over an encrypted channel, and you can rotate, audit, and revoke access centrally. In this guide you will create a vault, store and retrieve secrets with the Azure CLI, choose between access policies and Azure RBAC, pull secrets from a Linux VM using a managed identity, rotate a secret, and fix the classic “access denied” errors that trip up first-time users.
Prerequisites
- An Azure subscription with a resource group you can create resources in (for example
rg-secrets-demo). - The Azure CLI installed and logged in (
az login) — if you haven’t set it up yet, see our guide on installing Azure CLI on Linux. - Sufficient permissions to create a Key Vault and assign roles (Contributor on the resource group is enough for most steps).
- A Linux VM in Azure is helpful for the managed-identity section — our tutorial on creating and hardening an Azure Linux virtual machine covers that setup.
Step 1: Create an Azure Key Vault
Key Vault names must be globally unique across Azure and contain only letters, numbers, and hyphens. Create one with az keyvault create:
az group create --name rg-secrets-demo --location eastus
az keyvault create \
--name kv-linuxops-2026 \
--resource-group rg-secrets-demo \
--location eastus \
--enable-rbac-authorization true
The --enable-rbac-authorization true flag is the most important decision here. Azure Key Vault supports two permission models: the legacy access policy model (a vault-level ACL managed with az keyvault set-policy) and Azure role-based access control (RBAC). Microsoft now recommends RBAC for almost everything, because roles can be assigned at the right scope, audited through Azure Policy, and combined with Privileged Identity Management. If you omit the flag, new vaults in many regions default to RBAC anyway, but setting it explicitly makes your intent clear.
Confirm the vault exists and note its URI:
az keyvault show --name kv-linuxops-2026 \
--query "{name:name, vaultUri:vaultUri, sku:properties.sku.name, rbac:properties.enableRbacAuthorization}" \
-o table
Step 2: Grant Yourself Permission to Manage Secrets in Azure Key Vault
With the RBAC model, creating the vault does not automatically give you the right to write secrets — that data-plane permission lives in separate roles. Assign yourself the Key Vault Secrets Officer role, scoped to the vault:
VAULT_ID=$(az keyvault show --name kv-linuxops-2026 --query id -o tsv)
az role assignment create \
--assignee $(az ad signed-in-user show --query id -o tsv) \
--role "Key Vault Secrets Officer" \
--scope "$VAULT_ID"
Role assignments can take a minute or two to propagate. The companion role Key Vault Secrets User grants read-only secret access — that is the role you will typically give to a VM’s managed identity, following the principle of least privilege.
Step 3: Store Your First Secret in Azure Key Vault
Use az keyvault secret set to write a secret. Secret names must be 1–127 characters, start with a letter, and contain only letters, digits, and hyphens. Let’s store a database password:
az keyvault secret set \
--vault-name kv-linuxops-2026 \
--name db-password \
--value "CorrectHorseBatteryStaple-2026"
If a secret with that name already exists, the command creates a new version instead of failing — old versions remain retrievable by their versioned URI until you delete them, which is the foundation of safe rotation. Verify it landed:
az keyvault secret show \
--vault-name kv-linuxops-2026 \
--name db-password \
--query "{value:value, version:id}" -o tsv
For a cleaner value-only output, which is exactly what scripts need:
az keyvault secret show \
--vault-name kv-linuxops-2026 \
--name db-password \
--query value -o tsv
A common Azure Key Vault mistake is typing the secret value directly into the shell with --value, which leaves it in your history. Read the value from a file or a prompt instead:
read -s SECRET_VALUE
az keyvault secret set --vault-name kv-linuxops-2026 --name api-token --value "$SECRET_VALUE"
unset SECRET_VALUE
Step 4: Retrieve Secrets from a Linux VM with a Managed Identity
The real payoff comes on your servers. Instead of copying a password to every VM, you let the VM prove who it is to Azure. This is a managed identity: an Azure-managed service principal attached to the VM, so no credential is ever stored on the machine itself. This runtime-retrieval pattern is the whole point of Azure Key Vault: secrets live only in the vault, never baked into VM images, scripts, or config files.
Enable a system-assigned managed identity
az vm identity assign \
--name webvm01 \
--resource-group rg-secrets-demo
Grant the VM read-only secret access
az role assignment create \
--assignee $(az vm show --name webvm01 --resource-group rg-secrets-demo \
--query identity.principalId -o tsv) \
--role "Key Vault Secrets User" \
--scope "$VAULT_ID"
Fetch the secret from inside the VM
SSH into the VM, then query the Azure Instance Metadata Service to get a token, and call the Key Vault data-plane API — or simply use az login --identity with the CLI installed on the VM:
# on the VM itself, authenticate as the managed identity
az login --identity
# fetch the latest version of the secret
az keyvault secret show \
--vault-name kv-linuxops-2026 \
--name db-password \
--query value -o tsv
Your application can do the same in code using any Azure SDK with DefaultAzureCredential, which detects the managed identity automatically. No password ever touches the VM’s disk — if the VM is compromised or decommissioned, you revoke the role assignment and the secret is unreachable.
Step 5: Rotate an Azure Key Vault Secret Safely
Because every az keyvault secret set on an existing name creates a new version, rotation is straightforward:
- Write the new value:
az keyvault secret set --vault-name kv-linuxops-2026 --name db-password --value "New-Random-Value-2027". - Update the consumer (database, API provider) to accept the new credential alongside the old one where possible.
- Restart or reload services so they fetch the new value at startup.
- Once nothing uses the old version, deactivate the old one by updating its attributes to disabled with
az keyvault secret set-attributes --enabled false --id <versioned-id>.
Services that read secrets on every startup — rather than caching them in config files — pick up rotations with a simple restart. For fully automatic rotation of Azure SQL or Storage keys, Azure Key Vault supports built-in rotation policies; check the official Azure Key Vault documentation for services that integrate with it.
Troubleshooting Azure Key Vault Errors
“Access denied to the vault” or HTTP 403 on secret operations
This is almost always the permission model biting you. Three things to check in order:
- Which permission model is the vault using? Run
az keyvault show --name kv-linuxops-2026 --query properties.enableRbacAuthorization. If it istrue, legacy access policies are ignored — you must use RBAC role assignments, notaz keyvault set-policy. - Are you holding a data-plane role? Subscription Owner alone does not grant secret access; you need Key Vault Secrets Officer (write) or Key Vault Secrets User (read) on the vault.
- Did the assignment propagate? RBAC changes can take several minutes. Re-run the command after a short wait.
“Vault not found” (404) on the CLI
Confirm the vault name is correct and that you are logged into the right subscription with az account show. Remember that Key Vault names are globally unique — a name that resolves for someone else’s subscription will not resolve for yours.
Managed identity login fails on the VM
Check that the identity is actually enabled (az vm show --name webvm01 -g rg-secrets-demo --query identity) and that the VM’s outbound network allows the metadata endpoint at 169.254.169.254. Some hardened network security group setups block it by accident.
Secret value contains unexpected characters
Secrets containing $, !, or backslashes can be mangled by shell expansion when you pass them with --value. Prefer --file with the value in a file, or single quotes with history expansion disabled.
Conclusion
Moving secrets out of code and environment files into Azure Key Vault is one of the highest-leverage security improvements a Linux sysadmin can make on Azure. You create the vault once with az keyvault create, control access with modern RBAC roles instead of opaque access policies, write secrets with az keyvault secret set, and let your VMs pull what they need at runtime through managed identities — no credentials baked into images or scripts. Versioned secrets make rotation a routine operation instead of an incident. Start by migrating your most sensitive credential — the production database password — and the rest will follow naturally.