WireGuard on Azure · Part 1 of 3

Manual tests with PowerShell

Deploying the whole infrastructure with PowerShell (resource group, VNet, NSG, public IP, NIC, VM), then configuring WireGuard by hand on the VM: keys, wg0.conf, forwarding, NAT, a first client and tests. Understanding every piece before automating.

  • Azure
  • PowerShell
  • WireGuard
  • iptables

We are going to deploy the following architecture with PowerShell:

Diagram of the manual deployment (labels in French). In the rg-wireguard-lab resource group, the 10.10.0.0/16 VNet holds the 10.10.1.0/24 subnet, where the Ubuntu 24.04 VM (Standard_B1s) runs: kernel 6.8 with ip_forward set to 1, a network card with IP forwarding on, a wg0 interface at 10.33.33.1/24 on UDP 51820 with MTU 1420, and its wg0.conf file. The static public IP and the NSG (UDP 51820 open, SSH 22 restricted to my IP, implicit deny at 65500) are attached to the card. The client machine, wg0 interface at 10.33.33.2/32 with its client1.conf, reaches the VM across the Internet through an encrypted tunnel. AllowedIPs 0.0.0.0/0 sends all of the machine's traffic into the tunnel, and the 25 s keepalive survives home NAT.
The step 1 deployment: the VM and its network in the resource group, the client machine, and the WireGuard tunnel between the two. · enlarge diagram

A lot of information for something that, seen from a distance, is architecturally very simple:

  • A client machine
  • An Azure VM

Other components come into play, of course, such as virtual networks, subnets, NSGs and public IPs.

For this first step, we will deploy the base of our WireGuard system, which we will build on afterwards. We start by deploying the resource group and the virtual network, and add its subnet.
We then attach the NSG and its rules for SSH and WireGuard.
Next we create a public IP, which we associate with a network interface in the subnet. And we deploy the virtual machine. The rest is just WireGuard configuration.

Deploying the VM

Prerequisites

powershell
Connect-AzAccount
Set-AzContext -Subscription "Root subscription"

New-AzResourceGroup -Name "rg-wireguard-lab" -Location "switzerlandnorth"
bash
ssh-keygen -t ed25519 -f "$HOME\.ssh\wglab" -C "wglab"

Variables

powershell
$rgName   = "rg-wireguard-lab"
$location = (Get-AzResourceGroup -Name $rgName).Location
$prefix   = "wglab"
$adminUser = "louis"
$sshPubKey = Get-Content "$HOME\.ssh\wglab.pub" -Raw

$dnsLabel = "wglab-$((New-Guid).Guid.Substring(0,6))"
$myIp = (Invoke-RestMethod "https://api.ipify.org?format=json").ip

NSG

powershell
$ruleWg = New-AzNetworkSecurityRuleConfig -Name "allow-wireguard" `
  -Protocol Udp -Direction Inbound -Priority 100 -Access Allow `
  -SourceAddressPrefix * -SourcePortRange * `
  -DestinationAddressPrefix * -DestinationPortRange 51820

$ruleSsh = New-AzNetworkSecurityRuleConfig -Name "allow-ssh-admin" `
  -Protocol Tcp -Direction Inbound -Priority 110 -Access Allow `
  -SourceAddressPrefix "$myIp/32" -SourcePortRange * `
  -DestinationAddressPrefix * -DestinationPortRange 22

$nsg = New-AzNetworkSecurityGroup -Name "nsg-$prefix" `
  -ResourceGroupName $rgName -Location $location `
  -SecurityRules $ruleWg, $ruleSsh

The NSG blocks all inbound traffic by default (implicit rule at priority 65500), so these two rules alone define the exposed surface.

VNet and subnet

powershell
$subnet = New-AzVirtualNetworkSubnetConfig -Name "snet-$prefix" `
  -AddressPrefix "10.10.1.0/24" -NetworkSecurityGroup $nsg

$vnet = New-AzVirtualNetwork -Name "vnet-$prefix" `
  -ResourceGroupName $rgName -Location $location `
  -AddressPrefix "10.10.0.0/16" -Subnet $subnet

Public IP and network card

powershell
$pip = New-AzPublicIpAddress -Name "pip-$prefix" `
  -ResourceGroupName $rgName -Location $location `
  -AllocationMethod Static -Sku Standard -DomainNameLabel $dnsLabel

$nic = New-AzNetworkInterface -Name "nic-$prefix" `
  -ResourceGroupName $rgName -Location $location `
  -SubnetId $vnet.Subnets[0].Id -PublicIpAddressId $pip.Id `
  -EnableIPForwarding

