Edit

Share via


Tutorial: Connect virtual networks with virtual network peering

You can connect virtual networks to each other with virtual network peering. These virtual networks can be in the same region or different regions (also known as global virtual network peering). Once virtual networks are peered, resources in both virtual networks can communicate with each other over a low-latency, high-bandwidth connection using the Microsoft backbone network.

Diagram of Azure resources created in tutorial.

In this tutorial, you learn how to:

  • Create virtual networks
  • Connect two virtual networks with virtual network peering
  • Deploy a virtual machine (VM) into each virtual network
  • Communicate between virtual machines

Prerequisites

Create a resource group

  1. Sign in to the Azure portal.

  2. In the search box at the top of the portal, enter Resource group. Select Resource groups in the search results.

  3. Select + Create.

  4. In the Basics tab of Create a resource group, enter, or select the following information:

    Setting Value
    Subscription Select your subscription.
    Resource group Enter test-rg.
    Region Select East US 2.
  5. Select Review + create.

  6. Select Create.

Create a virtual network

  1. In the search box at the top of the portal, enter Virtual network. Select Virtual networks in the search results.

  2. Select + Create.

  3. On the Basics tab of Create virtual network, enter, or select the following information:

    Setting Value
    Project details
    Subscription Select your subscription.
    Resource group Select test-rg.
    Instance details
    Name Enter vnet-1.
    Region Select East US 2.
  4. Select Next to proceed to the Security tab.

  5. Select Next to proceed to the IP Addresses tab.

  6. In the address space box in Subnets, select the default subnet.

  7. In Edit subnet, enter, or select the following information:

    Setting Value
    Subnet details
    Subnet template Leave the default Default.
    Name Enter subnet-1.
    Starting address Leave the default of 10.0.0.0.
    Subnet size Leave the default of /24 (256 addresses).
  8. Select Save.

  9. Select Review + create at the bottom of the screen, and when validation passes, select Create.

Create a second virtual network

Repeat the previous steps to create a second virtual network with the following values:

Note

The second virtual network can be in the same region as the first virtual network or in a different region. You can skip the Bastion deployment for the second virtual network. After the virtual network peering is established, you can connect to both virtual machines with the same Bastion deployment.

Setting Value
Name vnet-2
Address space 10.1.0.0/16
Resource group test-rg
Subnet name subnet-1
Subnet address range 10.1.0.0/24

Deploy Azure Bastion

Azure Bastion uses your browser to connect to virtual machines (VMs) in your virtual network over secure shell (SSH) or remote desktop protocol (RDP) by using their private IP addresses. The virtual machines don't need public IP addresses, client software, or special configuration. For more information about Azure Bastion, see Azure Bastion.

Note

Hourly pricing starts from the moment that Bastion is deployed, regardless of outbound data usage. For more information, see Pricing and SKUs. If you're deploying Bastion as part of a tutorial or test, we recommend that you delete this resource after you finish using it.

  1. In the search box at the top of the portal, enter Bastion. Select Bastions in the search results.

  2. Select + Create.

  3. In the Basics tab of Create a Bastion, enter, or select the following information:

    Setting Value
    Project details
    Subscription Select your subscription.
    Resource group Select test-rg.
    Instance details
    Name Enter bastion.
    Region Select East US 2.
    Tier Select Developer.
    Configure virtual networks
    Virtual network Select vnet-1.
    Subnet The AzureBastionSubnet is created automatically with an address space of /26 or larger.
  4. Select Review + create.

  5. Select Create.

Peer virtual networks

Create virtual network peer

Use the following steps to create a two way network peer between vnet1 and vnet2.

  1. In the search box at the top of the portal, enter Virtual network. Select Virtual networks in the search results.

  2. Select vnet-1.

  3. In Settings select Peerings.

  4. Select + Add.

  5. Enter or select the following information in Add peering:

    Setting Value
    Remote virtual network summary
    Peering link name Enter vnet-2-to-vnet-1.
    Virtual network deployment model Leave the default of Resource Manager.
    Subscription Select your subscription.
    Virtual network Select vnet-2.
    Remote virtual network peering settings
    Allow 'vnet-2' to access 'vnet-1' Leave the default of selected.
    Allow 'vnet-2' to receive forwarded traffic from 'vnet-1' Select the checkbox.
    Allow gateway or route server in 'vnet-2' to forward traffic to 'vnet-1' Leave the default of cleared.
    Enable 'vnet-2' to use 'vnet-1's' remote gateway or route server Leave the default of cleared.
    Local virtual network peering summary
    Peering link name Enter vnet-1-to-vnet-2.
    Local virtual network peering settings
    Allow 'vnet-1' to access 'vnet-2' Leave the default of selected.
    Allow 'vnet-1' to receive forwarded traffic from 'vnet-2' Select the checkbox.
    Allow gateway or route server in 'vnet-1' to forward traffic to 'vnet-2' Leave the default of cleared.
    Enable 'vnet-1' to use 'vnet-2's' remote gateway or route server Leave the default of cleared.

    Screenshot of Add peering in the Azure portal.

  6. Select Add.

