将定义池中的IP地址分配给P2S VPN用户 - Azure CLI

通过创建策略组(用户组),可以根据用户的身份或身份验证凭据,将连接到点对站点(P2S)VPN 网关的用户分配到特定地址池中的 IP 地址。 本文帮助你使用 Azure CLI 配置策略组、组成员和优先级组。 有关使用策略组和组成员进行 P2S 连接的详细信息,包括注意事项和限制,请参阅 “关于用户组”。

Prerequisites

本文假定你具有 VPN 网关 P2S 配置。 如果尚未设置 P2S VPN 网关,请参阅以下文章:

  • 如需在本地运行 CLI 参考命令,请安装 Azure CLI。 如果在 Windows 或 macOS 上运行,请考虑在 Docker 容器中运行 Azure CLI。 有关详细信息,请参阅如何在 Docker 容器中运行 Azure CLI。

    • 如果使用本地安装,请使用 az login 命令登录到Azure CLI。 若要完成身份验证过程,请遵循终端中显示的步骤。 有关其他登录选项,请参阅使用 Azure CLI 登录。

    • 出现提示时,请在首次使用时安装 Azure CLI 扩展。 有关扩展的详细信息,请参阅 将扩展与 Azure CLI 配合使用。

    • 运行az version命令,以查看已安装的版本和依赖库。 若要升级到最新版本,请运行az upgrade。

Workflow

本文使用以下工作流来帮助为 P2S VPN 网关连接设置用户组和 IP 地址池。

  1. 考虑配置要求。
  2. 选择身份验证机制。
  3. 创建用户组。
  4. 配置网关设置。

考虑配置要求

本部分列出了用户组和 IP 地址池的配置要求和限制。

  • 最大组数:单个 P2S VPN 网关最多可以引用 90 个组。

  • 最大成员数:分配给网关的所有组中,策略/组成员的总数为390。

  • 多次分配:如果将一个组分配给同一网关上的多个连接配置,则该组及其成员将被多次计数。 示例:如果将一个具有 10 个成员的策略组分配到三个 VPN 连接配置,则应计为共有 30 个成员的三个组,而不是一个具有 10 个成员的组。

  • 并发用户:并发用户总数由网关的缩放单元和分配给每个用户组的 IP 地址数决定。 而不是由与网关关联的策略/组成员数确定。

  • 将某个组创建为 VPN 服务器配置的一部分后,无法修改该组的名称和默认设置。

  • 组名称应不同。

  • 数字优先级较低的组在数字优先级较高的组之前进行处理。 如果连接用户是多个组的成员,则网关在分配 IP 地址时,将其视为数字优先级较低的组的成员。

  • 无法删除现有点到站点 VPN 网关正在使用的组。

  • 可以通过单击与该组对应的上下箭头按钮来重新排列组的优先级。

  • 地址池不能与其他连接配置中使用的地址池重叠(相同或不同的网关)。
  • 地址池也不能与虚拟网络地址空间、虚拟中心地址空间或本地地址重叠。
  • 地址池的大小不能小于 /24。 例如,不能分配 /25 或 /26 的范围。

选择身份验证机制

以下章节列出了创建用户组时可用的认证机制。

Microsoft Entra 组

若要创建和管理Microsoft Entra 组,请参阅 “管理Microsoft Entra 组和组成员身份。

  • 在点到站点配置中指定 Microsoft Entra 用户组对象 ID(而非组名)。

    • 针对此具体情况,请使用:

      • 工程组 aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb
      • 金融集团 bbbbbbbb-1111-2222-3333-cccccccccccc

      Microsoft Entra ID组和对象 ID 的关系图。

  • 你可以将 Microsoft Entra 用户分配到多个 Active Directory 组中,VPN 网关 将用户视为 VPN 用户或策略的一部分。 如果用户属于多个组,则在点到站点连接中选择具有最低数值优先级的组。

RADIUS - NPS 供应商特定的属性

