Use Azure Cli through Windows Terminal to set and update the public IP address in Azure VM

Original link: https://www.microcharon.top/tech/345.html

Azure Cloud Shell

Since there are several Azure accounts, it would be cumbersome to log in to the portal every time to make some settings, so I chose to use Azure Cli on my Lenovo yoga through the Windows terminal to update the relevant public IPv4 address of the Azure VM

The following updates are only for IPv4 addresses, and IPv6 is not studied here

prepare in advance

  • Azure account enables any valid subscription
  • Azure resource group with at least virtual machine, public IP address, network interface
  • Cloud Shell has been initialized in the Azure Portal
  • Windows terminal can use Azure Cloud Shell

This time I use my pay-as-you-go subscription, 1 B1s type Azure VM in Hong Kong as an example

  • Resource group: AzureVMHK_group
  • Azure VM: AzureVMHK
  • Public IP address: AzureVMHK-ip
  • Network interface: azurevmhk41
  • IP configuration 1: ipconfig1
  • IP configuration 2: ipconfig2

Azure CLI uses

List the required resource names

A total of 3 necessary names are required: resource group name, network interface name, IP configuration name

Use the following commands to list all the resource groups in the Azure account, the network interface names of the resource group AzureVMHK_group, all the public IP addresses of the resource group AzureVMHK_group, and all the IP configuration names of the azurevmhk41 network interface in the resource group AzureVMHK_group

Replace <nic-name> with the name of the network interface and replace <resource-group-name> with the name of the resource group

 az group list --output table #az network nic list --resource-group <resource-group-name> --output table az network nic list --resource-group AzureVMHK_group --output table #az network public-ip list --resource-group <resource-group-name> --output table az network public-ip list --resource-group AzureVMHK_group --output table #az network nic ip-config list --nic-name <nic-name> --resource-group <resource-group-name> --output table az network nic ip-config list --nic-name azurevmhk41 --resource-group AzureVMHK_group --output table

列出所需资源名称

Stop and start virtual machine running (optional)

Only stop the corresponding virtual machine, use the following command Azure will still bill your virtual machine

Replace <vm-name> with the name of the virtual machine and <resource-group-name> with the name of the resource group.

 #az vm stop --name <vm-name> --resource-group <resource-group-name> az vm stop --name AzureVMHK --resource-group AzureVMHK_group #az vm start --name <vm-name> --resource-group <resource-group-name> az vm start --name AzureVMHK --resource-group AzureVMHK_group

Use the –remove parameter to remove the public IP address from the network interface

Use the –remove parameter to remove the public IP address from the IP configuration of the network interface

Replace <ip-config-name> with the name of the IP configuration, <nic-name> with the name of the network interface, and replace <resource-group-name> with the name of the corresponding resource group.

 #az network nic ip-config update --name <ip-config-name> --nic-name <nic-name> --resource-group <resource-group-name> --remove publicIPAddress az network nic ip-config update --name ipconfig1 --nic-name azurevmhk41 --resource-group AzureVMHK_group --remove publicIPAddress

移除公用IP位址

Update the new public IP address and associate it with the corresponding network interface

Please update according to the IP address type, Dynamic or Static

Dynamic public IP address update

Please replace <ip-config-name> with the name of the IP configuration, <nic-name> with the name of the corresponding network interface, <public-ip-name> with the name of the newly created public IP address, and replace <resource-group-name> with the name of the resource group

 #az network nic ip-config update --name <ip-config-name> --nic-name <nic-name> --resource-group <resource-group-name> --public-ip-address <public-ip-name> az network nic ip-config update --name ipconfig1 --nic-name azurevmhk41 --resource-group AzureVMHK_group --public-ip-address AzureVMHK-ip

动态公用IP位址更新

Static public IP address update

Please replace <public-ip-name> with the name of the new public IP address, <static-ip-address> with the static IP address ( 20.24.194.210 in this case), and <resource-group-name> with the name of the resource group

 #az network public-ip create --name <public-ip-name> --resource-group <resource-group-name> --allocation-method Static --ip-address <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 <ip-config-name> --nic-name <nic-name> --resource-group <resource-group-name> --public-ip-address <public-ip-name> az network nic ip-config update --name ipconfig1 --nic-name azurevmhk41 --resource-group AzureVMHK_group --public-ip-address AzureVMHK-ip-1

Query the details of the public IP address

 #az network public-ip show --resource-group <resource-group-name> --name <public-ip-name> --query <JMESPath Str> az network public-ip show --resource-group AzureVMHK_group --name AzureVMHK-ip --query "{fqdn: dnsSettings.fqdn, address: ipAddress, ip_allocation_method: publicIPAllocationMethod}"

The returned json example is as follows

 { "address": "20.24.*.*", "fqdn": "*.eastasia.cloudapp.azure.com", "ip_allocation_method": "Dynamic" }

查询公用IP位址的详细资料

Azure 门户中查看IP位址

References

Azure Cloud Shell Quick Start | Microsoft Learn

az network nic ip-config | Microsoft Learn

az network public-ip | Microsoft Learn

Set the IP address of the Azure web interface | Microsoft Learn

Public IP Addresses – Create Or Update – REST API (Azure Virtual Networks) | Microsoft Learn

This article is transferred from: https://www.microcharon.top/tech/345.html
This site is only for collection, and the copyright belongs to the original author.