快速入門:使用 Azure CLI 診斷虛擬機網路流量篩選問題

在本快速入門中,您會部署虛擬機,並使用 網路監看員IP流程驗證來測試不同IP位址的連線能力。 使用IP流程驗證結果,您可以判斷封鎖流量並導致通訊失敗的安全性規則,並瞭解如何加以解決。 您也會瞭解如何使用 網路介面的有效安全性規則 來判斷安全性規則允許或拒絕流量的原因。

圖表顯示 網路監看員 快速入門中建立的資源。

如果您沒有 Azure 訂用帳戶,請在開始前建立免費帳戶

必要條件

  • 具有有效訂用帳戶的 Azure 帳戶。

  • Azure Cloud Shell 或 Azure CLI。

    本文中的步驟會在 Azure Cloud Shell以互動方式執行 Azure CLI 命令。 若要在 Cloud Shell 中執行命令,請選取程式代碼區塊右上角的 [ 開啟 Cloud Shell ]。 選取 [複製] 以複製程式碼,並將它貼到 Cloud Shell 中以執行。 您也可以從 Azure 入口網站內執行 Cloud Shell。

    您也可以 在本機 安裝 Azure CLI 來執行命令。 本快速入門需要 Azure CLI 2.0 版或更新版本。 如果您在本機執行 Azure CLI,請使用 az login 命令登入 Azure。

建立虛擬機器

在本節中,您會在美國東部區域建立虛擬網路和子網。 然後,您會在子網中建立具有默認網路安全組的虛擬機。

  1. 使用 az group create 建立資源群組。 Azure 資源群組是在其中部署與管理 Azure 資源的邏輯容器。

    # Create a resource group.
    az group create --name 'myResourceGroup' --location 'eastus'
    
  2. 使用 az network vnet create 來建立虛擬網路。

    # Create a virtual network and a subnet.
    az network vnet create --resource-group 'myResourceGroup' --name 'myVNet' --subnet-name 'mySubnet' --subnet-prefixes 10.0.0.0/24 
    
  3. 使用 az network nsg create 建立預設網路安全組。

    # Create a default network security group.
    az network nsg create --name 'myVM-nsg' --resource-group 'myResourceGroup' --location 'eastus'
    
  4. 使用 az vm create 建立虛擬機。 出現提示時,請輸入使用者名稱和密碼。

    # Create a Linux virtual machine using the latest Ubuntu 20.04 LTS image.
    az vm create --resource-group 'myResourceGroup' --name 'myVM' --location 'eastus' --vnet-name 'myVNet' --subnet 'mySubnet' --public-ip-address '' --nsg 'myVM-nsg' --image 'Canonical:0001-com-ubuntu-server-focal:20_04-lts-gen2:latest'
    

使用IP流程驗證測試網路通訊

在本節中,您會使用 網路監看員 的IP流程驗證功能來測試虛擬機的網路通訊。

  1. 使用 az network watcher test-ip-flow 命令,使用 IP 流量驗證來測試從 myVM13.107.21.200 的輸出通訊(13.107.21.200 是 所使用的 www.bing.com其中一個公用 IP 位址):

    # Start the IP flow verify session to test outbound flow to www.bing.com.
    az network watcher test-ip-flow --direction 'outbound' --protocol 'TCP' --local '10.0.0.4:60000' --remote '13.107.21.200:80' --vm 'myVM' --nic 'myVmVMNic' --resource-group 'myResourceGroup' --out 'table'
    

    幾秒鐘后,您會收到類似下列範例的輸出:

    Access  RuleName
    ------  --------
    Allow   defaultSecurityRules/AllowInternetOutBound
    

    測試結果表示允許 存取 13.107.21.200 ,因為預設安全性規則 AllowInternetOutBound。 根據預設,Azure 虛擬機可以存取因特網。

  2. 將 RemoteIPAddress 變更10.0.1.10 並重複測試。 10.0.1.10 是 myVNet 位址空間中的私人IP位址。

    # Start the IP flow verify session to test outbound flow to 10.0.1.10.
    az network watcher test-ip-flow --direction 'outbound' --protocol 'TCP' --local '10.0.0.4:60000' --remote '10.0.1.10:80' --vm 'myVM' --nic 'myVmVMNic' --resource-group 'myResourceGroup' --out 'table'
    

    幾秒鐘后,您會收到類似下列範例的輸出:

    Access RuleName
    ------ --------
    Allow  defaultSecurityRules/AllowVnetOutBound
    

    第二個測試的結果指出,由於預設安全性規則 AllowVnetOutBound,允許存取 10.0.1.10 根據預設,Azure 虛擬機可以存取其虛擬網路位址空間中的所有IP位址。

  3. 將 RemoteIPAddress 變更10.10.10.10 並重複測試。 10.10.10.10 是不在 myVNet 位址空間中的私人IP位址。

    # Start the IP flow verify session to test outbound flow to 10.10.10.10.
    az network watcher test-ip-flow --direction 'outbound' --protocol 'TCP' --local '10.0.0.4:60000' --remote '10.10.10.10:80' --vm 'myVM' --nic 'myVmVMNic' --resource-group 'myResourceGroup' --out 'table'
    

    幾秒鐘后,您會收到類似下列範例的輸出:

    Access RuleName
    ------ --------
    Allow  defaultSecurityRules/DenyAllOutBound
    

    第三項測試的結果表示存取權因為預設安全性規則 DenyAllOutBound遭到拒絕為 10.10.10.10

  4. 將方向變更為輸入、將本機埠變更80,並將遠端埠變更為60000,然後重複測試。

    # Start the IP flow verify session to test inbound flow from 10.10.10.10.
    az network watcher test-ip-flow --direction 'inbound' --protocol 'TCP' --local '10.0.0.4:80' --remote '10.10.10.10:6000' --vm 'myVM' --nic 'myVmVMNic' --resource-group 'myResourceGroup' --out 'table'
    

    幾秒鐘之後,您會收到類似下列範例的輸出:

    Access RuleName
    ------ --------
    Allow  defaultSecurityRules/DenyAllInBound
    

    第四項測試的結果指出,由於預設安全性規則 DenyAllInBound,存取權從 10.10.10.10 拒絕。 根據預設,會拒絕從虛擬網路外部存取 Azure 虛擬機的所有存取。