有关网络策略服务器(NPS)供应商特定属性的配置信息,请参阅 RADIUS - 为供应商特定的属性配置 NPS。

证书

若要生成自签名证书,请参阅 使用 PowerShell 为点到站点生成和导出证书。 要生成具有特定通用名称的证书,运行 New-SelfSignedCertificate PowerShell 命令时,将xx@domain.com参数更改为相应值(例如 )。 例如,可以使用以下 主题生成证书:

数字证书字段 Value Description
主题 CN= cert@marketing.contoso.com 营销部门的数字证书
主题 CN= cert@sale.contoso.com 销售部门的数字证书

具有数字证书身份验证的多个地址池功能适用于基于 “使用者” 字段的特定用户组。 选择条件不适用于主题备用名称(SAN)证书。

如果要在其证书中指定 SAN,则必须与主题相同,以便多池功能能够正常工作。 主题与 SAN 之间的差异会导致问题。

创建用户组

1.声明变量

rg="p2s-multipool"
location="chinanorth3"
vnetName="VNet1"
vNetAddressPrefix="10.1.0.0/16"
gatewaySubnetPrefix="10.1.0.0/24"
gatewayName="gw"

# Name of the first public IP address of the gateway
gwpip="${gatewayName}-pip"

# Local folder that holds the exported root certificate (P2SRoot.cer).
# The gateway-create command references the .cer file by this path.
pathFiles="$(pwd)"
certPath="$pathFiles/cert"

2. 连接你的账户并创建虚拟网络

打开你的Bash控制台,连接你的账户。 欲了解更多信息,请参见“使用Azure CLI管理Azure资源。 使用下面的示例来帮助您连接:

az account set --subscription <YourSubscriptionName>
az account show

创建资源组、虚拟网络、网关子网以及网关的公共IP地址。

# create a Resource Group
az group create --name "$rg" --location "$location"

# create an Azure VNet
az network vnet create \
  --name "$vnetName" \
  --resource-group "$rg" \
  --location "$location" \
  --address-prefixes "$vNetAddressPrefix"

# create the GatewaySubnet
az network vnet subnet create \
  --name "GatewaySubnet" \
  --vnet-name "$vnetName" \
  --resource-group "$rg" \
  --address-prefixes "$gatewaySubnetPrefix"

# create the first public IP of the VPN Gateway
az network public-ip create \
  --name "$gwpip" \
  --resource-group "$rg" \
  --location "$location" \
  --allocation-method Static \
  --sku Standard \
  --tier Regional \
  --zone 1 2 3

3. 规划你的用户组

在这种情况下,考虑四个用户组:两个使用数字证书认证的用户组和两个使用Microsoft Entra ID认证的用户组。

  • 两个组使用带有通用名称的客户端证书。

    • yourServiceName@marketing.contoso.com
    • yourServiceName@sale.contoso.com
  • 在 Microsoft Entra ID 中创建的两个组。 以下对象ID是示例;用你实际的 Microsoft Entra 组对象 ID 替换它们。

    • 工程组 aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb
    • 金融 bbbbbbbb-1111-2222-3333-cccccccccccc

4. 创建VPN网关并添加P2S配置

以下配置将使VPN网关使用客户端地址池和认证选项:

  • 基于证书的身份验证: 证书
  • Microsoft Entra ID: AAD

有关 P2S Microsoft Entra ID 身份验证值的详细信息,请参阅 配置 P2S VPN 网关以Microsoft Entra ID 身份验证。

导出你根证书的公开数据,格式为.cer,并保存为 P2SRoot.cer ,存放在 cert 变量引用的 $certPath 文件夹里。 .cer文件只保存公证书,不包含私钥。 在以下命令的参数中,按其完整路径 --root-cert-data 引用该文件。

