azure cli

Azure CLI 2.60+

Azure CLI(az)命令快速參考:驗證、subscription/資源管理、VM/AKS/Storage/App Service/Key Vault、query 格式化、CI 友善工作流——附常用參數與實作範例。

43 條命令

說明

az --version

印出 Azure CLI 版本與其使用的 Python 直譯器。

az --version
az find <query>

依關鍵字搜尋 `az` 指令——使用 CLI 隨附的靜態搜尋索引。

az find 'aks'
az <group> --help

顯示某個群組下的所有指令。

<group> --help;<group> <command> --help

az storage --help
az <group> <command> --help

顯示單一指令的旗標與範例。

az vm create --help
az interactive

進入具有自動補完、語法高亮與內嵌文件的互動式 shell。

--style;--update;--no-param-expansion

az interactive

驗證

az login

啟動瀏覽器式登入(無顯示器時使用 device code 流程)。

--use-device-code;--tenant <tenant>;--service-principal -u <id> -p <secret> --tenant <tenant>;--federated-token <token>;--identity

az login
az login --use-device-code

使用 device-code 流程——印出 URL 與代碼,可貼到任何裝置的瀏覽器。

az login --use-device-code --tenant contoso.onmicrosoft.com
az login --service-principal -u <appId> -p <password> --tenant <tenant>

以 client secret 登入 service principal(CI/自動化)。

-u <id>;-p <secret>;--tenant <tenant>;--federated-token <jwt>

az login --service-principal -u $APP_ID -p $APP_SECRET --tenant $TENANT
az logout

清除所有憑證並結束當前工作階段。

--username <upn>

az logout
az account show

印出目前使用中的 subscription、tenant 與使用者。

--output table;--query

az account show --output table
az account list

列出帳號可存取的所有 subscription。

--output table;--query '[].{name:name,id:id}';--all

az account list --output table
az account set --subscription <id>

切換後續指令使用的 subscription。

--subscription <id>;--name <name>

az account set --subscription 'My Dev Subscription'
az account clear

從本地快取清除所有 subscription(強制重新登入)。

az account clear && az login

輸出

az <cmd> --output <format>

選擇輸出格式:`json`(預設)、`table`、`tsv`、`yaml`、`yamlc`、`none`。

json;table;tsv;yaml;yamlc;none

az vm list --output table
az <cmd> --query '<JMES>'

以 JMESPath 運算式投影回應。

--query '[].name';--query 'length(@)';--query 'sort_by(@, &name)'

az vm list --query '[].{name:name,rg:resourceGroup,size:hardwareProfile.vmSize}' --output table
az <cmd> --no-wait

立即返回,不等待長時間執行作業完成(之後可用 `az <verb> wait` 輪詢)。

az vm create --no-wait --resource-group rg --name myvm --image image

資源群組

az group list

列出當前 subscription 中的所有資源群組。

--output table;--query '[].name'

az group list --output table
az group create --name <rg> --location <loc>

建立新資源群組。

--name;--location eastus;--tags env=prod

az group create --name rg-prod --location eastus --tags env=prod
az group delete --name <rg>

刪除資源群組與其內含的所有資源(不可逆)。

--name;--yes;--no-wait

az group delete --name rg-old --yes --no-wait

資源

az resource list

列出當前 subscription 下的所有 Azure 資源。

--resource-group <rg>;--resource-type <type>;--name <name>;--tag env=prod

az resource list --output table
az tag

以資源群組為單位管理資源標籤。

create;update;list;delete

az tag create --resource-group rg-prod --tags env=prod owner=alice

VM

az vm list

列出所有 VM 及其規格與狀態。

--resource-group;--show-details;--query

az vm list --output table
az vm start --name <name> --resource-group <rg>

啟動 VM(若先前已停止,會取消分配計算資源)。

--no-wait

az vm start -n vm-dev -g rg-dev --no-wait
az vm stop --name <name> --resource-group <rg>

停止(取消分配)VM 以停止計算計費。

--no-wait

az vm deallocate -n vm-dev -g rg-dev --no-wait
az ssh vm --name <name> --resource-group <rg>

使用 Azure CLI 2.30+ 新的原生 SSH 支援,無需明確的公用 IP 即可開啟工作階段。

-n;-g;--ssh-key-file <path>;--port <p>

az ssh vm --name vm-dev --resource-group rg-dev

AKS

az aks list

列出所有 AKS 叢集。

--resource-group;--output table

az aks list --output table
az aks get-credentials --name <name> --resource-group <rg> --overwrite-existing

