
由于 Azure 账户有好几个,每次登录门户进行某些设置会显得麻烦,因此选用在自己的 Lenovo yoga 上通过 Windows 终端使用 Azure Cli 来更新 Azure VM 的相关公用 IPv4 位址
以下仅针对 IPv4 位址的相关更新,IPv6 此处不做研究
预先准备
- Azure 账户启用任意一个有效订阅
- Azure 资源组中至少有虚拟机,公共 IP 位址,网路接口
- Azure 门户中已为 Cloud Shell 进行初始化设定
- Windows 终端可以使用 Azure Cloud Shell
本次使用我的即用即付订阅,1 台香港地区的 B1s 类型 Azure VM 为示例
- 资源组:AzureVMHK_group
- Azure VM:AzureVMHK
- 公共 IP 位址:AzureVMHK-ip
- 网路接口:azurevmhk41
- IP 组态1:ipconfig1
- IP 组态2:ipconfig2
Azure Cli使用
列出所需资源名称
一共需要 3 个必要的名称:资源组名称、网路介面名称、IP 组态名称
使用以下命令分别列出 Azure 账户中的所有资源组、资源组 AzureVMHK_group 的网路介面名称、资源组 AzureVMHK_group 的所有公用 IP 位址,资源组 AzureVMHK_group 中 azurevmhk41网路接口的所有 IP 组态名称
替换为网路接口的名称,并将 替换为资源组的名称
az group list --output table
#az network nic list --resource-group --output table
az network nic list --resource-group AzureVMHK_group --output table
#az network public-ip list --resource-group --output table
az network public-ip list --resource-group AzureVMHK_group --output table
#az network nic ip-config list --nic-name --resource-group --output table
az network nic ip-config list --nic-name azurevmhk41 --resource-group AzureVMHK_group --output table

停止并启动虚拟机运行(可选)
仅对对应虚拟机进行停止操作,使用以下命令 Azure 依然会对你的虚拟机进行计费
请将 替换为虚拟机的名称,并将 替换为资源组的名称。
#az vm stop --name --resource-group
az vm stop --name AzureVMHK --resource-group AzureVMHK_group
#az vm start --name --resource-group
az vm start --name AzureVMHK --resource-group AzureVMHK_group
使用 --remove 参数从网路接口中移除公用IP位址
使用 --remove 参数从网路接口的 IP 组态中移除公用 IP 位址
替换为 IP 组态的名称, 替换为网路接口的名称,并将 替换为对应资源组的名称。
#az network nic ip-config update --name --nic-name --resource-group --remove publicIPAddress
az network nic ip-config update --name ipconfig1 --nic-name azurevmhk41 --resource-group AzureVMHK_group --remove publicIPAddress

更新新的公用IP位址并关联到对应网路接口
请根据 IP 位址类型进行更新,Dynamic or Static
动态公用IP位址更新
请将 替换为 IP 组态的名称, 替换为对应网路接口的名称, 替换为新创建的公共 IP 位址的名称,并将 替换为资源组的名称
#az network nic ip-config update --name --nic-name --resource-group --public-ip-address
az network nic ip-config update --name ipconfig1 --nic-name azurevmhk41 --resource-group AzureVMHK_group --public-ip-address AzureVMHK-ip

静态公用IP位址更新
请将 替换为新公共 IP 位址的名称, 替换为静态 IP 位址(这个情况下为 20.24.194.210),并将 替换为资源组的名称
#az network public-ip create --name --resource-group --allocation-method Static --ip-address
az network public-ip create --name AzureVMHK-ip-1 --resource-group AzureVMHK_group --allocation-method Static --ip-address "20.24.194.210"
#az network nic ip-config update --name --nic-name --resource-group --public-ip-address
az network nic ip-config update --name ipconfig1 --nic-name azurevmhk41 --resource-group AzureVMHK_group --public-ip-address AzureVMHK-ip-1
查询公用IP位址的详细资料
#az network public-ip show --resource-group --name --query
az network public-ip show --resource-group AzureVMHK_group --name AzureVMHK-ip --query "{fqdn: dnsSettings.fqdn, address: ipAddress, ip_allocation_method: publicIPAllocationMethod}"
返回 json 示例如下
{
"address": "20.24.*.*",
"fqdn": "*.eastasia.cloudapp.azure.com",
"ip_allocation_method": "Dynamic"
}


结语
内容可能还有不详实或错误之处,如果有请指出来让我修改
原文连结如下
通过Windows终端使用Azure Cli来设定更新Azure VM中的公用IP位址 - Microcharon
参考资料
Azure Cloud Shell快速入門 | Microsoft Learn
az network nic ip-config | Microsoft Learn
az network public-ip | Microsoft Learn
設定 Azure 網路介面的 IP 位址 | Microsoft Learn
Public IP Addresses - Create Or Update - REST API (Azure Virtual Networks) | Microsoft Learn

没有AZ绑定
学习了