接下来,创建虚拟网络网关。 在以下命令中,替换以下值:

  • 将 --aad-tenant 和 --aad-issuer 中的租户 ID(示例中使用 aaaabbbb-0000-cccc-1111-dddd2222eeee)替换为你的 Microsoft Entra 租户 ID。
  • 将 --aad-audience 设置为 Microsoft 注册的 Azure VPN 客户端应用 ID 的相应值。 还可以在此字段中使用 自定义受众 。
  • 将 192.168.0.0/24 与地址池一起使用,以连接不属于多个地址池配置的 VPN 客户端。
# Create the VPN Gateway with VpnGw2AZ SKU.
# $certPath/P2SRoot.cer is the local root certificate file to upload to the gateway.
# The .cer file holds only the public certificate, not the private key.
az network vnet-gateway create \
  --name "$gatewayName" \
  --resource-group "$rg" \
  --location "$location" \
  --vnet "$vnetName" \
  --gateway-type Vpn \
  --vpn-type RouteBased \
  --sku VpnGw2AZ \
  --public-ip-addresses "$gwpip" \
  --address-prefixes "192.168.0.0/24" \
  --client-protocol IkeV2 OpenVPN \
  --vpn-auth-type Certificate AAD \
  --root-cert-name "P2SRootcert" \
  --root-cert-data "$certPath/P2SRoot.cer" \
  --aad-tenant "https://login.chinacloudapi.cn/aaaabbbb-0000-cccc-1111-dddd2222eeee" \
  --aad-issuer "https://sts.chinacloudapi.cn/aaaabbbb-0000-cccc-1111-dddd2222eeee/" \
  --aad-audience "c632b3df-fb67-4d84-bdcf-b95ad541b5c8"

创建 VPN 网关可能需要大约 45 分钟或更多时间。

5. 验证VPN网关

# shows the gateway configuration, including the P2S client address pool and the root certificate.
az network vnet-gateway show \
  --name "$gatewayName" \
  --resource-group "$rg"

6. 创建政策小组和成员

使用 Azure CLI,将每个策略组及其成员定义为 JSON 对象。 为四个组中的每个组配置一个成员:

  • member1 和 member2 使用数字证书身份验证(身份验证类型:CertificateGroupId)。

  • member3和member4使用Microsoft Entra ID身份验证(身份验证类型:AADGroupId)。

    • 示例对象 ID aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb 和 bbbbbbbb-1111-2222-3333-cccccccccccc 代表了你 Microsoft Entra ID 租户中的两个不同的组 ID。 用你实际的组对象ID替换它们。
成员名称 身份验证类型 属性值
成员1 证书组ID marketing.contoso.com
成员2 证书组ID sale.contoso.com
member3 AADGroupId aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb
member4 AADGroupId bbbbbbbb-1111-2222-3333-cccccccccccc

创建四个策略组,采用下表中描述的默认策略和优先级:

策略组名称 默认策略 Priority
policyGroup1 True 0
policyGroup2 false 1
policyGroup3 false 2
policyGroup4 false 3
# policyGroup1 - default group, priority 0, member1 (CertificateGroupId)
policyGroup1='{
  "name": "policyGroup1",
  "properties": {
    "isDefault": true,
    "priority": 0,
    "policyMembers": [
      {
        "name": "member1",
        "attributeType": "CertificateGroupId",
        "attributeValue": "marketing.contoso.com"
      }
    ]
  }
}'

# policyGroup2 - priority 1, member2 (CertificateGroupId)
policyGroup2='{
  "name": "policyGroup2",
  "properties": {
    "isDefault": false,
    "priority": 1,
    "policyMembers": [
      {
        "name": "member2",
        "attributeType": "CertificateGroupId",
        "attributeValue": "sale.contoso.com"
      }
    ]
  }
}'

# policyGroup3 - priority 2, member3 (AADGroupId)
policyGroup3='{
  "name": "policyGroup3",
  "properties": {
    "isDefault": false,
    "priority": 2,
    "policyMembers": [
      {
        "name": "member3",
        "attributeType": "AADGroupId",
        "attributeValue": "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb"
      }
    ]
  }
}'

