如何使用 Azure CLI 管理 Azure DNS 中的 DNS 区域How to manage DNS Zones in Azure DNS using the Azure CLI

本指南介绍如何通过使用适用于 Windows、Mac 和 Linux 的跨平台 Azure CLI 管理 DNS 区域。This guide shows how to manage your DNS zones by using the cross-platform Azure CLI, which is available for Windows, Mac and Linux. 也可以使用 Azure PowerShell 或 Azure 门户管理 DNS 记区域。You can also manage your DNS zones using Azure PowerShell or the Azure portal.

本指南专门介绍公共 DNS 区域。This guide specifically deals with Public DNS zones. 有关使用 Azure CLI 管理 Azure DNS 中专用区域的详细信息,请参阅使用 Azure CLI 开始使用 Azure DNS 专用区域For information on using Azure CLI to manage Private Zones in Azure DNS, see Get started with Azure DNS Private Zones using Azure CLI.

简介Introduction

DNS 区域用来托管某个特定域的 DNS 记录。A DNS zone is used to host the DNS records for a particular domain. 若要开始在 Azure DNS 中托管域,需要为该域名创建 DNS 区域。To start hosting your domain in Azure DNS, you need to create a DNS zone for that domain name. 随后会在此 DNS 区域内为每个 DNS 记录创建域。Each DNS record for your domain is then created inside this DNS zone.

例如,域“contoso.com”可能包含几条 DNS 记录,如“mail.contoso.com”(用于邮件服务器)和“www.contoso.com”(用于网站)。For example, the domain 'contoso.com' may contain several DNS records, such as 'mail.contoso.com' (for a mail server) and 'www.contoso.com' (for a web site).

在 Azure DNS 中创建 DNS 区域时:When creating a DNS zone in Azure DNS:

  • 在资源组中,区域名称必须是唯一的,不能存在该域。The name of the zone must be unique within the resource group, and the zone must not exist already. 否则,操作会失败。Otherwise, the operation fails.
  • 可在不同资源组或不同 Azure 订阅中重复使用同一区域名称。The same zone name can be reused in a different resource group or a different Azure subscription.
  • 当多个区域共享相同的名称时,将为每个实例分配不同的名称服务器地址。Where multiple zones share the same name, each instance is assigned different name server addresses. 使用域名注册机构仅可配置一组地址。Only one set of addresses can be configured with the domain name registrar.

备注

不必拥有域名即可在 Azure DNS 中以该域名创建 DNS 区域。You do not have to own a domain name to create a DNS zone with that domain name in Azure DNS. 但是,需要拥有域才能通过域名注册机构将 Azure DNS 名称服务器配置为域名的正确名称服务器。However, you do need to own the domain to configure the Azure DNS name servers as the correct name servers for the domain name with the domain name registrar.

有关详细信息,请参阅 向 Azure DNS 委派域For more information, see Delegate a domain to Azure DNS.

设置适用于 Azure DNS 的 Azure CLISet up Azure CLI for Azure DNS

准备阶段Before you begin

在开始配置之前,请确保具备以下各项。Verify that you have the following items before beginning your configuration.

  • Azure 订阅。An Azure subscription. 如果还没有 Azure 订阅,可在开始前创建一个试用帐户If you don't already have an Azure subscription, you can create a trial account before you begin.

  • 安装最新版本的 Azure CLI(在 Windows、Linux 或 MAC 中可用)。Install the latest version of the Azure CLI, available for Windows, Linux, or MAC. 有关详细信息,请参阅安装 Azure CLIMore information is available at Install the Azure CLI.

登录到 Azure 帐户Sign in to your Azure account

打开控制台窗口并使用凭据进行身份验证。Open a console window and authenticate with your credentials. 有关详细信息,请阅读从 Azure CLI 登录 AzureFor more information, see Log in to Azure from the Azure CLI

az login

选择订阅Select the subscription

检查该帐户的订阅。Check the subscriptions for the account.

az account list

选择要使用的 Azure 订阅。Choose which of your Azure subscriptions to use.

az account set --subscription "subscription name"

可选:安装/使用 Azure DNS 专用区域功能Optional: To install/use Azure DNS Private Zones feature

可通过 Azure CLI 的扩展使用 Azure DNS 专用区域功能。The Azure DNS Private Zone feature is available via an extension to the Azure CLI. 安装“dns”Azure CLI 扩展Install the "dns" Azure CLI extension

az extension add --name dns

创建资源组Create a resource group

Azure Resource Manager 要求所有资源组指定一个位置。Azure Resource Manager requires that all resource groups specify a location. 此位置用作该资源组中的资源的默认位置。This is used as the default location for resources in that resource group. 但是,由于所有 DNS 资源都是全局性而非区域性的,因此资源组位置的选择不会影响 Azure DNS。However, because all DNS resources are global, not regional, the choice of resource group location has no impact on Azure DNS.

如果使用现有资源组,可跳过此步骤。You can skip this step if you are using an existing resource group.

