创建、更改或删除路由表Create, change, or delete a route table

Azure 自动在 Azure 子网、虚拟网络与本地网络之间路由流量。Azure automatically routes traffic between Azure subnets, virtual networks, and on-premises networks. 若要更改 Azure 的任何默认路由,可以创建一个路由表。If you want to change any of Azure's default routing, you do so by creating a route table. 如果你不熟悉虚拟网络中的路由,可在虚拟网络流量路由中了解详细信息,或者通过完成一个教程了解详细信息。If you're new to routing in virtual networks, you can learn more about it in virtual network traffic routing or by completing a tutorial.

准备阶段Before you begin

如果你没有 Azure 帐户,请使用有效的订阅设置一个帐户。If you don't have one, set up an Azure account with an active subscription. 免费创建订阅Create an account for free. 然后在开始执行本文任一部分中的步骤之前完成以下任务之一:Then complete one of these tasks before starting steps in any section of this article:

  • 门户用户 :使用 Azure 帐户登录到 Azure 门户Portal users : Sign in to the Azure portal with your Azure account.

  • PowerShell 用户 :在计算机中运行 PowerShell。PowerShell users : Run PowerShell from your computer.

    如果在本地运行 PowerShell,请使用 Azure PowerShell 模块 1.0.0 或更高版本。When you're running PowerShell locally, use Azure PowerShell module version 1.0.0 or later. 运行 Get-Module -ListAvailable Az.Network 查找已安装的版本。Run Get-Module -ListAvailable Az.Network to find the installed version. 如果需要进行升级,请参阅 Install Azure PowerShell module(安装 Azure PowerShell 模块)。If you need to upgrade, see Install Azure PowerShell module. 另请运行 Connect-AzAccount -Environment AzureChinaCloud 以创建与 Azure 的连接。Also run Connect-AzAccount -Environment AzureChinaCloud to create a connection with Azure.

  • Azure 命令行接口 (CLI) 用户 :在计算机中运行 CLI。Azure Command-line interface (CLI) users : Run the CLI from your computer. 如果在本地运行 Azure CLI,请使用 Azure CLI 2.0.31 或更高版本。Use Azure CLI version 2.0.31 or later if you're running the Azure CLI locally. 运行 az --version 查找已安装的版本。Run az --version to find the installed version. 如果需要进行安装或升级,请参阅安装 Azure CLIIf you need to install or upgrade, see Install Azure CLI. 另请运行 az login 以创建与 Azure 的连接。Also run az login to create a connection with Azure.

用于登录或者用于连接 Azure 的帐户必须分配有网络参与者角色,或者分配有自定义角色,并且该自定义角色分配有权限中列出的相应操作。The account you log into, or connect to Azure with must be assigned to the Network contributor role or to a Custom role that's assigned the appropriate actions listed in Permissions.

创建路由表Create a route table

在每个 Azure 位置和订阅中可创建的路由表数目有限制。There's a limit to how many route tables you can create per Azure location and subscription. 有关详细信息,请参阅网络限制 - Azure 资源管理器For details, see Networking limits - Azure Resource Manager.

  1. 在 。On the Azure portal menu or from the Home page, select Create a resource .

  2. 在搜索框中,输入“路由表” 。In the search box, enter Route table . 当“路由表”出现在搜索结果中时,请选择它。 When Route table appears in the search results, select it.

  3. 在“路由表” 页中,选择“创建” 。In the Route table page, select Create .

  4. 在“创建路由表”对话框中: In the Create route table dialog box:

    1. 输入路由表的名称 。Enter a Name for the route table.
    2. 选择“订阅” 。Choose your Subscription .
    3. 选择现有的资源组,或选择“新建”以创建新的资源组。 Choose an existing Resource group or select Create new to create a new resource group.
    4. 选择“位置” 。Choose a Location .
    5. 如果你计划将路由表与通过 VPN 网关连接到本地网络的虚拟网络中的子网相关联,并且不希望将本地路由传播到子网中的网络接口,请将“虚拟网络网关路由传播”设置为“已禁用” 。If you plan to associate the route table to a subnet in a virtual network that's connected to your on-premises network through a VPN gateway, and you don't want to propagate your on-premises routes to the network interfaces in the subnet, set Virtual network gateway route propagation to Disabled .
  5. 选择“创建”以创建新的路由表 。Select Create to create your new route table.

创建路由表 - 命令Create route table - commands

工具Tool 命令Command
Azure CLIAzure CLI az network route-table createaz network route-table create
PowerShellPowerShell New-AzRouteTableNew-AzRouteTable