把叢集的 kubeconfig 合併到 `~/.kube/config` 並切換 context。

--name;--resource-group;--admin;--overwrite-existing;--file <kubeconfig>

az aks get-credentials --name my-cluster --resource-group rg-prod --overwrite-existing
az aks create --name <name> --resource-group <rg>

建立 AKS 叢集。最常用的旗標:`--node-count`、`--node-vm-size`、`--kubernetes-version`、`--generate-ssh-keys`。

--name;-g;--node-count;--node-vm-size;--kubernetes-version;--generate-ssh-keys;--service-principal;--no-ssh-key

az aks create -g rg-prod -n my-cluster --node-count 3 --generate-ssh-keys

Storage

az storage account list

列出所有儲存體帳戶。

--output table;--query '[].name'

az storage account list --output table
az storage blob upload --account-name <acc> --container-name <c> --name <blob> --file <path>

把檔案以 blob 形式上傳。可使用 `--account-key`、SAS token 或 auth-mode login。

--account-name;--container-name;--name;--file <local>;--auth-mode login;--sas-token;--overwrite

az storage blob upload --account-name mysa --container-name uploads --name backup.zip --file backup.zip --auth-mode login
az storage blob download --account-name <acc> --container-name <c> --name <blob> --file <path>

把 blob 下載到本地路徑。

--account-name;--container-name;--name;--file <local>;--auth-mode login

az storage blob download --account-name mysa --container-name uploads --name backup.zip --file ./backup.zip

App Service

az webapp list

列出所有 App Service webapp。

--resource-group;--output table

az webapp list --output table
az webapp deploy --name <name> --resource-group <rg> --src-path <local-zip>

把建置好的構件部署到 webapp(zip deploy)。

--src-path <zip>;--type zip;--target-path;--async

az webapp deploy -g rg-prod -n web-prod --src-path ./app.zip
az webapp log tail --name <name> --resource-group <rg>

把容器日誌輸出串流到終端機。

--name;-g;--provider

az webapp log tail -n web-prod -g rg-prod

Key Vault

az keyvault list

列出 subscription 中的所有 Key Vault。

--output table;--resource-type vault

az keyvault list --output table
az keyvault secret set --vault-name <kv> --name <name> --value <value>

新增或更新秘密。可用 `--file <path>` 從檔案載入。

--vault-name;--name;--value;--file;--encoding utf-8

az keyvault secret set --vault-name my-kv --name DB-PASSWORD --value $DB_PASSWORD
az keyvault secret show --vault-name <kv> --name <name>

讀取秘密的 metadata 與值。

az keyvault secret show --vault-name my-kv --name DB-PASSWORD --query 'value' -o tsv

Container Registry

az acr list

列出所有 Container Registry。

--resource-group;--output table

az acr list --output table
az acr login --name <name>

把 Docker 登入 ACR——之後 `docker push/pull` 即可直接運作。

az acr login --name myregistry

診斷

az configure

管理 CLI 預設值(output 格式、預設 location、預設群組等)。

--defaults;--list-defaults

az configure --defaults location=eastus group=rg-prod
az upgrade

把 Azure CLI 升級到最新版本(僅 Linux——Windows 用安裝程式升級)。

az upgrade
az extension list

列出已安裝的 CLI 擴充(az aks-preview、az spring 等)。

list;show;add;remove;update

az extension add --name aks-preview
az --debug <cmd>

把 HTTP 請求/回應除錯資訊印到 stderr。

--debug;--only-show-errors

az --debug vm list 2> az.log

相關命令速查

關於 Azure CLI

Azure CLI(`az`)是 Microsoft 官方的命令列工具,用於管理 Azure 資源。2014 年發佈,以 Python 建置,`az` 跨平台(Windows、macOS、Linux),以單一 MSI/pkg/apt/brew 安裝,是每個 Azure 場景(過去需要 PowerShell `Az` 模組)的預設 CLI。目前穩定版線為 Azure CLI 2.60+(Python 3.11+ 執行環境)。`az` 遵循動詞-名詞慣例:`az <verb> <noun>`(例如 `az storage blob upload`、`az aks get-credentials`、`az vm list`)。指令支援 JMESPath query(`--query`)、表格/json/yaml/tsv 輸出(`--output`),以及資源層級的自動補完。驗證支援互動式 `az login`、service principal、managed identity 與 OIDC/federated credentials(CI 無需 secret)。Azure CLI 以 MIT 授權,除了明確的 API 呼叫之外,不會上傳源碼。

速查頁版本 1.0.0