az group create --name myresourcegroup --location "China East"

获取帮助Getting help

与 Azure DNS 相关的所有 Azure CLI 命令都以 az network dns 开头。All Azure CLI commands relating to Azure DNS start with az network dns. 可使用 --help 选项(缩写形式 -h)获取有关每个命令的帮助。Help is available for each command using the --help option (short form -h). 例如:For example:

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

创建 DNS 区域Create a DNS zone

使用 az network dns zone create 命令创建 DNS 区域。A DNS zone is created using the az network dns zone create command. 有关帮助,请参阅 az network dns zone create -hFor help, see az network dns zone create -h.

以下示例在名为 MyResourceGroup 的资源组中创建名为 contoso.com 的 DNS 区域:The following example creates a DNS zone called contoso.com in the resource group called MyResourceGroup:

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

使用标记创建 DNS 区域To create a DNS zone with tags

下面的示例演示如何通过 --tags 参数(缩写形式 -t)使用两个 Azure 资源管理器标记、project = demo 和 env = test 创建 DNS 区域:The following example shows how to create a DNS zone with two Azure Resource Manager tags, project = demo and env = test, by using the --tags parameter (short form -t):

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

获取 DNS 区域Get a DNS zone

若要检索 DNS 区域,请使用 az network dns zone showTo retrieve a DNS zone, use az network dns zone show. 有关帮助,请参阅 az network dns zone show --helpFor help, see az network dns zone show --help.

以下示例从资源组 MyResourceGroup 返回 DNS 区域 contoso.com 及其关联数据。The following example returns the DNS zone contoso.com and its associated data from resource group MyResourceGroup.

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

以下示例是响应。The following example is the response.

{
  "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.cn.",
    "ns2-04.azure-dns.cn.",
    "ns3-04.azure-dns.cn.",
    "ns4-04.azure-dns.cn."
  ],
  "numberOfRecordSets": 4,
  "resourceGroup": "myresourcegroup",
  "tags": {},
  "type": "Microsoft.Network/dnszones"
}

请注意,az network dns zone show 不返回 DNS 记录。Note that DNS records are not returned by az network dns zone show. 若要列出 DNS 记录,请使用 az network dns record-set listTo list DNS records, use az network dns record-set list.

列出 DNS 区域List DNS zones

若要枚举 DNS 区域,请使用 az network dns zone listTo enumerate DNS zones, use az network dns zone list. 有关帮助,请参阅 az network dns zone list --helpFor help, see az network dns zone list --help.

指定资源组仅列出资源组内的区域:Specifying the resource group lists only those zones within the resource group:

az network dns zone list --resource-group MyResourceGroup

省略资源组可列出订阅中的所有区域:Omitting the resource group lists all zones in the subscription:

az network dns zone list 

更新 DNS 区域Update a DNS zone

可以使用 az network dns zone update 对 DNS 区域资源进行更改。Changes to a DNS zone resource can be made using az network dns zone update. 有关帮助,请参阅 az network dns zone update --helpFor help, see az network dns zone update --help.

此命令不会更新区域中的任何 DNS 记录集(请参阅如何管理 DNS 记录)。This command does not update any of the DNS record sets within the zone (see How to Manage DNS records). 它仅用于更新区域资源本身的属性。It is only used to update properties of the zone resource itself. 这些属性当前仅限于区域资源的 Azure 资源管理器“标记”These properties are currently limited to the Azure Resource Manager 'tags' for the zone resource.

以下示例演示如何更新 DNS 区域上的标记。The following example shows how to update the tags on a DNS zone. 现有标记替换为指定值。The existing tags are replaced by the value specified.

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

删除 DNS 区域Delete a DNS zone

可以使用 az network dns zone delete 删除 DNS 区域。DNS zones can be deleted using az network dns zone delete. 有关帮助,请参阅 az network dns zone delete --helpFor help, see az network dns zone delete --help.

备注

删除 DNS 区域也会删除该区域中的所有 DNS 记录。Deleting a DNS zone also deletes all DNS records within the zone. 无法撤消此操作。This operation cannot be undone. 如果 DNS 区域在使用中,则使用该区域的服务在区域删除后将无效。If the DNS zone is in use, services using the zone will fail when the zone is deleted.

若要防止意外删除区域,请参阅如何保护 DNS 区域和记录To protect against accidental zone deletion, see How to protect DNS zones and records.

此命令提示用户进行确认。This command prompts for confirmation. 可选的 --yes 开关禁止显示此提示。The optional --yes switch suppresses this prompt.

以下示例演示如何从资源组 MyResourceGroup 中删除区域 contoso.comThe following example shows how to delete the zone contoso.com from resource group MyResourceGroup.

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

后续步骤Next steps

了解如何在 DNS 区域中管理记录集和记录Learn how to manage record sets and records in your DNS zone.

了解如何将域委派给 Azure DNSLearn how to delegate your domain to Azure DNS.