查看路由表View route tables

转到 Azure 门户来管理虚拟网络。Go to the Azure portal to manage your virtual network. 搜索并选择“路由表”。 Search for and select Route tables . 随后将列出订阅中存在的路由表。The route tables that exist in your subscription are listed.

查看路由表 - 命令View route table - commands

工具Tool 命令Command
Azure CLIAzure CLI az network route-table listaz network route-table list
PowerShellPowerShell Get-AzRouteTableGet-AzRouteTable

查看路由表详细信息View details of a route table

  1. 转到 Azure 门户来管理虚拟网络。Go to the Azure portal to manage your virtual network. 搜索并选择“路由表”。 Search for and select Route tables .

  2. 在路由表列表中,选择要查看其详细信息的路由表。In the route table list, choose the route table that you want to view details for.

  3. 在路由表页中的“设置” 下,查看路由表中的“路由” ,或者查看与该路由表关联的“子网” 。In the route table page, under Settings , view the Routes in the route table or the Subnets the route table is associated to.

若要详细了解常见的 Azure 设置,请参阅以下信息:To learn more about common Azure settings, see the following information:

查看路由表详细信息 - 命令View details of route table - commands

工具Tool 命令Command
Azure CLIAzure CLI az network route-table showaz network route-table show
PowerShellPowerShell Get-AzRouteTableGet-AzRouteTable

更改路由表Change a route table

  1. 转到 Azure 门户来管理虚拟网络。Go to the Azure portal to manage your virtual network. 搜索并选择“路由表”。 Search for and select Route tables .

  2. 在路由表列表中,选择要更改的路由表。In the route table list, choose the route table that you want to change.

最常见的更改是添加路由、删除路由、将路由表关联到子网,或者从子网取消关联路由表。The most common changes are to add routes, remove routes, associate route tables to subnets, or dissociate route tables from subnets.

更改路由表 - 命令Change a route table - commands

工具Tool 命令Command
Azure CLIAzure CLI az network route-table updateaz network route-table update
PowerShellPowerShell Set-AzRouteTableSet-AzRouteTable

将路由表关联到子网Associate a route table to a subnet

可以选择性地将路由表关联到子网。You can optionally associate a route table to a subnet. 一个路由表可与零个或多个子网相关联。A route table can be associated to zero or more subnets. 由于路由表不会关联到虚拟网络,因此,必须将路由表关联到你希望该路由表关联到的每个子网。Because route tables aren't associated to virtual networks, you must associate a route table to each subnet you want the route table associated to. 如果虚拟网络已连接到 Azure 虚拟网络网关(ExpressRoute 或 VPN),则 Azure 会根据路由表中创建的路由、默认路由以及从本地网络传播的路由,来路由离开子网的所有流量。Azure routes all traffic leaving the subnet based on routes you've created within route tables, default routes, and routes propagated from an on-premises network, if the virtual network is connected to an Azure virtual network gateway (ExpressRoute or VPN). 只能将路由表关联到该路由表所在的同一 Azure 位置和订阅中的虚拟网络内的子网。You can only associate a route table to subnets in virtual networks that exist in the same Azure location and subscription as the route table.

  1. 转到 Azure 门户来管理虚拟网络。Go to the Azure portal to manage your virtual network. 搜索并选择“虚拟网络”。 Search for and select Virtual networks .

  2. 在虚拟网络列表中,选择包含要将路由表关联到的子网的虚拟网络。In the virtual network list, choose the virtual network that contains the subnet you want to associate a route table to.

  3. 在虚拟网络菜单栏中选择“子网”。 In the virtual network menu bar, choose Subnets .

  4. 选择要将路由表关联到的子网。Select the subnet you want to associate the route table to.

  5. 在“路由表”中,选择要关联到子网的路由表。 In Route table , choose the route table you want to associate to the subnet.

  6. 选择“保存” 。Select Save .

如果虚拟网络已连接到 Azure VPN 网关,请不要将路由表与包含目标为 0.0.0.0/0 的路由的 If your virtual network is connected to an Azure VPN gateway, don't associate a route table to the gateway subnet that includes a route with a destination of 0.0.0.0/0 . 这样做可能会阻止网关正常工作。Doing so can prevent the gateway from functioning properly. 有关在路由中使用 0.0.0.0/0 的详细信息,请参阅 For more information about using 0.0.0.0/0 in a route, see Virtual network traffic routing.

关联路由表 - 命令Associate a route table - commands

工具Tool 命令Command
Azure CLIAzure CLI az network vnet subnet updateaz network vnet subnet update
PowerShellPowerShell Set-AzVirtualNetworkSubnetConfigSet-AzVirtualNetworkSubnetConfig

