如何使用 Azure CLI 管理 Azure DNS 中的 DNS 區域

本文說明如何使用跨平台的 Azure CLI 來管理 DNS 區域。 Azure CLI 適用於 Windows、Mac 和 Linux。 您也可以使用 Azure PowerShell 或 Azure 入口網站來管理 DNS 區域。

本指南特別說明公用 DNS 區域。 如需在 Azure DNS 中使用 Azure CLI 管理私人區域的資訊,請參閱開始使用 Azure CLI 處理 Azure DNS 私人區域

簡介

DNS 區域用於裝載特定網域的 DNS 記錄。 若要開始將網域裝載到 Azure DNS 中,您必須建立該網域名稱的 DNS 區域。 接著在此 DNS 區域內,建立網域的每筆 DNS 記錄。

例如,網域 'contoso.com' 可能包含數筆 DNS 記錄,例如 'mail.contoso.com' (用於郵件伺服器) 和 'www.contoso.com' (用於網站)。

在 Azure DNS 中建立 DNS 區域時︰

  • 區域的名稱在資源群組內必須是唯一的,且區域必須尚未存在。 否則作業會失敗。
  • 不同的資源群組或不同的 Azure 訂用帳戶中,可重複使用相同的區域名稱。
  • 多個區域共用相同的名稱時,系統就會將不同的名稱伺服器位址,指派給每個執行個體。 只可以向網域名稱註冊機構登記一組位址。

注意

您不必擁有網域名稱,也能在 Azure DNS 中以該網域名稱建立 DNS 區域。 不過,的確需要擁有網域,才能將 Azure DNS 名稱伺服器,設定為網域名稱的正確名稱伺服器,供網域名稱註冊機構使用。

如需詳細資訊,請參閱將網域委派給 Azure DNS

設定適用於 Azure DNS 的 Azure CLI

開始之前

在開始設定之前,請確認您具備下列項目。

  • Azure 訂用帳戶。 如果您還沒有 Azure 訂用帳戶,則可以啟用 MSDN 訂戶權益或註冊免費帳戶

  • 安裝最新版的 Azure CLI,該 CLI 適用於 Windows、Linux 或 MAC。 您可以在 安裝 Azure CLI中取得詳細資訊。

登入您的 Azure 帳戶

開啟主控台視窗,並驗證您的認證。 如需詳細資訊,請參閱從 Azure CLI 登入 Azure

az login

選取訂用帳戶

檢查帳戶的訂用帳戶。

az account list

選擇其中一個要使用的 Azure 訂用帳戶。

az account set --subscription "subscription name"

選擇性:安裝/使用 Azure DNS 私人區域功能

Azure DNS 私用區域功能可透過 Azure CLI 的延伸模組取得。 安裝 “dns” Azure CLI 擴充功能

az extension add --name dns

建立資源群組

Azure Resource Manager 需要資源群組具有指定的位置。 此位置用來作為該資源群組中所有資源的預設位置。 由於所有 DNS 資源皆是全域,因此資源群組位置的選擇不會影響 Azure DNS。

如果您使用現有的資源群組,則可略過此步驟。

az group create --name myresourcegroup --location "West US"

取得說明

與 Azure DNS 相關的所有 Azure CLI 命令都會以 az network dns 開頭。 使用 --help 選項 (簡短形式為 -h) 即可取得每個命令的說明。 例如:

az network dns --help
az network dns zone --help
az network dns zone create --help

建立 DNS 區域

使用 az network dns zone create 命令建立 DNS 區域。 如需協助,請參閱 az network dns zone create -h

下列範例會在稱為 MyResourceGroup 的資源群組中建立稱為 contoso.com 的 DNS 區域:

az network dns zone create --resource-group MyResourceGroup --name contoso.com

使用標籤建立 DNS 區域

下列範例示範如何使用 --tags 參數 (簡短形式為 -t),利用 project = demoenv = test 這兩個 Azure Resource Manager 標籤,建立 DNS 區域:

az network dns zone create --resource-group MyResourceGroup --name contoso.com --tags "project=demo" "env=test"

取得 DNS 區域

若要擷取 DNS 區域,請使用 az network dns zone show。 如需協助,請參閱 az network dns zone show --help

下列範例會從資源群組 MyResourceGroup 傳回 DNS 區域 contoso.com 及其相關聯的資料。

az network dns zone show --resource-group myresourcegroup --name contoso.com

以下是回應範例。

{
  "etag": "00000002-0000-0000-3d4d-64aa3689d201",
  "id": "/subscriptions/147a22e9-2356-4e56-b3de-1f5842ae4a3b/resourceGroups/myresourcegroup/providers/Microsoft.Network/dnszones/contoso.com",
  "location": "global",
  "maxNumberOfRecordSets": 5000,
  "name": "contoso.com",
  "nameServers": [
    "ns1-04.azure-dns.com.",
    "ns2-04.azure-dns.net.",
    "ns3-04.azure-dns.org.",
    "ns4-04.azure-dns.info."
  ],
  "numberOfRecordSets": 4,
  "resourceGroup": "myresourcegroup",
  "tags": {},
  "type": "Microsoft.Network/dnszones"
}

若要列出 DNS 記錄,請使用 az network dns record-set list

列出 DNS 區域

若要列舉 DNS 區域,請使用 az network dns zone list。 如需協助,請參閱 az network dns zone list --help

指定資源群組只會列出資源群組內的區域︰

az network dns zone list --resource-group MyResourceGroup

省略資源群組則會列出訂用帳戶中的所有區域︰

az network dns zone list 

更新 DNS 區域

您可以使用 az network dns zone update變更 DNS 區域資源。 如需協助,請參閱 az network dns zone update --help

此命令不會更新區域內的任何 DNS 記錄集 (請參閱如何管理 DNS 記錄)。 它只用來更新區域資源本身的屬性。 這些屬性目前僅限於區域資源的 Azure Resource Manager「標籤」

下列範例示範如何更新 DNS 區域上的標籤。 現有標籤會由指定值取代。

az network dns zone update --resource-group myresourcegroup --name contoso.com --set tags.team=support

刪除 DNS 區域

可以使用 az network dns zone delete刪除 DNS 區域。 如需協助,請參閱 az network dns zone delete --help

注意

刪除 DNS 區域也會刪除該區域內的所有 DNS 記錄。 此作業無法復原。 如果 DNS 區域正在使用中,當該區域遭到刪除時,使用該區域的服務將會失敗。

若要防止區域意外遭到刪除,請參閱如何保護 DNS 區域和記錄

此命令會提示您確認。 選擇性的 --yes 參數會隱藏這個提示。

下列範例示範如何從資源群組 MyResourceGroup 刪除區域 contoso.com

az network dns zone delete --resource-group myresourcegroup --name contoso.com

下一步

了解如何在 DNS 區域中管理記錄集和記錄

了解如何將您的網域委派給 Azure DNS