# policyGroup4 - priority 3, member4 (AADGroupId)
policyGroup4='{
  "name": "policyGroup4",
  "properties": {
    "isDefault": false,
    "priority": 3,
    "policyMembers": [
      {
        "name": "member4",
        "attributeType": "AADGroupId",
        "attributeValue": "bbbbbbbb-1111-2222-3333-cccccccccccc"
      }
    ]
  }
}'

注释

显示的 Microsoft Entra 组对象 ID(例如 aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb)是示例值。 用你 Microsoft Entra ID 租户中的实际对象 ID 替换它们。

7. 向VPN网关添加策略组

以下配置将四个网关策略组添加到VPN网关中。 这些组通过以下方式授权连接:使用在公用名称 (CN) 字段中包含特定值的数字证书,以及使用指定组对象 ID 的 Microsoft Entra ID 组:

  • 政策组1:marketing.contoso.com,成员1
  • 政策组2:sale.contoso.com,成员2
  • policyGroup3: aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb, 成员3
  • policyGroup4: bbbbbbbb-1111-2222-3333-cccccccccccc, 成员4
# Repeat the --add flag, one per policy group.
az network vnet-gateway update \
  --name "$gatewayName" \
  --resource-group "$rg" \
  --add virtualNetworkGatewayPolicyGroups "$policyGroup1" \
  --add virtualNetworkGatewayPolicyGroups "$policyGroup2" \
  --add virtualNetworkGatewayPolicyGroups "$policyGroup3" \
  --add virtualNetworkGatewayPolicyGroups "$policyGroup4"

显示政策组名称列表:

# List the current policy groups in order.
# index order: the first entry is index 0, the second is index 1, and so on.
az network vnet-gateway show \
  --name "$gatewayName" \
  --resource-group "$rg" \
  --query "virtualNetworkGatewayPolicyGroups[].name" \
  --output tsv

可视化特定政策组的所有属性:

# Show the properties of a specific group, for example policyGroup2:
az network vnet-gateway show \
  --name "$gatewayName" \
  --resource-group "$rg" \
  --query "virtualNetworkGatewayPolicyGroups[?name=='policyGroup2']" \
  --output json

8. 创建VPN客户端连接配置

VPN 配置定义四个不同的地址池,每个池都链接到特定的策略组。

VPN客户端配置名称 地址池 PolicyGroup
config1 192.168.1.0/24 policyGroup1:marketing.contoso.com
config2 192.168.2.0/24 policyGroup2:sale.contoso.com
config3 192.168.3.0/24 政策组3:工程 aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb
config4 192.168.4.0/24 政策组4:财务 bbbbbbbb-1111-2222-3333-cccccccccccc

注释

示例池(192.168.1.0/24、 192.168.2.0/24 等)使用文档安全的范围。 用适合你环境的真实地址范围替换它们。 每个池至少是 /24,不能与其他池或默认网关地址池重叠,也不能与你的虚拟网络、虚拟枢纽或本地地址空间重叠。

首先,获取每个策略组的ID:

# Create per-policy-group P2S client connection configurations with dedicated pools.
policyGroup1Id=$(az network vnet-gateway show -g "$rg" -n "$gatewayName" --query "virtualNetworkGatewayPolicyGroups[?name=='policyGroup1'].id | [0]" -o tsv)

policyGroup2Id=$(az network vnet-gateway show -g "$rg" -n "$gatewayName" --query "virtualNetworkGatewayPolicyGroups[?name=='policyGroup2'].id | [0]" -o tsv)

policyGroup3Id=$(az network vnet-gateway show -g "$rg" -n "$gatewayName" --query "virtualNetworkGatewayPolicyGroups[?name=='policyGroup3'].id | [0]" -o tsv)

policyGroup4Id=$(az network vnet-gateway show -g "$rg" -n "$gatewayName" --query "virtualNetworkGatewayPolicyGroups[?name=='policyGroup4'].id | [0]" -o tsv)

