Computer >> Hướng Dẫn Máy Tính >  >> Kết Nối Mạng >> An Ninh Mạng

Cách thay đổi Network Security Group (NSG) trên Azure

Azure Network Security Group (NSG) - Quản trị & Thay đổi

Network Security Group (NSG) là tài nguyên Azure chứa danh sách quy tắc bảo mật (Security Rules) cho phép/từ chối traffic mạng vào/ra các tài nguyên Azure (VM, Subnet, NIC). NSG hoạt động ở Layer 3/4 (Network/Transport).

Cấu trúc quy tắc NSG

Cách thay đổi NSG trên Azure Portal

  1. Đăng nhập portal.azure.com
  2. Tìm kiếm Network security groups > Chọn NSG cần sửa
  3. Menu bên trái: Inbound security rules hoặc Outbound security rules
  4. Thêm mới: Click + Add > Điền thông tin > Add
  5. Sửa: Click tên rule > Chỉnh sửa > Save
  6. Xóa: Click ... > Delete
  7. Sắp xếp priority: Kéo thả hoặc chỉnh số priority

Cách thay đổi NSG bằng Azure CLI

# Tạo rule mới
az network nsg rule create \
  --resource-group myRG \
  --nsg-name myNSG \
  --name Allow-HTTP \
  --priority 100 \
  --source-address-prefixes Internet \
  --destination-port-ranges 80 \
  --protocol Tcp \
  --access Allow \
  --direction Inbound

# Cập nhật rule
az network nsg rule update \
  --resource-group myRG \
  --nsg-name myNSG \
  --name Allow-HTTP \
  --priority 110 \
  --destination-port-ranges 8080

# Xóa rule
az network nsg rule delete --resource-group myRG --nsg-name myNSG --name Allow-HTTP

Cách thay đổi NSG bằng PowerShell

# Lấy NSG
$nsg = Get-AzNetworkSecurityGroup -ResourceGroupName myRG -Name myNSG

# Thêm rule
Add-AzNetworkSecurityRuleConfig -NetworkSecurityGroup $nsg `
  -Name Allow-HTTPS -Priority 120 -SourceAddressPrefix Internet `
  -DestinationPortRange 443 -Protocol Tcp -Access Allow -Direction Inbound

# Áp dụng thay đổi
Set-AzNetworkSecurityGroup -NetworkSecurityGroup $nsg

NSG vs Azure Firewall vs ASG

Thuộc tínhMô tả
NameTên quy tắc (duy nhất trong NSG)
PrioritySố 100-4096 (thấp hơn = ưu tiên cao hơn)
Source/DestinationIP CIDR, Service Tag (Internet, VirtualNetwork, AzureLoadBalancer), Application Security Group (ASG)
ProtocolTCP, UDP, ICMP, ESP, AH, * (All)
DirectionInbound (vào) / Outbound (ra)
Port RangeCổng đích (vd: 80, 443, 22, 3389, 1000-2000)
ActionAllow / Deny
Đặc điểmNSGAzure FirewallASG (App Security Group)
LayerL3/L4L3/L4/L7Logical grouping
StatefulCóCó (Full stateful)N/A
FQDN/SQL/Threat IntelKhôngCóKhông
SNAT/DNATKhôngCóKhông
Chi phíMiễn phí~$1.25/hr + dataMiễn phí

Best Practice NSG

  • Gán NSG tại Subnet (khuyên dùng) thay vì NIC riêng lẻ
  • Dùng Service Tags (Internet, VirtualNetwork, AzureCloud, Storage, Sql...) thay vì hardcode IP
  • Dùng ASG nhóm VM theo vai trò (Web, App, DB) > Rule trỏ đến ASG
  • Priority: Leave gaps (100, 200, 300...) để chèn rule mới
  • Default rules: Không xóa, hiểu rõ DenyAllInbound/AllowVnetInbound/AllowAzureLoadBalancerInbound
  • Log: Bật NSG Flow Logs gửi Log Analytics/Storage để audit, anomaly detection

Lưu ý: Không thể rename NSG

Azure không hỗ trợ đổi tên NSG. Phải tạo NSG mới với tên mong muốn, copy rules, associate lại resources, rồi xóa NSG cũ.