在本快速入門中,您會部署虛擬機器,並使用網路監看員 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。
建立虛擬機器
在本節中,您會在美國東部區域建立虛擬網路和子網路。 然後,您會在具有預設網路安全性群組的子網路中建立虛擬機器。
使用 az group create 建立資源群組。 Azure 資源群組是一種邏輯容器,您可在其中部署和管理 Azure 資源。
# Create a resource group. az group create --name 'myResourceGroup' --location 'eastus'使用 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使用 az network nsg create 建立預設的網路安全群組。
# Create a default network security group. az network nsg create --name 'myVM-nsg' --resource-group 'myResourceGroup' --location 'eastus'備註
網路安全群組的預設規則會阻擋所有來自網際網路的入站存取,包括 SSH。 要連接虛擬機,請使用 Azure Bastion。 更多資訊請參見 快速入門:部署 Azure Bastion 使用預設設定。
使用 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' --generate-ssh-keys
透過使用 IP 流驗證測試網路通訊
在本節中,請使用 網路監看員 的 IP flow verify 功能來測試虛擬機之間的網路通訊。
使用 az network watcher test-ip-flow 指令,透過 IP flow verify 測試從 myVM 到
13.107.21.200的外發通訊(這是其中一個公共 IP 位址):www.bing.com# 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 虛擬機器可以存取網際網路。
將 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 位址。
將 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 ------ -------- Deny defaultSecurityRules/DenyAllOutBound第三個測試的結果指出,由於預設安全性規則 DenyAllOutBound,拒絕存取 10.10.10.10。
將 [方向] 變更為 [輸入]、[本機連接埠] 變更為 [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 ------ -------- Deny 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'
回傳的輸出包含以下關於 AllowInternetOutbound 規則的資訊,該規則允許外撥存取權限:www.bing.com
{
"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