接着,将 vngClientConnConfig1VPN 客户端配置以 JSON 格式加载到变量 、 vngClientConnConfig2、 vngClientConnConfig3和 vngClientConnConfig4:

read -r -d '' vngClientConnConfig1 <<EOF
{
  "name": "config1",
  "properties": {
    "virtualNetworkGatewayPolicyGroups": [
      { "id": "$policyGroup1Id" }
    ],
    "vpnClientAddressPool": {
      "addressPrefixes": ["192.168.1.0/24"]
    }
  }
}
EOF

read -r -d '' vngClientConnConfig2 <<EOF
{
  "name": "config2",
  "properties": {
    "virtualNetworkGatewayPolicyGroups": [
      { "id": "$policyGroup2Id" }
    ],
    "vpnClientAddressPool": {
      "addressPrefixes": ["192.168.2.0/24"]
    }
  }
}
EOF

read -r -d '' vngClientConnConfig3 <<EOF
{
  "name": "config3",
  "properties": {
    "virtualNetworkGatewayPolicyGroups": [
      { "id": "$policyGroup3Id" }
    ],
    "vpnClientAddressPool": {
      "addressPrefixes": ["192.168.3.0/24"]
    }
  }
}
EOF

read -r -d '' vngClientConnConfig4 <<EOF
{
  "name": "config4",
  "properties": {
    "virtualNetworkGatewayPolicyGroups": [
      { "id": "$policyGroup4Id" }
    ],
    "vpnClientAddressPool": {
      "addressPrefixes": ["192.168.4.0/24"]
    }
  }
}
EOF

9. 将配置应用到 VPN 网关

az network vnet-gateway update \
  --name "$gatewayName" \
  --resource-group "$rg" \
  --add vpnClientConfiguration.vngClientConnectionConfigurations "$vngClientConnConfig1" \
  --add vpnClientConfiguration.vngClientConnectionConfigurations "$vngClientConnConfig2" \
  --add vpnClientConfiguration.vngClientConnectionConfigurations "$vngClientConnConfig3" \
  --add vpnClientConfiguration.vngClientConnectionConfigurations "$vngClientConnConfig4"

按索引顺序列出VPN客户端连接配置。

# list of vpn client connection configs (index order)
az network vnet-gateway show \
  --name "$gatewayName" \
  --resource-group "$rg" \
  --query "vpnClientConfiguration.vngClientConnectionConfigurations[].name" -o tsv

在多个地址池设置结束时,可以通过以下逻辑结构来表示配置:

策略组名称 默认策略 Priority 身份验证类型 组配置值 VPN 客户端地址池
policyGroup1 True 0 证书组ID marketing.contoso.com 192.168.1.0/24
policyGroup2 false 1 证书组ID sale.contoso.com 192.168.2.0/24
policyGroup3 false 2 AADGroupId aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb 192.168.3.0/24
policyGroup4 false 3 AADGroupId bbbbbbbb-1111-2222-3333-cccccccccccc 192.168.4.0/24

查看地址池配置

请使用以下命令查看与你的策略组关联的地址池。

# Show the address pool associated with a policy group
az network vnet-gateway show \
  --name "$gatewayName" \
  --resource-group "$rg" \
  --query "vpnClientConfiguration.vngClientConnectionConfigurations[?name=='config1'].vpnClientAddressPool.addressPrefixes[]" \
  --output tsv

# Show the config name of the address pool configuration and the associated address pool(s)
az network vnet-gateway show \
  --name "$gatewayName" \
  --resource-group "$rg" \
  --query "vpnClientConfiguration.vngClientConnectionConfigurations[].{config:name,addressPools:join(', ', vpnClientAddressPool.addressPrefixes)}" \
  --output table

你也可以同时显示策略组名称、配置名称和关联地址池:

