在本快速入門中,您會部署虛擬機器,並使用網路監看員 IP 流程驗證,來測試往返不同 IP 位址的連線能力。 使用 IP 流量驗證結果,您可判斷封鎖流量並導致通訊失敗的安全性規則,並了解如何將其解決。 您也會了解如何針對網路介面使用有效的安全性規則,以判斷安全性規則為何允許或拒絕流量的原因。
如果您沒有 Azure 訂用帳戶,請在開始前建立免費帳戶。
必要條件
具有有效訂用帳戶的 Azure 帳戶。
Azure Cloud Shell 或 Azure PowerShell。
本文中的步驟會在 Azure Cloud Shell 中以互動方式執行 Azure PowerShell Cmdlet。 若要在 Cloud Shell 中執行命令,請選取程式碼區塊右上角的 [開啟 Cloud Shell]。 選取 [複製] 以複製程式碼,然後將其貼入 Cloud Shell 以執行。 您也可以從 Azure 入口網站內執行 Cloud Shell。
您也可以在本機安裝 Azure PowerShell 來執行 Cmdlet。 本快速入門需要 Az PowerShell 模組。 如需詳細資訊,請參閱安裝 Azure PowerShell (部分機器翻譯)。 若要尋找已安裝的版本,請執行
Get-InstalledModule -Name Az
。 如果您在本機執行 PowerShell,請使用 Connect-AzAccount Cmdlet 登入 Azure。
建立虛擬機器
在本節中,您會在美國東部區域建立虛擬網路和子網路。 然後,您會在具有預設網路安全性群組的子網路中建立虛擬機器。
使用 New-AzResourceGroup 來建立資源群組。 Azure 資源群組是在其中部署與管理 Azure 資源的邏輯容器。
# Create a resource group. New-AzResourceGroup -Name 'myResourceGroup' -Location 'eastus'
使用 New-AzVirtualNetworkSubnetConfig 建立虛擬機器子網路和堡壘主機子網路的子網路設定。
# Create subnets configuration. $Subnet = New-AzVirtualNetworkSubnetConfig -Name 'mySubnet' -AddressPrefix '10.0.0.0/24'
使用 New-AzVirtualNetwork 建立虛擬網路。
# Create a virtual network. New-AzVirtualNetwork -Name 'myVNet' -ResourceGroupName 'myResourceGroup' -Location 'eastus' -AddressPrefix '10.0.0.0/16' -Subnet $Subnet
使用 New-AzNetworkSecurityGroup 建立預設網路安全性群組。
# Create a network security group. New-AzNetworkSecurityGroup -Name 'myVM-nsg' -ResourceGroupName 'myResourceGroup' -Location 'eastus'
使用 New-AzVM 建立虛擬機器。 出現提示時,請輸入使用者名稱和密碼。
# Create a Linux virtual machine using the latest Ubuntu 20.04 LTS image. New-AzVm -ResourceGroupName 'myResourceGroup' -Name 'myVM' -Location 'eastus' -VirtualNetworkName 'myVNet' -SubnetName 'mySubnet' -SecurityGroupName 'myVM-nsg' -Image 'Canonical:0001-com-ubuntu-server-focal:20_04-lts-gen2:latest'
使用 IP 流量驗證來測試網路通訊
在本節中,您會使用網路監看員的 IP 流量驗證功能,來測試虛擬機器之間的網路通訊。
使用 Test-AzNetworkWatcherIPFlow,以利用 IP 流量驗證來測試從 myVM 到 13.107.21.200 的輸出通訊 (
13.107.21.200
是www.bing.com
使用的其中一個公用 IP 位址):# Place myVM configuration into a variable. $vm = Get-AzVM -ResourceGroupName 'myResourceGroup' -Name 'myVM' # Start the IP flow verify session to test outbound flow to www.bing.com. Test-AzNetworkWatcherIPFlow -Location 'eastus' -TargetVirtualMachineId $vm.Id -Direction 'Outbound' -Protocol 'TCP' -RemoteIPAddress '13.107.21.200' -RemotePort '80' -LocalIPAddress '10.0.0.4' -LocalPort '60000'
幾秒鐘之後,您會得到類似下列範例的輸出:
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. Test-AzNetworkWatcherIPFlow -Location 'eastus' -TargetVirtualMachineId $vm.Id -Direction 'Outbound' -Protocol 'TCP' -RemoteIPAddress '10.0.1.10' -RemotePort '80' -LocalIPAddress '10.0.0.4' -LocalPort '60000'
幾秒鐘之後,您會得到類似下列範例的輸出:
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. Test-AzNetworkWatcherIPFlow -Location 'eastus' -TargetVirtualMachineId $vm.Id -Direction 'Outbound' -Protocol 'TCP' -RemoteIPAddress '10.10.10.10' -RemotePort '80' -LocalIPAddress '10.0.0.4' -LocalPort '60000'
幾秒鐘之後,您會得到類似下列範例的輸出:
Access RuleName ------ -------- Allow defaultSecurityRules/DenyAllOutBound
第三個測試的結果指出,由於預設安全性規則 DenyAllOutBound,拒絕存取 10.10.10.10。
將 Direction 變更為 Inbound、將 LocalPort 變更為 80,並將 RemotePort 變更為 60000,然後重複測試。
# Start the IP flow verify session to test inbound flow from 10.10.10.10. Test-AzNetworkWatcherIPFlow -Location 'eastus' -TargetVirtualMachineId $vm.Id -Direction 'Inbound' -Protocol 'TCP' -RemoteIPAddress '10.10.10.10' -RemotePort '60000' -LocalIPAddress '10.0.0.4' -LocalPort '80'
幾秒鐘之後,您會得到類似下列範例的輸出:
Access RuleName ------ -------- Allow defaultSecurityRules/DenyAllInBound
第四個測試的結果指出,由於預設安全性規則 DenyAllInBound,拒絕從 10.10.10.10 存取。 根據預設,會拒絕從虛擬網路外部對 Azure 虛擬機器的所有存取。
檢視安全性規則的詳細資料
若要判斷為何上節中的規則允許或拒絕通訊,請使用 Get-AzEffectiveNetworkSecurityGroup Cmdlet,針對 myVM 虛擬機器的網路介面檢閱有效安全性規則:
# Get the effective security rules for the network interface of myVM.
Get-AzEffectiveNetworkSecurityGroup -NetworkInterfaceName 'myVM' -ResourceGroupName 'myResourceGroup'
針對允許對外存取 www.bing.com
的 AllowInternetOutbound 規則,傳回的輸出包含下列資訊:
{
"Name": "defaultSecurityRules/AllowInternetOutBound",
"Protocol": "All",
"SourcePortRange": [
"0-65535"
],
"DestinationPortRange": [
"0-65535"
],
"SourceAddressPrefix": [
"0.0.0.0/0",
"0.0.0.0/0"
],
"DestinationAddressPrefix": [
"Internet"
],
"ExpandedSourceAddressPrefix": [],
"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"
],
"Access": "Allow",
"Priority": 65001,
"Direction": "Outbound"
},
您可以在輸出中看到位址前置詞 13.104.0.0/13 是 AllowInternetOutBound 規則的位址前置詞之一。 此前置詞包含 IP 位址 13.107.21.200,您用來測試 www.bing.com
的輸出通訊。
同樣地,您可以檢查其他規則,以查看每個規則下的來源和目的地 IP 位址前置詞。
清除資源
您可以使用 Remove-AzResourceGroup,來刪除不再需要的資源群組及其包含的所有資源:
# Delete the resource group and all resources it contains.
Remove-AzResourceGroup -Name 'myResourceGroup' -Force