从子网取消关联路由表Dissociate a route table from a subnet

从子网取消关联路由表后,Azure 会根据流量的默认路由来路由流量。When you dissociate a route table from a subnet, Azure routes traffic based on its default routes.

  1. 转到 Azure 门户来管理虚拟网络。Go to the Azure portal to manage your virtual network. 搜索并选择“虚拟网络”。 Search for and select Virtual networks .

  2. 在虚拟网络列表中,选择包含要与路由表取消关联的子网的虚拟网络。In the virtual network list, choose the virtual network that contains the subnet you want to dissociate a route table from.

  3. 在虚拟网络菜单栏中选择“子网”。 In the virtual network menu bar, choose Subnets .

  4. 选择要从中取消关联路由表的子网。Select the subnet you want to dissociate the route table from.

  5. 在“路由表”中选择“无”。 In Route table , choose None .

  6. 选择“保存” 。Select Save .

取消关联路由表 - 命令Dissociate a route table - commands

工具Tool 命令Command
Azure CLIAzure CLI az network vnet subnet updateaz network vnet subnet update
PowerShellPowerShell Set-AzVirtualNetworkSubnetConfigSet-AzVirtualNetworkSubnetConfig

删除路由表Delete a route table

不能删除已关联到任何子网的路由表。You can't delete a route table that's associated to any subnets. 在尝试删除路由表之前,请从所有子网取消关联该路由表。Dissociate a route table from all subnets before attempting to delete it.

  1. 转到 Azure 门户来管理路由表。Go to the Azure portal to manage your route tables. 搜索并选择“路由表”。 Search for and select Route tables .

  2. 在路由表列表中,选择要删除的路由表。In the route table list, choose the route table you want to delete.

  3. 选择“删除”,然后在确认对话框中选择“是”。 Select Delete , and then select Yes in the confirmation dialog box.

删除路由表 - 命令Delete a route table - commands

工具Tool 命令Command
Azure CLIAzure CLI az network route-table deleteaz network route-table delete
PowerShellPowerShell Remove-AzRouteTableRemove-AzRouteTable

创建路由Create a route

在每个 Azure 位置和订阅中,可为每个路由表创建的路由数目有限制。There's a limit to how many routes per route table can create per Azure location and subscription. 有关详细信息,请参阅网络限制 - Azure 资源管理器For details, see Networking limits - Azure Resource Manager.

  1. 转到 Azure 门户来管理路由表。Go to the Azure portal to manage your route tables. 搜索并选择“路由表”。 Search for and select Route tables .

  2. 在路由表列表中,选择要将路由添加到的路由表。In the route table list, choose the route table you want to add a route to.

  3. 在路由表菜单栏中,选择“路由” > “添加”。 From the route table menu bar, choose Routes > Add .

  4. 为该路由输入一个在路由表中唯一的路由名称 。Enter a unique Route name for the route within the route table.

  5. 以无类域间路由 (CIDR) 表示法输入要将流量路由到的地址前缀。 Enter the Address prefix , in Classless Inter-Domain Routing (CIDR) notation, that you want to route traffic to. 该前缀不能在路由表的多个路由中重复,不过,可以包含在另一个前缀中。The prefix can't be duplicated in more than one route within the route table, though the prefix can be within another prefix. 例如,如果在一个路由中定义了 10.0.0.0/16 作为前缀,仍可使用 10.0.0.0/22 地址前缀定义另一个路由。 For example, if you defined 10.0.0.0/16 as a prefix in one route, you can still define another route with the 10.0.0.0/22 address prefix. Azure 根据最长的前缀匹配项选择流量的路由。Azure selects a route for traffic based on longest prefix match. 有关详细信息,请参阅 Azure 如何选择路由To learn more, see How Azure selects a route.

  6. 选择一个“下一跃点类型”。 Choose a Next hop type . 若要详细了解下一跃点类型,请参阅虚拟网络流量路由To learn more about next hop types, see Virtual network traffic routing.

  7. 如果为“下一跃点类型”选择了“虚拟设备”,请为“下一跃点地址”输入一个 IP 地址。 If you chose a Next hop type of Virtual appliance , enter an IP address for Next hop address .

  8. 选择“确定” 。Select OK .

创建路由 - 命令Create a route - commands

工具Tool 命令Command
Azure CLIAzure CLI az network route-table route createaz network route-table route create
PowerShellPowerShell New-AzRouteConfigNew-AzRouteConfig

查看路由View routes