-EnableIPForwarding is the parameter not to forget. Without it, Azure checks the source IP of every packet leaving the NIC and drops anything that does not match the card’s address, which means all the traffic relayed for the VPN clients.

The VM

powershell
# -Credential is required even for SSH-only: the password will be ignored,
# but it must satisfy the Azure complexity policy.
$randomPass = ConvertTo-SecureString ([guid]::NewGuid().Guid + "aA1!") -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential ($adminUser, $randomPass)

$vmsz = @{
  VMName = "vm-$prefix" 
  VMSize = "Standard_B1s"
}

$vmos = @{
  ComputerName  = "vm-$prefix" 
  Credential    = $cred
}

$vmkey = @{
  KeyData = $sshPubKey
  Path    = "/home/$adminUser/.ssh/authorized_keys"
}

$vmimage = @{
  PublisherName = "Canonical" 
  Offer         = "ubuntu-24_04-lts" 
  Skus          = "server" 
  Version       = "latest"
}

$vmdisk = @{
  Name                = "osdisk-$prefix" 
  CreateOption        = "FromImage "
  StorageAccountType  = "StandardSSD_LRS" 
  DiskSizeInGB        = 30
}

$vmConfig = New-AzVMConfig @vmsz `
  | Set-AzVMOperatingSystem @vmos -Linux -DisablePasswordAuthentication `
  | Add-AzVMSshPublicKey @vmkey `
  | Set-AzVMSourceImage @vmimage `
  | Set-AzVMOSDisk @vmdisk `
  | Add-AzVMNetworkInterface -Id $nic.Id

New-AzVM -ResourceGroupName $rgName -Location $location -VM $vmConfig

Allow two to three minutes for the creation.

Check

powershell
$fqdn = (Get-AzPublicIpAddress -Name "pip-$prefix" -ResourceGroupName $rgName).DnsSettings.Fqdn
ssh -i "$HOME\.ssh\wglab" "$adminUser@$fqdn"

Configuring the VM

Installation

bash
sudo apt update
sudo apt install -y wireguard wireguard-tools iptables qrencode

Nothing to compile here: WireGuard has been in the Linux kernel since 5.6, and Ubuntu 24.04 runs 6.8. The wireguard package only brings metadata, and wireguard-tools provides the wg and wg-quick binaries:

bash
lsmod | grep wireguard        # empty for now, the module loads on demand

Generating the server keys

bash
sudo -i 

umask 077
mkdir -p /etc/wireguard
cd /etc/wireguard
sh -c 'umask 077; wg genkey > server.key'
sh -c 'wg pubkey < server.key > server.pub'
cat server.pub

Server configuration

First get the name of the outgoing interface:

bash
ip -4 route show default

It will most likely be eth0. Then:

bash
sudo tee /etc/wireguard/wg0.conf > /dev/null <<EOF
[Interface]
Address = 10.33.33.1/24
ListenPort = 51820
PrivateKey = $(sudo cat /etc/wireguard/server.key)
MTU = 1420

PostUp = iptables -A FORWARD -i %i -j ACCEPT
PostUp = iptables -A FORWARD -o %i -j ACCEPT
PostUp = iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
PostDown = iptables -D FORWARD -i %i -j ACCEPT
PostDown = iptables -D FORWARD -o %i -j ACCEPT
PostDown = iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE
EOF

sudo chmod 600 /etc/wireguard/wg0.conf

Directive by directive:

  • Address: the server’s IP inside the tunnel. The /24 tells wg-quick to add a route to the whole 10.33.33.0/24 through wg0. It is a range completely independent from the Azure VNet’s 10.10.1.0/24.
  • ListenPort: UDP only. WireGuard has no TCP mode, by design: wrapping TCP inside TCP makes performance collapse as soon as packets get lost.
  • MTU: 1500 minus the 60 bytes of encapsulation, minus another 20 because why not. If left at the default, full-size packets get fragmented or silently dropped, which gives the most baffling symptom there is: ping works, curl on a small page works, but large transfers freeze.
  • %i: replaced by wg-quick with the interface name, here wg0.
  • The FORWARD rules: Ubuntu leaves the FORWARD policy at ACCEPT by default, so they are often redundant. They make the setup explicit and resilient to a later hardening of the default policy.
  • MASQUERADE: the NAT itself. Packets coming from 10.33.33.x leave through eth0 with the VM’s public IP, and the kernel’s connection tracking handles the way back.

Enabling forwarding

bash
echo 'net.ipv4.ip_forward=1' | sudo tee /etc/sysctl.d/99-wireguard.conf
sudo sysctl --system

cat /proc/sys/net/ipv4/ip_forward   # should print 1

IP forwarding is therefore enabled at two levels: on the Azure NIC, and in the OS kernel. Both are essential for WireGuard to work.

Starting up

bash
sudo systemctl enable --now wg-quick@wg0
sudo systemctl status wg-quick@wg0
sudo wg show

wg show should display the interface with its public key and listening port, with no peer yet.
Check that the interface is up:

bash
ip addr show wg0
sudo iptables -t nat -L POSTROUTING -n -v    # the MASQUERADE rule should be there

A first client

On a machine, whether the server, the client machine or another one, generate the 3 keys:

  • A private key
  • The public key derived from that private key
  • A preshared key

The private key stays on the client machine, and the public key and the preshared key are to be copied to the client machine and to the server.

⚠️ Here, for the test, everything is done from the server, so the client’s private key will be there too. This is of course not recommended in a production environment.

bash
cd /etc/wireguard
sudo sh -c 'umask 077; wg genkey > client1.key'
sudo sh -c 'wg pubkey < client1.key > client1.pub'
sudo sh -c 'umask 077; wg genpsk > client1.psk'

The preshared key is optional. It adds a symmetric layer on top of the Curve25519 exchange, which protects against retroactive decryption by a quantum computer (the famous “harvest now, decrypt later”). It costs nothing and is easy to set up, so we might as well use it.

Declaring the peer on the server side:

bash
sudo tee -a /etc/wireguard/wg0.conf > /dev/null <<EOF

[Peer]
# client1
PublicKey = $(sudo cat /etc/wireguard/client1.pub)
PresharedKey = $(sudo cat /etc/wireguard/client1.psk)
AllowedIPs = 10.33.33.2/32
EOF

Then reload without cutting existing connections:

bash
sudo wg syncconf wg0 <(sudo wg-quick strip wg0)
sudo wg show

The client configuration

bash
sudo tee /etc/wireguard/client1.conf > /dev/null <<EOF
[Interface]
PrivateKey = $(sudo cat /etc/wireguard/client1.key)
Address = 10.33.33.2/32
DNS = 1.1.1.1
MTU = 1420

[Peer]
PublicKey = $(sudo cat /etc/wireguard/server.pub)
PresharedKey = $(sudo cat /etc/wireguard/client1.psk)
Endpoint = wglab-407595.switzerlandnorth.cloudapp.azure.com:51820
AllowedIPs = 0.0.0.0/0, ::/0
PersistentKeepalive = 25
EOF

sudo qrencode -t ansiutf8 < /etc/wireguard/client1.conf   # for mobile
sudo cat /etc/wireguard/client1.conf                      # for a desktop

PersistentKeepalive = 25 sends an empty packet every 25 seconds. Without it, a client behind a home NAT sees its mapping closed after 30 to 180 seconds of inactivity, and the server can no longer reach it to initiate anything. Twenty-five seconds is the chosen value because it stays below the threshold of almost all consumer equipment.

Address = 10.33.33.2/32 on the client side: the /32 is deliberate. A /24 would make the client believe it is directly connected to the whole tunnel subnet and would create a competing route.

Test

Install the WireGuard client on a machine, import the configuration, and enable the tunnel. Then:

bash
# Server side
sudo wg show          # "latest handshake" should appear
powershell
# Windows client side
(Invoke-RestMethod "https://api.ipify.org?format=json").ip   # should be the VM's IP
ping 10.33.33.1

And a DNS leak test on dnsleaktest.com to confirm that the queries really go through the tunnel.

Cleaning up

powershell
Stop-AzVm -Name "vm-$prefix" -ResourceGroupName $rgName -Force
Remove-AzResourceGroup -Name $rgName -Force

Next step

We automate all of this with Terraform and a nice cloud-init configuration!

© 2026 Louis Lecat