Bagikan melalui


Diagnose a virtual machine network traffic filter problem

In this article, you learn how to diagnose network traffic filter issues for a virtual machine (VM). The process involves viewing the effective security rules applied by the network security group (NSG).

Dengan NSG, Anda dapat mengontrol jenis lalu lintas yang mengalir masuk ke dan keluar dari VM. Anda dapat mengaitkan NSG ke subnet di jaringan virtual Azure, antarmuka jaringan yang dipasang pada VM, atau keduanya. Aturan keamanan efektif yang diterapkan pada antarmuka jaringan adalah agregasi aturan yang ada di NSG yang terkait dengan antarmuka jaringan dan subnet tempat antarmuka jaringan berada. Rules in different NSGs can sometimes conflict with each other and affect a VM's network connectivity. Anda dapat melihat semua aturan keamanan yang efektif dari NSG yang diterapkan pada antarmuka jaringan VM Anda. Jika Anda tidak terbiasa dengan jaringan virtual, antarmuka jaringan, atau konsep NSG, lihat Gambaran umum jaringan virtual, Antarmuka jaringan, dan Gambaran umum kelompok keamanan jaringan.

Skenario

Anda mencoba membuat sambungan ke VM melalui port 80 dari internet, tetapi koneksinya gagal. Untuk mengetahui mengapa Anda tidak dapat mengakses port 80 dari Internet, Anda dapat melihat aturan keamanan yang efektif untuk antarmuka jaringan dengan menggunakan portal Microsoft Azure, PowerShell, atau Azure CLI.

Langkah-langkah setelahnya mengasumsikan bahwa Anda memiliki VM yang ada untuk melihat aturan keamanan yang efektif. Jika Anda tidak memiliki VM, terapkan VM Linux atau Windows terlebih dahulu untuk menyelesaikan tugas di artikel ini. Contoh dalam artikel ini adalah untuk VM bernama myVM dengan antarmuka jaringan bernama myVMVMNic. VM dan antarmuka jaringan berada dalam grup sumber daya bernama myResourceGroup, dan berada di wilayah AS Timur. Ubah nilai dalam langkah-langkah yang sesuai untuk VM yang sedang Anda diagnosis masalahnya.