一个路由表包含零个或多个路由。A route table contains zero or more routes. 若要详细了解在查看路由时所列出的信息,请参阅虚拟网络流量路由To learn more about the information listed when viewing routes, see Virtual network traffic routing.

  1. 转到 Azure 门户来管理路由表。Go to the Azure portal to manage your route tables. 搜索并选择“路由表”。 Search for and select Route tables .

  2. 在路由表列表中,选择要查看其路由的路由表。In the route table list, choose the route table you want to view routes for.

  3. 在路由表菜单栏中,选择“路由”查看路由列表。 In the route table menu bar, choose Routes to see the list of routes.

查看路由 - 命令View routes - commands

工具Tool 命令Command
Azure CLIAzure CLI az network route-table route listaz network route-table route list
PowerShellPowerShell Get-AzRouteConfigGet-AzRouteConfig

查看路由详细信息View details of a route

  1. 转到 Azure 门户来管理路由表。Go to the Azure portal to manage your route tables. 搜索并选择“路由表”。 Search for and select Route tables .

  2. 在路由表列表中,选择包含你要查看其详细信息的路由的路由表。In the route table list, choose the route table containing the route you want to view details for.

  3. 在路由表菜单栏中,选择“路由”查看路由列表。 In the route table menu bar, choose Routes to see the list of routes.

  4. 选择要查看其详细信息的路由。Select the route you want to view details of.

查看路由详细信息 - 命令View details of a route - commands

工具Tool 命令Command
Azure CLIAzure CLI az network route-table route showaz network route-table route show
PowerShellPowerShell Get-AzRouteConfigGet-AzRouteConfig

更改路由Change a route

  1. 转到 Azure 门户来管理路由表。Go to the Azure portal to manage your route tables. 搜索并选择“路由表”。 Search for and select Route tables .

  2. 在路由表列表中,选择包含你要更改的路由的路由表。In the route table list, choose the route table containing the route you want to change.

  3. 在路由表菜单栏中,选择“路由”查看路由列表。 In the route table menu bar, choose Routes to see the list of routes.

  4. 选择要更改的路由。Choose the route you want to change.

  5. 将现有设置更改为新设置,然后选择“保存”。 Change existing settings to their new settings, then select Save .

更改路由 - 命令Change a route - commands

工具Tool 命令Command
Azure CLIAzure CLI az network route-table route updateaz network route-table route update
PowerShellPowerShell Set-AzRouteConfigSet-AzRouteConfig

删除路由Delete a route

  1. 转到 Azure 门户来管理路由表。Go to the Azure portal to manage your route tables. 搜索并选择“路由表”。 Search for and select Route tables .

  2. 在路由表列表中,选择包含你要删除的路由的路由表。In the route table list, choose the route table containing the route you want to delete.

  3. 在路由表菜单栏中,选择“路由”查看路由列表。 In the route table menu bar, choose Routes to see the list of routes.

  4. 选择要删除的路由。Choose the route you want to delete.

  5. 选择“删除”,然后在确认对话框中选择“是”。 Select Delete , then select Yes in the confirmation dialog box.

删除路由 - 命令Delete a route - commands

工具Tool 命令Command
Azure CLIAzure CLI az network route-table route deleteaz network route-table route delete
PowerShellPowerShell Remove-AzRouteConfigRemove-AzRouteConfig

查看有效路由View effective routes

附加到 VM 的每个网络接口的有效路由是你已创建的路由表、Azure 的默认路由,以及通过 Azure 虚拟网络网关和边界网关协议 (BGP) 从本地网络传播的任何路由的组合。The effective routes for each VM-attached network interface are a combination of route tables that you've created, Azure's default routes, and any routes propagated from on-premises networks via the Border Gateway Protocol (BGP) through an Azure virtual network gateway. 排查路由问题时,了解网络接口的有效路由非常有用。Understanding the effective routes for a network interface is helpful when troubleshooting routing problems. 可以查看已附加到运行中 VM 的任何网络接口的有效路由。You can view the effective routes for any network interface that's attached to a running VM.

  1. 转到 Azure 门户来管理 VM。Go to the Azure portal to manage your VMs. 搜索并选择“虚拟机” 。Search for and select Virtual machines .

  2. 在虚拟机列表中,选择要查看其有效路由的 VM。In the virtual machine list, choose the VM you want to view effective routes for.

  3. 在 VM 菜单栏中选择“网络”。 In the VM menu bar, choose Networking .

  4. 选择网络接口的名称。Select the name of a network interface.

  5. 在网络接口菜单栏中选择“有效路由”。 In the network interface menu bar, select Effective routes .

  6. 查看有效路由的列表,以了解你要将流量路由到的目标地址是否存在适当的路由。Review the list of effective routes to see whether the correct route exists for where you want to route traffic to. 虚拟网络流量路由中详细了解此列表中列出的下一跃点类型。Learn more about next hop types that you see in this list in Virtual network traffic routing.