Create virtual machines

Test the communication between the virtual machines by creating a virtual machine in each virtual network. The virtual machines can communicate with each other over the virtual network peering you created.

  1. In the search box at the top of the portal, enter Virtual machine. Select Virtual machines in the search results.

  2. Select + Create then Azure virtual machine.

  3. In Create a virtual machine enter, or select the following information in the Basics tab:

    Setting Value
    Project details
    Subscription Select your subscription.
    Resource group Select test-rg.
    Instance details
    Virtual machine name Enter vm-1.
    Region Select (US) East US 2.
    Availability options Select No infrastructure redundancy required.
    Security type Select Standard.
    Image Select Ubuntu Server 24.04 LTS - x64 Gen2.
    VM architecture Leave the default of x64.
    Size Select a size.
    Administrator account
    Authentication type Select SSH public key.
    Username Enter a username.
    SSH public key source Select Generate new key pair.
    Key pair name Enter vm-1-key.
    Inbound port rules
    Public inbound ports Select None.
  4. Select Next: Disks then Next: Networking.

  5. In the Networking tab, enter, or select the following information:

    Setting Value
    Network interface
    Virtual network Select vnet-1.
    Subnet Select subnet-1 (10.0.0.0/24).
    Public IP Select None.
    Network interface (NIC) network security group Select Advanced.
    Configure network security group Select Create new.
    In Name enter nsg-1.
    Select OK.
  6. Leave the rest of the options at the defaults and select Review + create.

  7. Select Create.

Repeat the previous steps to create a second virtual machine in the second virtual network with the following values:

Setting Value
Virtual machine name vm-2
Region East US 2 or same region as vnet-2.
SSH public key source Generate new key pair.
Key pair name vm-2-key.
Virtual network Select vnet-2.
Subnet Select subnet-1 (10.1.0.0/24).
Public IP None
Network security group name nsg-2

Wait for the virtual machines to be created before continuing with the next steps.

Connect to a virtual machine

  1. In the search box at the top of the portal, enter Virtual machine. Select Virtual machines in the search results.

  2. In Virtual machines, select vm-1.

  3. Select Connect then Connect via Bastion in the Overview section.

  4. In the Bastion connection page, enter or select the following information:

    Setting Value
    Authentication Type Select SSH Private Key from Local File.
    Username Enter the username you created.
    Local File Select the vm-1-key private key file you downloaded.
  5. Select Connect.

Communicate between virtual machines

  1. At the bash prompt for vm-1, enter ping -c 4 10.1.0.4.

    You get a reply similar to the following message:

    azureuser@vm-1:~$ ping -c 4 10.1.0.4
    PING 10.1.0.4 (10.1.0.4) 56(84) bytes of data.
    64 bytes from 10.1.0.4: icmp_seq=1 ttl=64 time=2.29 ms
    64 bytes from 10.1.0.4: icmp_seq=2 ttl=64 time=1.06 ms
    64 bytes from 10.1.0.4: icmp_seq=3 ttl=64 time=1.30 ms
    64 bytes from 10.1.0.4: icmp_seq=4 ttl=64 time=0.998 ms
    
    --- 10.1.0.4 ping statistics ---
    4 packets transmitted, 4 received, 0% packet loss, time 3004ms
    rtt min/avg/max/mdev = 0.998/1.411/2.292/0.520 ms
    
  2. Close the Bastion session.

  3. In the search box at the top of the portal, enter Virtual machine. Select Virtual machines in the search results.

  4. In Virtual machines, select vm-2.

  5. Select Connect then Connect via Bastion in the Overview section.

  6. In the Bastion connection page, enter or select the following information:

    Setting Value
    Authentication Type Select SSH Private Key from Local File.
    Username Enter the username you created.
    Local File Select the vm-2-key private key file you downloaded.
  7. Select Connect.

  8. At the bash prompt for vm-2, enter ping -c 4 10.0.0.4.

    You get a reply similar to the following message:

    azureuser@vm-2:~$ ping -c 4 10.0.0.4
    PING 10.0.0.4 (10.0.0.4) 56(84) bytes of data.
    64 bytes from 10.0.0.4: icmp_seq=1 ttl=64 time=1.81 ms
    64 bytes from 10.0.0.4: icmp_seq=2 ttl=64 time=3.35 ms
    64 bytes from 10.0.0.4: icmp_seq=3 ttl=64 time=0.811 ms
    64 bytes from 10.0.0.4: icmp_seq=4 ttl=64 time=1.28 ms
    
  9. Close the Bastion session.

When you finish using the resources that you created, you can delete the resource group and all its resources.

  1. In the Azure portal, search for and select Resource groups.

  2. On the Resource groups page, select the test-rg resource group.

  3. On the test-rg page, select Delete resource group.

  4. Enter test-rg in Enter resource group name to confirm deletion, and then select Delete.

Next steps

In this tutorial, you:

  • Created virtual network peering between two virtual networks.

  • Tested the communication between two virtual machines over the virtual network peering with ping.

To learn more about virtual network peering: