使用 PowerShell 建立網路安全性群組 (傳統)

您可以在虛擬網路中,使用 NSG 控制傳輸至一個或多個虛擬機器 (VM)、角色執行個體、網路介面卡 (NIC) 或子網路的流量。 NSG 包含存取控制規則,可根據流量方向、通訊協定、來源位址和連接埠與目的地位址和連接埠,允許或拒絕流量。 NSG 的規則可以隨時變更,而變更時會套用至所有相關聯的執行個體。

如需 NSG 的詳細資訊,請瀏覽 何謂 NSG

重要

使用 Azure 資源之前,請務必了解 Azure 目前有 Azure Resource Manager 和「傳統」兩種部署模型。 在使用任何 Azure 資源之前,請先確認您了解 部署模型和工具 。 您可以按一下本文頂端的索引標籤,檢視不同工具的文件。

本文涵蓋之內容包括傳統部署模型。 您也可以 在資源管理員部署模型中建立 NSG

狀況

為了更清楚說明如何建立 NSG,此文件會使用下列案例:

VNet 案例

在這個案例中,您會在 TestVNet 虛擬網路的每個子網路中建立 NSG ,如下所述:

  • NSG-FrontEnd。 前端 NSG 會套用到「前端」子網路,且包含兩個規則:
    • rdp-rule。 允許 RDP 流量流向「前端」子網路。
    • web-rule。 允許 HTTP 流量流向「前端」子網路。
  • NSG-BackEnd。 後端 NSG 會套用到「後端」子網路,且包含兩個規則:
    • sql-rule。 僅允許 SQL 流量來自「前端」子網路。
    • web-rule。 拒絕來自「後端」子網路的所有網際網路繫結流量。

這些規則的組合會建立類似 DMZ 的案例,其後端子網路只能接收來自前端子網路 SQL 的傳入流量,且無網際網路的存取權;而前端子網路則可與網際網路通訊,並接收傳入的 HTTP 要求。

以下的範例 PowerShell 命令會預期已根據上述案例建立簡單的環境。 如果您想要執行如本文件中所顯示的命令,請先建置 建立 VNet以建置測試環境。

建立前端子網路的 NSG

  1. 如果您未曾用過 Azure PowerShell,請參閱如何安裝和設定 Azure PowerShell

  2. 建立名為 NSG-FrontEnd 的網路安全性群組:

    New-AzureNetworkSecurityGroup -Name "NSG-FrontEnd" -Location uswest `
      -Label "Front end subnet NSG"
    
  3. 建立允許從網際網路存取連接埠 3389 的安全性規則:

    Get-AzureNetworkSecurityGroup -Name "NSG-FrontEnd" `
      | Set-AzureNetworkSecurityRule -Name rdp-rule `
      -Action Allow -Protocol TCP -Type Inbound -Priority 100 `
      -SourceAddressPrefix Internet  -SourcePortRange '*' `
      -DestinationAddressPrefix '*' -DestinationPortRange '3389'
    
  4. 建立允許從網際網路存取連接埠 80 的安全性規則:

    Get-AzureNetworkSecurityGroup -Name "NSG-FrontEnd" `
      | Set-AzureNetworkSecurityRule -Name web-rule `
      -Action Allow -Protocol TCP -Type Inbound -Priority 200 `
      -SourceAddressPrefix Internet  -SourcePortRange '*' `
      -DestinationAddressPrefix '*' -DestinationPortRange '80'
    
  5. 建立網路安全性群組與子網的關聯:

    Get-AzureNetworkSecurityGroup -Name "NSG-Frontend" `
    | Set-AzureNetworkSecurityGroupToSubnet -VirtualNetworkName "TestVNet" `
    -Subnet "FrontEnd"
    

建立後端子網路的 NSG

  1. 建立名為 NSG-BackEnd 的網路安全性群組:

    New-AzureNetworkSecurityGroup -Name "NSG-BackEnd" -Location uswest `
      -Label "Back end subnet NSG"
    
  2. 建立允許從前端子網路存取連接埠 1433 (SQL Server 所使用的預設連接埠) 的安全性規則:

    Get-AzureNetworkSecurityGroup -Name "NSG-FrontEnd" `
      | Set-AzureNetworkSecurityRule -Name rdp-rule `
      -Action Allow -Protocol TCP -Type Inbound -Priority 100 `
      -SourceAddressPrefix 192.168.1.0/24  -SourcePortRange '*' `
      -DestinationAddressPrefix '*' -DestinationPortRange '1433'
    
  3. 建立封鎖從子網路存取網際網路的安全性規則:

    Get-AzureNetworkSecurityGroup -Name "NSG-BackEnd" `
      | Set-AzureNetworkSecurityRule -Name block-internet `
      -Action Deny -Protocol '*' -Type Outbound -Priority 200 `
      -SourceAddressPrefix '*'  -SourcePortRange '*' `
      -DestinationAddressPrefix Internet -DestinationPortRange '*'
    
  4. 建立網路安全性群組與子網的關聯:

    Get-AzureNetworkSecurityGroup -Name "NSG-Backend" `
    | Set-AzureNetworkSecurityGroupToSubnet -VirtualNetworkName "TestVNet" `
    -Subnet "BackEnd"