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:

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
Connect-AzAccount
Set-AzContext -Subscription "Root subscription"
New-AzResourceGroup -Name "rg-wireguard-lab" -Location "switzerlandnorth"ssh-keygen -t ed25519 -f "$HOME\.ssh\wglab" -C "wglab"Variables
$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").ipNSG
$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, $ruleSshThe NSG blocks all inbound traffic by default (implicit rule at priority 65500), so these two rules alone define the exposed surface.
VNet and subnet
$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 $subnetPublic IP and network card
$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
# -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 $vmConfigAllow two to three minutes for the creation.
Check
$fqdn = (Get-AzPublicIpAddress -Name "pip-$prefix" -ResourceGroupName $rgName).DnsSettings.Fqdn
ssh -i "$HOME\.ssh\wglab" "$adminUser@$fqdn"Configuring the VM
Installation
sudo apt update
sudo apt install -y wireguard wireguard-tools iptables qrencodeNothing 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:
lsmod | grep wireguard # empty for now, the module loads on demandGenerating the server keys
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.pubServer configuration
First get the name of the outgoing interface:
ip -4 route show defaultIt will most likely be eth0. Then:
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.confDirective by directive:
- Address: the server’s IP inside the tunnel. The
/24tellswg-quickto add a route to the whole10.33.33.0/24throughwg0. It is a range completely independent from the Azure VNet’s10.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:
pingworks,curlon a small page works, but large transfers freeze. - %i: replaced by
wg-quickwith the interface name, herewg0. - The FORWARD rules: Ubuntu leaves the
FORWARDpolicy atACCEPTby 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.xleave througheth0with the VM’s public IP, and the kernel’s connection tracking handles the way back.
Enabling forwarding
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 1IP 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
sudo systemctl enable --now wg-quick@wg0
sudo systemctl status wg-quick@wg0
sudo wg showwg show should display the interface with its public key and listening port, with no peer yet.
Check that the interface is up:
ip addr show wg0
sudo iptables -t nat -L POSTROUTING -n -v # the MASQUERADE rule should be thereA 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.
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:
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
EOFThen reload without cutting existing connections:
sudo wg syncconf wg0 <(sudo wg-quick strip wg0)
sudo wg showThe client configuration
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 desktopPersistentKeepalive = 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:
# Server side
sudo wg show # "latest handshake" should appear# Windows client side
(Invoke-RestMethod "https://api.ipify.org?format=json").ip # should be the VM's IP
ping 10.33.33.1And a DNS leak test on dnsleaktest.com to confirm that the queries really go through the tunnel.
Cleaning up
Stop-AzVm -Name "vm-$prefix" -ResourceGroupName $rgName -Force
Remove-AzResourceGroup -Name $rgName -ForceNext step
We automate all of this with Terraform and a nice cloud-init configuration!