共用方式為


快速入門:使用 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 以執行命令。 本快速入門需要 2.0 版或更新版本的 Azure CLI。 如果您在本機執行 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.200www.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
    

    測試結果指出,由於預設安全性規則 AllowInternetOutBound,允許存取 13.107.21.200。 根據預設,Azure 虛擬機器可以存取網際網路。

  2. RemoteIPAddress 變更為 10.0.1.10 並重複測試。 10.0.1.10myVNet 位址空間中的私人 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.comAllowInternetOutbound 規則,傳回的輸出包含下列資訊:

{
  "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/13AllowInternetOutBound 規則的位址前置詞之一。 此前置詞包含 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 網路監看員針對連線進行疑難排解