Establishing an Azure VPN Connection from CLI/PowerShell
Connecting to Azure Virtual Private Network (VPN) using Command Line Interface (CLI) or PowerShell provides a flexible and efficient way to manage your network connections. In this article, we will guide you through the process of establishing an Azure VPN connection using CLI/PowerShell.
Prerequisites
Before we begin, make sure you have the following prerequisites:
- An Azure subscription
- A virtual network and a virtual network gateway set up in Azure
- A computer with Azure CLI or PowerShell installed
Step 1: Install Azure CLI or PowerShell
If you don't have Azure CLI or PowerShell installed on your computer, follow these steps:
For Azure CLI:
- Visit the Azure CLI installation page and follow the instructions for your operating system.
- Once installed, open the command prompt or terminal and run the command
az loginto sign in to your Azure account.
For PowerShell:
- Visit the PowerShell installation page and follow the instructions for your operating system.
- Launch PowerShell and run the command
Connect-AzAccountto sign in to your Azure account.
Step 2: Retrieve VPN Gateway information
Next, you need to gather the necessary information about your VPN Gateway in Azure. Follow these steps:
- Sign in to the Azure portal (https://portal.azure.com).
- Navigate to your virtual network and select the virtual network gateway you want to connect to.
- Under the "Overview" section, make a note of the following details:
- Public IP address
- Gateway type
- VPN type
- Shared key (pre-shared key)
Step 3: Establish the VPN connection
Now that you have the required information, you can establish the VPN connection using either Azure CLI or PowerShell.
Using Azure CLI:
- Open the command prompt or terminal.
- Run the following command to create a VPN connection:
az network vpn-connection create --name MyVpnConnection --resource-group MyResourceGroup --vnet-gateway1 MyVnetGateway --shared-key MySharedKey --local-gateway2 MyLocalGateway - Replace the placeholders with the appropriate values:
MyVpnConnection: The name for your VPN connectionMyResourceGroup: The name of your resource groupMyVnetGateway: The name of your virtual network gatewayMySharedKey: The shared key (pre-shared key) for your VPN connectionMyLocalGateway: The name of your local network gateway (if applicable)
Using PowerShell:
- Launch PowerShell.
- Run the following command to create a VPN connection:
New-AzVpnConnection -Name MyVpnConnection -ResourceGroupName MyResourceGroup -VirtualNetworkGateway1 MyVnetGateway -SharedKey MySharedKey -LocalNetworkGateway2 MyLocalGateway - Replace the placeholders with the appropriate values, similar to the Azure CLI command.
Once the command executes successfully, your VPN connection will be established. You can verify the connection status in the Azure portal.
Conclusion
In this article, we learned how to establish an Azure VPN connection from CLI/PowerShell. By following these steps, you can easily connect to your Azure VPN using the command line interface, providing a convenient way to manage your network connections.
| Reference | Link |
|---|---|
| Azure CLI installation | https://docs.microsoft.com/en-us/cli/azure/install-azure-cli |
| PowerShell installation | https://docs.microsoft.com/en-us/powershell/azure/new-azureps-module-az?view=azps-10.10.0 |
| Azure portal | https://portal.azure.com |