使用 Azure CLI 为虚拟机配置专用 IP 地址Configure private IP addresses for a virtual machine using the Azure CLI

系统会从指定的范围自动为虚拟机 (VM) 分配专用 IP 地址,具体取决于 VM 部署时所在的子网。A virtual machine (VM) is automatically assigned a private IP address from a range that you specify, based on the subnet in which the VM is deployed. VM 会保留地址,直到该 VM 被删除。The VM retains the address until the VM is deleted. Azure 从你创建 VM 时所在的子网动态分配下一个可用的专用 IP 地址。Azure dynamically assigns the next available private IP address from the subnet you create a VM in. 如果想要已分配给 VM 的子网中的特定 IP 地址,请分配静态 IP 地址。If you want a specific IP address from the subnet assigned to the VM, assign a static IP address.

方案Scenario

为了更好地说明如何为 VM 配置静态 IP 地址,本文档使用这一方案:To better illustrate how to configure a static IP address for a VM, this document uses this scenario:

虚拟网络方案:前端和后端子网,前端子网具有静态 IP 地址

在此方案中,在 FrontEnd 子网中创建一个名为 DNS01 的 VM,然后将它设置为使用静态 IP 地址 192.168.1.101 。In this scenario, you create a VM named DNS01 in the FrontEnd subnet, and then set it to use a static IP address of 192.168.1.101.

备注

以下示例 Azure CLI 命令需要现有的简单环境。The following sample Azure CLI commands expect an existing simple environment. 若要运行本文档中所显示的命令,请首先构建创建 VNet 中所述的测试环境。If you want to run the commands as they are displayed in this document, first build the test environment described in create a vnet.

在创建 VM 时指定静态专用 IP 地址Specify a static private IP address when creating a VM

若要在名为 TestVNet 的 VNet 的 FrontEnd 子网中使用静态专用 IP 192.168.1.101 创建名为 DNS01 的 VM,请完成以下步骤:To create a VM named DNS01 in the FrontEnd subnet of a VNet named TestVNet with a static private IP of 192.168.1.101, complete the following steps:

  1. 如果尚未这样做,请安装并配置最新的 Azure CLI,并使用 az login 登录 Azure 帐户。If you haven't yet, install and configure the latest Azure CLI and log in to an Azure account using az login.

    备注

    在 Azure China 中使用 Azure CLI 2.0 之前,请首先运行 az cloud set -n AzureChinaCloud 更改云环境。Before you can use Azure CLI 2.0 in Azure China, please run az cloud set -n AzureChinaCloud first to change the cloud environment. 如果要切换回全局 Azure,请再次运行 az cloud set -n AzureCloudIf you want to switch back to Global Azure, run az cloud set -n AzureCloud again.

  2. 运行 az network nic create 命令,创建具有静态专用 IP 的 NIC。Run the az network nic create command to create a NIC with a static private IP. 在输出后显示的列表说明了所用的参数。The list shown after the output explains the parameters used.

    az network nic create \
    --resource-group TestRG \
    --name TestNIC \
    --location chinaeast \
    --subnet FrontEnd \
    --private-ip-address 192.168.1.101 \
    --vnet-name TestVNet
    

    预期输出:Expected output:

    {
        "newNIC": {
            "dnsSettings": {
            "appliedDnsServers": [],
            "dnsServers": []
            },
            "enableIPForwarding": false,
            "ipConfigurations": [
            {
                "etag": "W/\"<guid>\"",
                "id": "/subscriptions/<guid>/resourceGroups/TestRG/providers/Microsoft.Network/networkInterfaces/TestNIC/ipConfigurations/ipconfig1",
                "name": "ipconfig1",
                "properties": {
                "primary": true,
                "privateIPAddress": "192.168.1.101",
                "privateIPAllocationMethod": "Static",
                "provisioningState": "Succeeded",
                "subnet": {
                    "id": "/subscriptions/<guid>/resourceGroups/TestRG/providers/Microsoft.Network/virtualNetworks/TestVNet/subnets/FrontEnd",
                    "resourceGroup": "TestRG"
                }
                },
                "resourceGroup": "TestRG"
            }
            ],
            "provisioningState": "Succeeded",
            "resourceGuid": "<guid>"
        }
    }
    

    参数:Parameters:

    • --private-ip-address:NIC 的静态专用 IP 地址。--private-ip-address: Static private IP address for the NIC.
    • --vnet-name:要在其中创建 NIC 的 VNet 的名称。--vnet-name: Name of the VNet in which to create the NIC.
    • --subnet:要在其中创建 NIC 的子网的名称。--subnet: Name of the subnet in which to create the NIC.
  3. 运行 az vm create 命令,以使用前面创建的公共 IP 和 NIC 创建 VM。Run the az vm create command to create the VM using the public IP and NIC created previously. 在输出后显示的列表说明了所使用的参数。The list shown after the output explains the parameters used.

    az vm create \
    --resource-group TestRG \
    --name DNS01 \
    --location chinaeast \
    --image Debian \
    --admin-username adminuser \
    --ssh-key-value ~/.ssh/id_rsa.pub \
    --nics TestNIC
    

    预期输出:Expected output:

    {
        "fqdns": "",
        "id": "/subscriptions/<guid>/resourceGroups/TestRG/providers/Microsoft.Compute/virtualMachines/DNS01",
        "location": "chinaeast",
        "macAddress": "00-0D-3A-92-C1-66",
        "powerState": "VM running",
        "privateIpAddress": "192.168.1.101",
        "publicIpAddress": "",
        "resourceGroup": "TestRG"
    }
    

    基本 az vm create 参数之外的参数。Parameters other than the basic az vm create parameters.

    • --nics:VM 附加到的 NIC 的名称。--nics: Name of the NIC to which the VM is attached.