# show policy group name, config name, and associated address pool(s)
az network vnet-gateway show -n "$gatewayName" -g "$rg" -o json |
jq -r '.vpnClientConfiguration.vngClientConnectionConfigurations[]? |
  [(.virtualNetworkGatewayPolicyGroups[0].id // "" | split("/")[-1]),
   (.name // ""),
   (.vpnClientAddressPool.addressPrefixes // [] | join(", "))] | @tsv' |
column -t -s $'\t'

移除地址池配置

要移除特定的地址池配置,可以在 Azure CLI 中通过--remove其零基索引移除列表元素。 先检查排序列表,然后移除匹配位置。

# Remove an address pool configuration
# Az CLI removes a list element by its zero-based INDEX with --remove.
# You check the ordered list first, then remove the matching position (index 3 = the 4th group, policyGroup4).
az network vnet-gateway update \
  --name "$gatewayName" \
  --resource-group "$rg" \
  --remove vpnClientConfiguration.vngClientConnectionConfigurations 3

# Remove the policyGroup4
az network vnet-gateway update \
  --name "$gatewayName" \
  --resource-group "$rg" \
  --remove virtualNetworkGatewayPolicyGroups 3

要删除所有策略组和地址池配置:

# Removing all policy groups and address pools
az network vnet-gateway update \
  --name "$gatewayName" \
  --resource-group "$rg" \
  --set vpnClientConfiguration.vngClientConnectionConfigurations=[] \
  --set virtualNetworkGatewayPolicyGroups=[]

如果出现问题

以下是为 P2S 连接配置策略组和 IP 地址池时可能会遇到的一些常见问题,以及帮助你解决这些问题的故障排除步骤。

  • 验证数据包是否具有正确的属性? Wireshark 或其他数据包捕获可以在 NPS 模式下运行,并使用共享密钥解密数据包。 可以验证数据包是否正从 RADIUS 服务器发送到配置了正确的 RADIUS VSA 的点到站点 VPN 网关。

  • 是否为用户分配了错误的 IP? 设置并检查 NPS 事件日志记录以进行身份验证,无论用户是否匹配策略。

  • 遇到地址池问题? 每个地址池都在网关上指定。 地址池拆分为两个地址池,并分配给点到站点 VPN 网关对中的每个主动-主动实例。 这些拆分地址应显示在有效的路由表中。 例如,如果指定 10.0.0.0/24,则应在有效路由表中看到两个“/25”路由。 如果不是这种情况,请尝试更改网关上定义的地址池。

  • P2S 客户端无法接收路由? 确保所有点到站点 VPN 连接配置都关联到 defaultRouteTable,并传播到同一组路由表。 如果使用门户,则应自动配置此项,但如果使用的是 REST、PowerShell 或 CLI,请确保正确设置所有传播和关联。

  • 您无法使用 Azure VPN 客户端启用多池吗? 如果使用 Azure VPN 客户端,请确保安装在用户设备上的 Azure VPN 客户端是最新版本。 需要再次下载客户端才能启用此功能。

  • 所有用户都被分配到默认组了吗? 如果使用Microsoft Entra身份验证,请确保服务器配置中的租户 URL 输入(https://login.chinacloudapi.cn/<tenant ID>)不会以 \ 结尾。 如果 URL 的输入以\结尾,网关将无法正确处理 Microsoft Entra 用户组,并且所有用户都将被分配到默认组。 若要修正,请修改服务器配置以删除尾随 \ 并修改网关上配置的地址池,以将更改应用到网关。 这是已知问题。

  • 尝试邀请外部用户使用 Multipool 功能? 如果使用Microsoft Entra身份验证,并且计划邀请外部用户(不属于 VPN 网关上配置的Microsoft Entra域的用户)连接到 VPN 网关,请确保外部用户的用户类型为 Member 而不是 Guest。 此外,请确保将用户的“名称”设置为用户的电子邮件地址。 如果未正确设置连接用户的用户类型和名称,或者无法将外部成员设置为 Microsoft Entra 域的“成员”,则连接用户将分配到默认组,并从默认 IP 地址池分配 IP。

后续步骤