Geavanceerd
Zoeken inInstallatiegidsen
Zo zoek je
  • Plak een foutmelding tussen aanhalingstekens, bijv. "permission denied", om de oplossingstabel te vinden.
  • Kies een gebied en een deel van de gids (installeren, eerste instellingen, dagelijks gebruik, beveiliging, oplossingen) om direct naar de juiste stap te gaan.
  • "twee woorden" vindt precies die zin.
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken
Probeer:

Gids 14Azure CLI · Cloud

Install and set up the Azure CLI

The Azure CLI (az) manages Microsoft Azure from the terminal: resource groups, virtual machines, storage, networks, AKS clusters, Key Vault and identities. It is the main tool for the AZ-104 exam and for scripts and pipelines on Azure. This guide installs az from the Microsoft repository on RHEL and Ubuntu, sets up a safe login, subscriptions and defaults, and shows the daily commands and the fixes for login and subscription errors.

  • 9 Lessen
  • 11 Commando's

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

$ als gewone gebruiker# als root (of met sudo)

14.1

Know it before you install

You log in once with your Microsoft Entra ID account. The CLI keeps a token and sends every command as an API call to Azure Resource Manager. Every resource lives in a subscription and a resource group.

ItemValue
Packageazure-cli (from packages.microsoft.com)
Programaz (Python inside, installed in /opt/az/)
Config and tokens~/.azure/ (config, token cache)
Extensions~/.azure/cliextensions/
EnvironmentAZURE_CONFIG_DIR, AZURE_CORE_OUTPUT, REQUESTS_CA_BUNDLE
NetworkHTTPS (443) to login.microsoftonline.com and management.azure.com

Commando's in deze les

14.2

Install on RHEL, Rocky, Alma and Fedora

Import the Microsoft key, add the repository package for your RHEL major version (9 or 10) and install:

rpm --import https://packages.microsoft.com/keys/microsoft.asc
dnf -y install https://packages.microsoft.com/config/rhel/9.0/packages-microsoft-prod.rpm
dnf -y install azure-cli
az version

On RHEL 10 use …/config/rhel/10/packages-microsoft-prod.rpm.

14.3

Install on Ubuntu and Debian

apt-get update
apt-get install -y apt-transport-https ca-certificates curl gnupg lsb-release
mkdir -p /etc/apt/keyrings
curl -fsSL https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor -o /etc/apt/keyrings/microsoft.gpg
chmod go+r /etc/apt/keyrings/microsoft.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/microsoft.gpg] https://packages.microsoft.com/repos/azure-cli/ $(lsb_release -cs) main" > /etc/apt/sources.list.d/azure-cli.list
apt-get update
apt-get install -y azure-cli
az version

Microsoft also has a one-line script (curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash). It does the same steps; the manual way shows you what changes.

14.4

First configuration: log in and choose the subscription

Log in. On a server without a browser use the device code:

az login
az login --use-device-code
To sign in, use a web browser to open the page https://microsoft.com/devicelogin and enter the code ABCD-EFGH to authenticate.

Check where you are, and choose the right subscription — this is the most important step:

az account list -o table
az account set --subscription "Prod-Shop"
az account show --query '{name:name, id:id, user:user.name}' -o table

Set defaults so you type less, and a readable output:

az config set core.output=table
az config set defaults.group=rg-shop-prod defaults.location=westeurope
az config set core.collect_telemetry=false
echo 'source /etc/bash_completion.d/azure-cli' >> ~/.bashrc
SettingWhy
az account setEvery command goes to this subscription. A wrong one = changes in the wrong place.
core.output=tableEasy to read; use -o json or -o tsv in scripts.
defaults.group, defaults.locationNo -g and -l on every command.
core.collect_telemetry=falseNo usage data sent to Microsoft.

Notitie

Scripts and pipelines do not use your personal login. Use a managed identity on Azure VMs (az login --identity), or workload identity federation (OIDC) in GitHub Actions and Azure DevOps — no client secrets to store or rotate.

14.5

Daily use: the commands and the real uses

Resource groups and resources:

az group list
az group create -n rg-lab -l westeurope --tags owner=ops env=lab
az resource list -g rg-shop-prod
az group delete -n rg-lab --yes --no-wait

Virtual machines:

az vm list -d --query "[].{name:name, state:powerState, ip:privateIps}"
az vm create -g rg-lab -n vm1 --image Ubuntu2404 --size Standard_B2s --admin-username azureuser --generate-ssh-keys --public-ip-address ""
az vm stop -g rg-lab -n vm1
az vm deallocate -g rg-lab -n vm1

Storage, AKS, Key Vault and identity:

az storage account list --query "[].{name:name, tls:minimumTlsVersion, public:allowBlobPublicAccess}"
az aks get-credentials -g rg-aks -n aks-prod
az keyvault secret show --vault-name kv-shop --name db-password --query value -o tsv
az role assignment list --assignee alice@example.com --all
az extension add --name ssh

Real uses at work:

  • Build and check resources fast; reports with --query (JMESPath) and -o tsv in scripts.
  • Get the kubeconfig of AKS for kubectl; read secrets from Key Vault in pipelines.
  • Audit: public storage accounts, open NSG rules, role assignments.

Op het werk

vm stop keeps paying for the compute; vm deallocate stops the costs (only the disks cost money).

14.6

Make it safe

Security checklist:

  • MFA (Conditional Access) for every human account.
  • Least privilege with Azure RBAC; give roles to groups, at the resource group, not the subscription.
  • Managed identities and OIDC instead of service principal secrets; if you must use a secret, short expiry and Key Vault.
  • Check the subscription before every change: az account show.
  • ~/.azure/ holds your tokens: never copy it to shared machines; az logout on shared hosts.
  • Keep az updated (az upgrade) — old versions miss security fixes.

Commando's in deze les

14.7

Update and remove

az upgrade
dnf -y upgrade azure-cli
apt-get install --only-upgrade -y azure-cli
dnf -y remove azure-cli
apt-get purge -y azure-cli
rm -rf ~/.azure

14.8

Fix common problems

ProblemCauseFix
Please run 'az login' to setup accountNo login, or the token expired.az login (or --use-device-code).
AADSTS50076 / AADSTS50079MFA or Conditional Access is required.Log in interactively; for a tenant: az login --tenant TENANT_ID.
The subscription '…' could not be foundWrong tenant or no access.az account list --all; log in to the right tenant.
AuthorizationFailed … does not have authorization to perform actionYour role is too small at this scope.az role assignment list --assignee ME --all; ask for a role.
Resources "not found"Wrong subscription or resource group.az account show; az config get defaults.
SSL: CERTIFICATE_VERIFY_FAILED behind a proxyThe company proxy CA is unknown.export REQUESTS_CA_BUNDLE=/etc/pki/tls/certs/ca-bundle.crt after adding the CA.
az: 'xyz' is not in the 'az' command groupThe command needs an extension.az extension add --name xyz (or az config set extension.use_dynamic_install=yes_without_prompt).

14.9

Next steps

1What is the first thing to check before you change resources with az?

The subscription: az account show.

2How should a pipeline log in to Azure without a stored secret?

With workload identity federation (OIDC), or a managed identity when it runs on Azure.

Commando's in deze les

Alle commando's in dit hoofdstuk