我们建议,除非有必要(例如,为一个 Windows VM 分配多个 IP 地址时),否则不要以静态方式在 VM 的操作系统中分配已分配给 Azure 虚拟机的专用 IP。It's recommended that you do not statically assign the private IP assigned to the Azure virtual machine within the operating system of a VM, unless necessary, such as when assigning multiple IP addresses to a Windows VM. 如果确实需要在操作系统中手动设置该专用 IP 地址,请确保它与分配给 Azure 网络接口的专用 IP 地址是同一地址,否则可能会丢失与虚拟机的连接。If you do manually set the private IP address within the operating system, ensure that it is the same address as the private IP address assigned to the Azure network interface, or you can lose connectivity to the virtual machine. 详细了解专用 IP 地址设置。Learn more about private IP address settings.

检索 VM 的静态专用 IP 地址信息Retrieve static private IP address information for a VM

运行以下 Azure CLI 命令以观察 Private IP alloc-methodPrivate IP address 的值:Run the following Azure CLI command to observe the values for Private IP alloc-method and Private IP address:

az vm show -g TestRG -n DNS01 --show-details --query 'privateIps'

预期输出:Expected output:

"192.168.1.101"

若要显示该 VM 的 NIC 的具体 IP 信息,请具体查询 NIC:To display the specific IP information of the NIC for that VM, query the NIC specifically:

az network nic show \
-g testrg \
-n testnic \
--query 'ipConfigurations[0].{PrivateAddress:privateIpAddress,IPVer:privateIpAddressVersion,IpAllocMethod:p
rivateIpAllocationMethod,PublicAddress:publicIpAddress}'

输出类似于:The output is something like:

{
    "IPVer": "IPv4",
    "IpAllocMethod": "Static",
    "PrivateAddress": "192.168.1.101",
    "PublicAddress": null
}

从 VM 中删除静态专用 IP 地址Remove a static private IP address from a VM

无法在用于Azure 资源管理器部署的 Azure CLI 中删除 NIC 的静态专用 IP 地址。You cannot remove a static private IP address from a NIC in Azure CLI for Azure Resource Manager deployments. 必须具备以下条件:You must:

  • 创建使用动态 IP 的新 NICCreate a new NIC that uses a dynamic IP
  • 在新建的 NIC 的 VM 上设置 NIC。Set the NIC on the VM do the newly created NIC.

若要更改上述命令中使用的 VM 的 NIC,请完成以下步骤:To change the NIC for the VM used in the previous commands, complete the following steps:

  1. 运行 azure network nic create 命令,通过具有新 IP 地址的动态 IP 分配创建新 NIC。Run the azure network nic create command to create a new NIC using dynamic IP allocation with a new IP address. 由于未指定任何 IP 地址,因此分配方法为“动态”。Because no IP address is specified, the allocation method is Dynamic.

    az network nic create     \
    --resource-group TestRG     \
    --name TestNIC2     \
    --location chinaeast     \
    --subnet FrontEnd    \
    --vnet-name TestVNet
    

    预期输出:Expected output:

    {
        "newNIC": {
            "dnsSettings": {
            "appliedDnsServers": [],
            "dnsServers": []
            },
            "enableIPForwarding": false,
            "ipConfigurations": [
            {
                "etag": "W/\"<guid>\"",
                "id": "/subscriptions/<guid>/resourceGroups/TestRG/providers/Microsoft.Network/networkInterfaces/TestNIC2/ipConfigurations/ipconfig1",
                "name": "ipconfig1",
                "properties": {
                "primary": true,
                "privateIPAddress": "192.168.1.4",
                "privateIPAllocationMethod": "Dynamic",
                "provisioningState": "Succeeded",
                "subnet": {
                    "id": "/subscriptions/<guid>/resourceGroups/TestRG/providers/Microsoft.Network/virtualNetworks/TestVNet/subnets/FrontEnd",
                    "resourceGroup": "TestRG"
                }
                },
                "resourceGroup": "TestRG"
            }
            ],
            "provisioningState": "Succeeded",
            "resourceGuid": "0808a61c-476f-4d08-98ee-0fa83671b010"
        }
    }
    
  2. 运行 azure vm set 命令以更改 VM 使用的 NIC。Run the azure vm set command to change the NIC used by the VM.

    az vm nic set --resource-group TestRG --vm-name DNS01 --nics TestNIC2
    

    预期输出:Expected output:

    [
        {
            "id": "/subscriptions/0e220bf6-5caa-4e9f-8383-51f16b6c109f/resourceGroups/TestRG/providers/Microsoft.Network/networkInterfaces/TestNIC3",
            "primary": true,
            "resourceGroup": "TestRG"
        }
    ]
    

    备注

    如果 VM 的空间足以拥有多个 NIC,请运行 azure network nic delete 命令删除旧版 NIC。If the VM is large enough to have more than one NIC, run the azure network nic delete command to delete the old NIC.

后续步骤Next steps

了解如何管理 IP 地址设置Learn about managing IP address settings.