查看有效路由 - 命令View effective routes - commands

工具Tool 命令Command
Azure CLIAzure CLI az network nic show-effective-route-tableaz network nic show-effective-route-table
PowerShellPowerShell Get-AzEffectiveRouteTableGet-AzEffectiveRouteTable

验证两个终结点之间的路由Validate routing between two endpoints

可以确定虚拟机与另一 Azure 资源的 IP 地址、本地资源或 Internet 上某个资源之间的下一跃点类型。You can determine the next hop type between a virtual machine and the IP address of another Azure resource, an on-premises resource, or a resource on the Internet. 排查路由问题时,确定 Azure 的路由很有帮助。Determining Azure's routing is helpful when troubleshooting routing problems. 若要完成此任务,必须使用现有的网络观察程序。To complete this task, you must have an existing network watcher. 如果没有网络观察程序,可以完成创建网络观察程序实例中的步骤来创建一个。If you don't have an existing network watcher, create one by completing the steps in Create a Network Watcher instance.

  1. 转到 Azure 门户来管理网络观察程序。Go to the Azure portal to manage your network watchers. 搜索并选择“网络观察程序”。 Search for and select Network Watcher .

  2. 在网络观察程序菜单栏中选择“下一跃点”。 In the network watcher menu bar, choose Next hop .

  3. 在“网络观察程序 | 下一跃点”页中: In the Network Watcher | Next hop page:

    1. 选择要从其验证路由的源 VM 的订阅和资源组。 Choose your Subscription and the Resource group of the source VM you want to validate routing from.

    2. 选择“虚拟机”,以及附加到 VM 的“网络接口” 。Choose the Virtual machine and the Network interface that's attached to the VM.

    3. 输入分配给要从其验证路由的网络接口的源 IP 地址。 Enter a Source IP address assigned to the network interface that you want to validate routing from.

    4. 输入要验证的、所要路由到的目标 IP 地址。 Enter a Destination IP address that you want to validate routing to.

  4. 选择“下一跃点”。 Select Next hop .

片刻之后,Azure 即会告知路由了流量的路由的下一跃点类型和 ID。After a short wait, Azure tells you the next hop type and the ID of the route that routed the traffic. 虚拟网络流量路由中详细了解返回的下一跃点类型。Learn more about next hop types that you see returned in Virtual network traffic routing.

验证两个终结点之间的路由 - 命令Validate routing between two endpoints - commands

工具Tool 命令Command
Azure CLIAzure CLI az network watcher show-next-hopaz network watcher show-next-hop
PowerShellPowerShell Get-AzNetworkWatcherNextHopGet-AzNetworkWatcherNextHop

权限Permissions

若要针对路由表和路由执行任务,必须将你的帐户分配到网络参与者角色或分配有下表中所列的相应操作的自定义角色To do tasks on route tables and routes, your account must be assigned to the Network contributor role or to a Custom role that's assigned the appropriate actions listed in the following table:

操作Action 名称Name
Microsoft.Network/routeTables/readMicrosoft.Network/routeTables/read 读取路由表Read a route table
Microsoft.Network/routeTables/writeMicrosoft.Network/routeTables/write 创建或更新路由表Create or update a route table
Microsoft.Network/routeTables/deleteMicrosoft.Network/routeTables/delete 删除路由表Delete a route table
Microsoft.Network/routeTables/join/actionMicrosoft.Network/routeTables/join/action 将路由表关联到子网Associate a route table to a subnet
Microsoft.Network/routeTables/routes/readMicrosoft.Network/routeTables/routes/read 读取路由Read a route
Microsoft.Network/routeTables/routes/writeMicrosoft.Network/routeTables/routes/write 创建或更新路由Create or update a route
Microsoft.Network/routeTables/routes/deleteMicrosoft.Network/routeTables/routes/delete 删除路由Delete a route
Microsoft.Network/networkInterfaces/effectiveRouteTable/actionMicrosoft.Network/networkInterfaces/effectiveRouteTable/action 为网络接口获取有效路由表Get the effective route table for a network interface
Microsoft.Network/networkWatchers/nextHop/actionMicrosoft.Network/networkWatchers/nextHop/action 从 VM 获取下一跃点Gets the next hop from a VM

后续步骤Next steps