檢視安全性規則的詳細資料

若要判斷上一節中的規則為何允許或拒絕通訊,請使用 az network nic list-effective-nsg 命令,檢閱 myVM 虛擬機網路介面的有效安全性規則:

# Get the effective security rules for the network interface of myVM.
az network nic list-effective-nsg --resource-group 'myResourceGroup' --name 'myVmVMNic'

傳回的輸出包含下列允許輸出存取www.bing.com的 AllowInternetOutbound 規則資訊

{
  "access": "Allow",
  "destinationAddressPrefix": "Internet",
  "destinationAddressPrefixes": [
	"Internet"
  ],
  "destinationPortRange": "0-65535",
  "destinationPortRanges": [
	"0-65535"
  ],
  "direction": "Outbound",
  "expandedDestinationAddressPrefix": [
	"1.0.0.0/8",
	"2.0.0.0/7",
	"4.0.0.0/9",
	"4.144.0.0/12",
	"4.160.0.0/11",
	"4.192.0.0/10",
	"5.0.0.0/8",
	"6.0.0.0/7",
	"8.0.0.0/7",
	"11.0.0.0/8",
	"12.0.0.0/8",
	"13.0.0.0/10",
	"13.64.0.0/11",
	"13.104.0.0/13",
	"13.112.0.0/12",
	"13.128.0.0/9",
	"14.0.0.0/7",
	...
	...
	...
	"200.0.0.0/5",
	"208.0.0.0/4"
  ],
  "name": "defaultSecurityRules/AllowInternetOutBound",
  "priority": 65001,
  "protocol": "All",
  "sourceAddressPrefix": "0.0.0.0/0",
  "sourceAddressPrefixes": [
	"0.0.0.0/0",
	"0.0.0.0/0"
  ],
  "sourcePortRange": "0-65535",
  "sourcePortRanges": [
	"0-65535"
  ]
},

您可以在輸出中看到位址前綴 13.104.0.0/13 是 AllowInternetOutBound 規則的地址前綴之一。 此前置詞包含IP位址 13.107.21.200,您用來測試對的 www.bing.com輸出通訊。

同樣地,您可以檢查其他規則,以查看每個規則下的來源和目的地 IP 位址前置。

清除資源

若不再需要,請使用 az group delete 刪除 myResourceGroup 資源群組及其包含的所有資源:

# Delete the resource group and all resources it contains.
az group delete --name 'myResourceGroup' --yes

下一步

在本快速入門中,您已建立 VM 並診斷輸入和輸出網路流量篩選器。 您已了解網路安全組規則允許或拒絕來自 VM 的流量。 深入瞭解 安全性規則 ,以及如何 建立安全性規則

即使已就地使用適當的網路流量篩選,由於路由設定,與虛擬機的通訊仍會失敗。 若要瞭解如何診斷虛擬機路由問題,請參閱 診斷虛擬機網路路由問題。 若要使用一個工具來診斷輸出路由、延遲和流量篩選問題,請參閱針對 Azure 網路監看員 的連線進行疑難解答。