使用 Azure CLI 修改本地网络网关设置Modify local network gateway settings using the Azure CLI

有时,本地网络网关的地址前缀或网关 IP 地址的设置会变更。Sometimes the settings for your local network gateway Address Prefix or Gateway IP Address change. 本文演示如何修改本地网络网关设置。This article shows you how to modify your local network gateway settings. 还可以选择以下列表中的其他选项,使用另一种方法来修改这些设置:You can also modify these settings using a different method by selecting a different option from the following list:

准备工作Before you begin

安装最新版本的 CLI 命令(2.0 或更高版本)。Install the latest version of the CLI commands (2.0 or later). 有关安装 CLI 命令的信息,请参阅安装 Azure CLIFor information about installing the CLI commands, see Install the Azure CLI.

使用 az login 命令登录到 Azure 订阅,并按照屏幕上的说明进行操作。Sign in to your Azure subscription with the az login command and follow the on-screen directions. 有关登录的详细信息,请参阅 Azure CLI 入门For more information about signing in, see Get Started with Azure CLI.

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.

如果有多个 Azure 订阅,请列出该帐户的订阅。If you have more than one Azure subscription, list the subscriptions for the account.

az account list --all

指定要使用的订阅。Specify the subscription that you want to use.

az account set --subscription <replace_with_your_subscription_id>

修改 IP 地址前缀Modify IP address prefixes

修改本地网关 IP 地址前缀 - 无网关连接To modify local network gateway IP address prefixes - no gateway connection

如果没有网关连接且需要添加或删除 IP 地址前缀,则可使用 az network local-gateway create 命令,该命令也是用来创建本地网关的。If you don't have a gateway connection and you want to add or remove IP address prefixes, you use the same command that you use to create the local network gateway, az network local-gateway create. 也可使用该命令来更新 VPN 设备的网关 IP 地址。You can also use this command to update the gateway IP address for the VPN device. 请使用本地网关的现有名称来覆盖当前设置。To overwrite the current settings, use the existing name of your local network gateway. 如果使用其他名称,请创建一个新的本地网关,而不是覆盖现有的。If you use a different name, you create a new local network gateway, instead of overwriting the existing one.

每次进行更改时,必须指定前缀的完整列表,不能仅指定要更改的前缀。Each time you make a change, the entire list of prefixes must be specified, not just the prefixes that you want to change. 仅指定需要保留的前缀。Specify only the prefixes that you want to keep. 此例中为 10.0.0.0/24 和 20.0.0.0/24In this case, 10.0.0.0/24 and 20.0.0.0/24

az network local-gateway create --gateway-ip-address 23.99.221.164 --name Site2 -g TestRG1 --local-address-prefixes 10.0.0.0/24 20.0.0.0/24

修改本地网关 IP 地址前缀 - 存在网关连接To modify local network gateway IP address prefixes - existing gateway connection

如果有网关连接且需要添加或删除 IP 地址前缀,可使用 az network local-gateway update 更新前缀。If you have a gateway connection and want to add or remove IP address prefixes, you can update the prefixes using az network local-gateway update. 这会导致 VPN 连接中断一段时间。This results in some downtime for your VPN connection. 修改 IP 地址前缀时,不需删除 VPN 网关。When modifying the IP address prefixes, you don't need to delete the VPN gateway.

每次进行更改时,必须指定前缀的完整列表,不能仅指定要更改的前缀。Each time you make a change, the entire list of prefixes must be specified, not just the prefixes that you want to change. 在此示例中,10.0.0.0/24 和 20.0.0.0/24 已存在。In this example, 10.0.0.0/24 and 20.0.0.0/24 are already present. 我们会添加前缀 30.0.0.0/24 和 40.0.0.0/24,并在更新时指定所有 4 个前缀。We add the prefixes 30.0.0.0/24 and 40.0.0.0/24 and specify all 4 of the prefixes when updating.

az network local-gateway update --local-address-prefixes 10.0.0.0/24 20.0.0.0/24 30.0.0.0/24 40.0.0.0/24 --name VNet1toSite2 -g TestRG1

修改网关 IP 地址Modify the gateway IP address

修改本地网关的“gatewayIpAddress”To modify the local network gateway 'gatewayIpAddress'

如果要连接的 VPN 设备已更改其公共 IP 地址,则需根据该更改修改本地网关。If the VPN device that you want to connect to has changed its public IP address, you need to modify the local network gateway to reflect that change. 可以更改网关 IP 地址而不删除现有的 VPN 网关连接(如果有)。The gateway IP address can be changed without removing an existing VPN gateway connection (if you have one). 要修改网关 IP 地址,请使用 az network local-gateway update 命令将值“Site2”和“TestRG1”替换为自己的值。To modify the gateway IP address, replace the values 'Site2' and 'TestRG1' with your own using the az network local-gateway update command.

az network local-gateway update --gateway-ip-address 23.99.222.170 --name Site2 --resource-group TestRG1

验证输出中的 IP 地址是否正确:Verify that the IP address is correct in the output:

"gatewayIpAddress": "23.99.222.170",

后续步骤Next steps

可验证网关连接。You can verify your gateway connection. 请参阅验证网关连接See Verify a gateway connection.