You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
description: In this article, you learn how to connect virtual networks with virtual network peering, using the Azure CLI.
4
4
services: virtual-network
5
5
author: asudbring
6
6
ms.service: virtual-network
7
-
ms.devlang: azurecli
8
7
ms.topic: how-to
9
-
ms.tgt_pltfrm: virtual-network
10
-
ms.date: 03/13/2018
8
+
ms.date: 04/15/2024
11
9
ms.author: allensu
12
10
ms.custom: devx-track-azurecli
13
11
# Customer intent: I want to connect two virtual networks so that virtual machines in one virtual network can communicate with virtual machines in the other virtual network.
14
12
---
15
13
16
14
# Connect virtual networks with virtual network peering using the Azure CLI
17
15
18
-
You can connect virtual networks to each other with virtual network peering. Once virtual networks are peered, resources in both virtual networks are able to communicate with each other, with the same latency and bandwidth as if the resources were in the same virtual network. In this article, you learn how to:
16
+
You can connect virtual networks to each other with virtual network peering. Once virtual networks are peered, resources in both virtual networks are able to communicate with each other, with the same latency and bandwidth as if the resources were in the same virtual network.
17
+
18
+
In this article, you learn how to:
19
19
20
20
* Create two virtual networks
21
+
21
22
* Connect two virtual networks with a virtual network peering
23
+
22
24
* Deploy a virtual machine (VM) into each virtual network
@@ -30,117 +33,119 @@ You can connect virtual networks to each other with virtual network peering. Onc
30
33
31
34
## Create virtual networks
32
35
33
-
Before creating a virtual network, you have to create a resource group for the virtual network, and all other resources created in this article. Create a resource group with [az group create](/cli/azure/group). The following example creates a resource group named *myResourceGroup* in the *eastus* location.
36
+
Before creating a virtual network, you have to create a resource group for the virtual network, and all other resources created in this article. Create a resource group with [az group create](/cli/azure/group). The following example creates a resource group named **test-rg** in the **eastus** location.
34
37
35
38
```azurecli-interactive
36
-
az group create --name myResourceGroup --location eastus
39
+
az group create \
40
+
--name test-rg \
41
+
--location eastus
37
42
```
38
43
39
-
Create a virtual network with [az network vnet create](/cli/azure/network/vnet). The following example creates a virtual network named *myVirtualNetwork1* with the address prefix *10.0.0.0/16*.
44
+
Create a virtual network with [az network vnet create](/cli/azure/network/vnet#az-network-vnet-create). The following example creates a virtual network named **vnet-1** with the address prefix **10.0.0.0/16**.
40
45
41
46
```azurecli-interactive
42
47
az network vnet create \
43
-
--name myVirtualNetwork1 \
44
-
--resource-group myResourceGroup \
48
+
--name vnet-1 \
49
+
--resource-group test-rg \
45
50
--address-prefixes 10.0.0.0/16 \
46
-
--subnet-name Subnet1 \
51
+
--subnet-name subnet-1 \
47
52
--subnet-prefix 10.0.0.0/24
48
53
```
49
54
50
-
Create a virtual network named *myVirtualNetwork2* with the address prefix *10.1.0.0/16*:
55
+
Create a virtual network named **vnet-2** with the address prefix **10.1.0.0/16**:
51
56
52
57
```azurecli-interactive
53
58
az network vnet create \
54
-
--name myVirtualNetwork2 \
55
-
--resource-group myResourceGroup \
59
+
--name vnet-2 \
60
+
--resource-group test-rg \
56
61
--address-prefixes 10.1.0.0/16 \
57
-
--subnet-name Subnet1 \
62
+
--subnet-name subnet-1 \
58
63
--subnet-prefix 10.1.0.0/24
59
64
```
60
65
61
66
## Peer virtual networks
62
67
63
-
Peerings are established between virtual network IDs, so you must first get the ID of each virtual network with [az network vnet show](/cli/azure/network/vnet) and store the ID in a variable.
68
+
Peerings are established between virtual network IDs. Obtain the ID of each virtual network with [az network vnet show](/cli/azure/network/vnet#az-network-vnet-show) and store the ID in a variable.
64
69
65
70
```azurecli-interactive
66
-
# Get the id for myVirtualNetwork1.
71
+
# Get the id for vnet-1.
67
72
vNet1Id=$(az network vnet show \
68
-
--resource-group myResourceGroup \
69
-
--name myVirtualNetwork1 \
73
+
--resource-group test-rg \
74
+
--name vnet-1 \
70
75
--query id --out tsv)
71
76
72
-
# Get the id for myVirtualNetwork2.
77
+
# Get the id for vnet-2.
73
78
vNet2Id=$(az network vnet show \
74
-
--resource-group myResourceGroup \
75
-
--name myVirtualNetwork2 \
79
+
--resource-group test-rg \
80
+
--name vnet-2 \
76
81
--query id \
77
82
--out tsv)
78
83
```
79
84
80
-
Create a peering from *myVirtualNetwork1* to *myVirtualNetwork2* with [az network vnet peering create](/cli/azure/network/vnet/peering). If the `--allow-vnet-access` parameter is not specified, a peering is established, but no communication can flow through it.
85
+
Create a peering from **vnet-1** to **vnet-2** with [az network vnet peering create](/cli/azure/network/vnet/peering#az-network-vnet-peering-create). If the `--allow-vnet-access` parameter isn't specified, a peering is established, but no communication can flow through it.
81
86
82
87
```azurecli-interactive
83
88
az network vnet peering create \
84
-
--name myVirtualNetwork1-myVirtualNetwork2 \
85
-
--resource-group myResourceGroup \
86
-
--vnet-name myVirtualNetwork1 \
89
+
--name vnet-1-to-vnet-2 \
90
+
--resource-group test-rg \
91
+
--vnet-name vnet-1 \
87
92
--remote-vnet $vNet2Id \
88
93
--allow-vnet-access
89
94
```
90
95
91
-
In the output returned after the previous command executes, you see that the **peeringState** is *Initiated*. The peering remains in the *Initiated* state until you create the peering from *myVirtualNetwork2* to *myVirtualNetwork1*. Create a peering from *myVirtualNetwork2* to *myVirtualNetwork1*.
96
+
In the output returned after the previous command executes, you see that the **peeringState** is **Initiated**. The peering remains in the **Initiated** state until you create the peering from **vnet-2** to **vnet-1**. Create a peering from **vnet-2** to **vnet-1**.
92
97
93
98
```azurecli-interactive
94
99
az network vnet peering create \
95
-
--name myVirtualNetwork2-myVirtualNetwork1 \
96
-
--resource-group myResourceGroup \
97
-
--vnet-name myVirtualNetwork2 \
100
+
--name vnet-2-to-vnet-1 \
101
+
--resource-group test-rg \
102
+
--vnet-name vnet-2 \
98
103
--remote-vnet $vNet1Id \
99
104
--allow-vnet-access
100
105
```
101
106
102
-
In the output returned after the previous command executes, you see that the **peeringState** is *Connected*. Azure also changed the peering state of the *myVirtualNetwork1-myVirtualNetwork2* peering to *Connected*. Confirm that the peering state for the *myVirtualNetwork1-myVirtualNetwork2* peering changed to *Connected* with [az network vnet peering show](/cli/azure/network/vnet/peering).
107
+
In the output returned after the previous command executes, you see that the **peeringState** is **Connected**. Azure also changed the peering state of the **vnet-1-to-vnet-2** peering to **Connected**. Confirm that the peering state for the **vnet-1-to-vnet-2** peering changed to **Connected** with [az network vnet peering show](/cli/azure/network/vnet/peering#az-network-vnet-show).
103
108
104
109
```azurecli-interactive
105
110
az network vnet peering show \
106
-
--name myVirtualNetwork1-myVirtualNetwork2 \
107
-
--resource-group myResourceGroup \
108
-
--vnet-name myVirtualNetwork1 \
111
+
--name vnet-1-to-vnet-2 \
112
+
--resource-group test-rg \
113
+
--vnet-name vnet-1 \
109
114
--query peeringState
110
115
```
111
116
112
-
Resources in one virtual network cannot communicate with resources in the other virtual network until the **peeringState** for the peerings in both virtual networks is *Connected*.
117
+
Resources in one virtual network can't communicate with resources in the other virtual network until the **peeringState** for the peerings in both virtual networks is **Connected**.
113
118
114
119
## Create virtual machines
115
120
116
121
Create a VM in each virtual network so that you can communicate between them in a later step.
117
122
118
123
### Create the first VM
119
124
120
-
Create a VM with [az vm create](/cli/azure/vm). The following example creates a VM named *myVm1* in the *myVirtualNetwork1* virtual network. If SSH keys do not already exist in a default key location, the command creates them. To use a specific set of keys, use the `--ssh-key-value` option. The `--no-wait` option creates the VM in the background, so you can continue to the next step.
125
+
Create a VM with [az vm create](/cli/azure/vm#az-vm-create). The following example creates a VM named **vm-1** in the **vnet-1** virtual network. If SSH keys don't already exist in a default key location, the command creates them. To use a specific set of keys, use the `--ssh-key-value` option. The `--no-wait` option creates the VM in the background, so you can continue to the next step.
121
126
122
127
```azurecli-interactive
123
128
az vm create \
124
-
--resource-group myResourceGroup \
125
-
--name myVm1 \
129
+
--resource-group test-rg \
130
+
--name vm-1 \
126
131
--image Ubuntu2204 \
127
-
--vnet-name myVirtualNetwork1 \
128
-
--subnet Subnet1 \
132
+
--vnet-name vnet-1 \
133
+
--subnet subnet-1 \
129
134
--generate-ssh-keys \
130
135
--no-wait
131
136
```
132
137
133
138
### Create the second VM
134
139
135
-
Create a VM in the *myVirtualNetwork2* virtual network.
140
+
Create a VM in the **vnet-2** virtual network.
136
141
137
142
```azurecli-interactive
138
143
az vm create \
139
-
--resource-group myResourceGroup \
140
-
--name myVm2 \
144
+
--resource-group test-rg \
145
+
--name vm-2 \
141
146
--image Ubuntu2204 \
142
-
--vnet-name myVirtualNetwork2 \
143
-
--subnet Subnet1 \
147
+
--vnet-name vnet-2 \
148
+
--subnet subnet-1 \
144
149
--generate-ssh-keys
145
150
```
146
151
@@ -149,13 +154,13 @@ The VM takes a few minutes to create. After the VM is created, the Azure CLI sho
@@ -165,28 +170,30 @@ Take note of the **publicIpAddress**. This address is used to access the VM from
165
170
166
171
## Communicate between VMs
167
172
168
-
Use the following command to create an SSH session with the *myVm2* VM. Replace `<publicIpAddress>` with the public IP address of your VM. In the previous example, the public IP address is *13.90.242.231*.
173
+
Use the following command to create an SSH session with the **vm-2** VM. Replace `<publicIpAddress>` with the public IP address of your VM. In the previous example, the public IP address is **13.90.242.231**.
169
174
170
175
```bash
171
176
ssh <publicIpAddress>
172
177
```
173
178
174
-
Ping the VM in *myVirtualNetwork1*.
179
+
Ping the VM in *vnet-1*.
175
180
176
181
```bash
177
182
ping 10.0.0.4 -c 4
178
183
```
179
184
180
185
You receive four replies.
181
186
182
-
Close the SSH session to the *myVm2* VM.
187
+
Close the SSH session to the **vm-2** VM.
183
188
184
189
## Clean up resources
185
190
186
-
When no longer needed, use [az group delete](/cli/azure/group) to remove the resource group and all of the resources it contains.
191
+
When no longer needed, use [az group delete](/cli/azure/group#az-group-delete) to remove the resource group and all of the resources it contains.
0 commit comments