Mendiagnosis dengan menggunakan portal Microsoft Azure

  1. Masuklah ke portal Microsoft Azure dengan akun Azure yang memiliki izin yang diperlukan.

  2. Di bagian atas portal Microsoft Azure, masukkan nama VM di kotak pencarian. Saat nama VM muncul di hasil pencarian, pilih VM tersebut.

  3. Expand Networking in the left pane. Select Network settings. The following figures show the network security group settings for the VM's network interface.

    Screenshot of security rules for NSG nsg-subnet.

    Screenshot of security rules for NSG nsg-nic.

    The rules you see listed in the previous figures are for a network interface named vm-1445. You see that there are Inbound port rules for the network interface from two different network security groups:

    • nsg-subnet: Associated to the subnet that the network interface is in.
    • nsg-nic: Associated to the network interface in the VM named vm-1445.

    DenyAllInBound adalah aturan yang mencegah komunikasi masuk ke VM melalui port 80, dari internet, seperti yang dijelaskan dalam skenario. Aturan ini mencantumkan 0.0.0.0/0 untuk SUMBER, yang mencakup internet. Tidak ada aturan lain dengan prioritas yang lebih tinggi (angka yang lebih rendah) yang mengizinkan port 80 masuk. Untuk mengizinkan port 80 masuk ke VM dari internet, lihat Mengatasi masalah. Untuk mempelajari selengkapnya tentang aturan keamanan dan bagaimana Azure menerapkan, lihat Kelompok keamanan jaringan.

    At the bottom of the picture, you also see Outbound port rules. The outbound port rules for the network interface are listed.

    Though the picture only shows four inbound rules for each NSG, your NSGs might have many more than four rules. In the picture, you see VirtualNetwork under Source and Destination and AzureLoadBalancer under SOURCE. VirtualNetwork dan AzureLoadBalancer adalah tag layanan. Tag layanan mewakili sekelompok awalan alamat IP yang membantu meminimalkan kompleksitas untuk pembuatan aturan keamanan.

  4. To view the effective security rules, select the interface in the network settings of the virtual machine. Ensure the VM is in a running state before proceeding.

  5. In the settings for the network interface, expand Help, then select Effective security rules.

    The following example shows the example network interface vm-1445 with the Effective security rules selected.

    Screenshot of effective security rules for network interface vm-1445.

    Aturan yang tercantum sama seperti yang Anda lihat di langkah 3, meskipun ada tab berbeda untuk NSG yang terkait dengan antarmuka jaringan dan subnet. Seperti yang Anda lihat di gambar, hanya 50 aturan pertama yang ditunjukkan. Untuk mengunduh file .csv yang berisi semua aturan, pilih Unduh.

  6. The previous steps showed the security rules for a network interface named vm-1445. What if a VM has two network interfaces? VM dalam contoh ini memiliki dua antarmuka jaringan yang dipasang di sana. Aturan keamanan yang efektif dapat berbeda untuk setiap antarmuka jaringan.

    To see the rules for the vm-nic-2 network interface, select it. As shown in the example that follows, the network interface has the same rules associated to its subnet as the vm-1445 network interface, because both network interfaces are in the same subnet. Saat Anda mengaitkan NSG ke subnet, aturannya diterapkan ke semua antarmuka jaringan di subnet.

    Screenshot of security rules for nic vm-nic-2.

    Unlike the vm-1445 network interface, the vm-nic-2 network interface doesn't have a network security group associated to it. Setiap antarmuka jaringan dan subnet dapat memiliki nol, atau satu, NSG yang terkait dengannya. NSG yang terkait dengan setiap antarmuka jaringan atau subnet bisa jadi sama atau berbeda. Anda dapat mengaitkan kelompok keamanan jaringan yang sama dengan antarmuka jaringan dan subnet sebanyak yang Anda pilih.

Meskipun aturan keamanan yang efektif dilihat melalui VM, Anda juga dapat melihat aturan keamanan yang efektif melalui individu.

Mendiagnosis dengan menggunakan PowerShell

Catatan

Sebaiknya Anda menggunakan modul Azure Az PowerShell untuk berinteraksi dengan Azure. Untuk memulai, lihat Menginstal Azure PowerShell. Untuk mempelajari cara bermigrasi ke modul Az PowerShell, lihat Memigrasikan Azure PowerShell dari AzureRM ke Az.

Anda dapat menjalankan perintah berikutnya di Azure Cloud Shell, atau dengan menjalankan PowerShell dari komputer. Azure Cloud Shell adalah shell interaktif gratis. Shell ini memiliki alat Azure umum yang telah dipasang sebelumnya dan dikonfigurasi untuk digunakan dengan akun Anda. Jika menjalankan PowerShell dari komputer, Anda memerlukan modul Azure PowerShell versi 1.0.0 atau yang lebih baru. Jalankan Get-Module -ListAvailable Az di komputer Anda untuk menemukan versi yang sudah terpasang. Jika Anda perlu peningkatan, lihat Instal modul Azure PowerShell. If you're running PowerShell locally, you also need to run Connect-AzAccount to log into Azure with an account that has the necessary permissions].

Dapatkan aturan keamanan yang efektif untuk antarmuka jaringan dengan Get-AzEffectiveNetworkSecurityGroup. The following example gets the effective security rules for a network interface named vm-nic that is in a resource group named test-rg:

$Params = @{
  NetworkInterfaceName = "vm-nic"
  ResourceGroupName    = "test-rg"
}
Get-AzEffectiveNetworkSecurityGroup @Params

Output dikembalikan dalam format json. Untuk memahami output, lihat menginterpretasikan output perintah. Output is only returned if an NSG is associated with the network interface, the subnet the network interface is in, or both. The VM must be in the running state. A VM might have multiple network interfaces with different NSGs applied. Saat memecahkan masalah, jalankan perintah untuk setiap antarmuka jaringan.

If you're still having a connectivity problem, see more diagnoses and considerations.

If you only know the VM name, use the following commands to list all network interface IDs attached to the VM.

$Params = @{
  Name              = "vm-1"
  ResourceGroupName = "test-rg"
}
$VM = Get-AzVM @Params
$VM.NetworkProfile

Anda menerima output yang mirip dengan contoh berikut:

NetworkInterfaces
-----------------
{/subscriptions/<ID>/resourceGroups/myResourceGroup/providers/Microsoft.Network/networkInterfaces/vm-nic

In the previous output, the network interface name is vm-nic.

Mendiagnosis dengan menggunakan Azure CLI

Jika menggunakan perintah Azure CLI untuk menyelesaikan tugas dalam artikel ini, jalankan perintah di Azure Cloud Shell, atau dengan menjalankan Azure CLI dari komputer Anda. Artikel ini memerlukan Azure CLI versi 2.0.32 atau yang lebih baru. Jalankan az --version untuk menemukan versi terinstal. Jika Anda perlu memasang atau meningkatkan, lihat Memasang Azure CLI. Jika Anda menjalankan Azure CLI secara lokal, maka Anda juga perlu menjalankan az login untuk masuk ke Azure dengan akun yang memiliki izin yang diperlukan.

Dapatkan aturan keamanan yang efektif untuk antarmuka jaringan dengan az network nic list-effective-nsg. The following example gets the effective security rules for a network interface named vm-nic that is in a resource group named test-rg:

az network nic list-effective-nsg \
  --name vm-nic \
  --resource-group test-rg

Output dikembalikan dalam format json. Untuk memahami output, lihat menginterpretasikan output perintah. Output is only returned if an NSG is associated with the network interface, the subnet the network interface is in, or both. The VM must be in the running state. A VM might have multiple network interfaces with different NSGs applied. Saat memecahkan masalah, jalankan perintah untuk setiap antarmuka jaringan.

If you're still having a connectivity problem, see more diagnoses and considerations.

If you only know the VM name, use the following commands to list all network interface IDs attached to the VM.

az vm show \
  --name vm-1 \
  --resource-group test-rg

Dalam output yang dikembalikan, Anda melihat informasi yang mirip dengan contoh berikut:

"networkProfile": {
    "additionalProperties": {},
    "networkInterfaces": [
      {
        "additionalProperties": {},
        "id": "/subscriptions/<ID>/resourceGroups/test-rg/providers/Microsoft.Network/networkInterfaces/vm-nic",
        "primary": true,
        "resourceGroup": "test-rg"
      },

In the previous output, the network interface name is vm-nic

Interpret command output

Terlepas dari apakah Anda menggunakan PowerShell atau Azure CLI untuk mendiagnosis masalahnya, Anda menerima output yang berisi informasi berikut:

  • NetworkSecurityGroup: ID kelompok keamanan jaringan.
  • Association: Indicates whether the network security group is linked to a NetworkInterface or Subnet. If the NSG's association status changes right before running the command, wait a few seconds for the output to reflect the update.
  • EffectiveSecurityRules: Penjelasan tentang setiap properti diperinci dalam Membuat aturan keamanan. Nama aturan yang diawali dengan DefaultSecurityRules/ adalah aturan keamanan default yang ada di setiap NSG. Rule names prefaced with securityRules/ are user created rules. Aturan yang menentukan tag layanan, seperti Internet, VirtualNetwork, dan AzureLoadBalancer untuk properti destinationAddressPrefix atau sourceAddressPrefix, juga memiliki nilai untuk properti expandedDestinationAddressPrefix. Properti expandedDestinationAddressPrefix mencantumkan semua awalan alamat yang diwakili oleh tag layanan.

Duplicate rules appear in the output when an NSG is linked to both the network interface and the subnet. Default rules and any custom rules shared between the NSGs cause these duplicates.

defaultSecurityRules/DenyAllInBound adalah aturan yang mencegah komunikasi masuk ke VM melalui port 80, dari internet, seperti yang dijelaskan dalam skenario. Tidak ada aturan lain dengan prioritas yang lebih tinggi (angka yang lebih rendah) yang memungkinkan port 80 masuk dari internet.

Mengatasi masalah

You can diagnose the problem described in the scenario using the Azure portal, PowerShell, or the Azure CLI. The solution is to create a network security rule with the following properties:

Property Value
Sumber Any
Source port ranges Any
Tujuan Alamat IP VM, rentang alamat IP, atau semua alamat di subnet.
Rentang port tujuan 80
Protokol TCP
Tindakan Izinkan
Prioritas 100
Nama Allow-HTTP-All

After rule creation, port 80 is allowed inbound from the internet because its priority is higher than the default DenyAllInBound rule. If NSGs are associated with both the network interface and the subnet, create the same rule in both NSGs. Pelajari cara membuat aturan keamanan.

When Azure processes inbound traffic, it processes rules in the NSG associated to the subnet (if there's an associated NSG), and then it processes the rules in the NSG associated to the network interface. If there's an NSG associated to the network interface and the subnet, the port must be open in both NSGs, for the traffic to reach the VM. Untuk meringankan masalah administrasi dan komunikasi, Anda sebaiknya mengaitkan NSG ke subnet, alih-alih antarmuka jaringan individual. Jika VM dalam subnet memerlukan aturan keamanan yang berbeda, Anda dapat membuat antarmuka jaringan menjadi anggota kelompok keamanan aplikasi (ASG) dan menentukan ASG sebagai sumber serta tujuan aturan keamanan. Pelajari selengkapnya tentang kelompok keamanan aplikasi.

If you're still having communication problems, see Considerations and more diagnosis.

Pertimbangan

Pertimbangkan poin berikut saat memecahkan masalah konektivitas:

  • Aturan keamanan default memblokir akses masuk dari internet dan hanya mengizinkan lalu lintas masuk dari jaringan virtual. Untuk mengizinkan lalu lintas masuk dari Internet, tambahkan aturan keamanan dengan prioritas lebih tinggi daripada aturan default. Pelajari selengkapnya tentang aturan keamanan default atau cara menambahkan aturan keamanan.

  • For peered virtual networks, by default, the VIRTUAL_NETWORK service tag automatically expands to include prefixes for peered virtual networks. To troubleshoot any issues related to virtual network peering, you can view the prefixes in the ExpandedAddressPrefix list. Pelajari selengkapnya tentang komunikasi dua arah jaringan virtual dan tag layanan.

  • Azure only shows effective security rules for a network interface when an NSG is associated with the VM's network interface or subnet. Additionally, the VM must be in the running state.

  • If the network interface or subnet lacks an associated NSG, all ports remain open for inbound and outbound access when a VM has a public IP address. This configuration allows unrestricted access to and from anywhere. To secure the VM, apply an NSG to the subnet hosting the network interface if it has a public IP address.

Diagnoses lainnya

  • Untuk menjalankan tes cepat dan menentukan apakah lalu lintas diizinkan ke atau dari VM, gunakan kemampuan verifikasi alur IP Azure Network Watcher. Verifikasi alur IP memberi tahu Anda apakah lalu lintas diizinkan atau ditolak. Jika ditolak, verifikasi alur IP memberi tahu Anda aturan keamanan mana yang menolak lalu lintas.
  • If there are no security rules causing a VM's network connectivity to fail, the problem might be due to:
    • Perangkat lunak firewall yang berjalan dalam sistem operasi VM
    • Rute yang dikonfigurasi untuk perangkat virtual atau lalu lintas di lokasi fisik. Lalu lintas internet dapat dialihkan ke jaringan lokal Anda melalui penerowongan paksa. If you force tunnel internet traffic to a virtual appliance, or on-premises, you might not be able to connect to the VM from the internet. To learn how to diagnose route problems that might impede the flow of traffic out of the VM, see Diagnose a virtual machine network traffic routing problem.

Langkah berikutnya