Why WireGuard Fails on LXC NAT VPS (and How to Fix It)
DEV Community

Why WireGuard Fails on LXC NAT VPS (and How to Fix It)

You've got a cheap NAT VPS - shared IPv4, a small range of forwarded ports, maybe 192 MB of RAM inside an LXC container - and you want your own WireGuard VPN on it. You run the popular install script and get this: RTNETLINK answers: Not supported …or a script that refuses to run with LXC is not supported.

This post explains why that happens, how to tell in one line whether your box can do it, and the minimal setup that works on a NAT VPS with only a handful of forwarded ports. I tested every step on a 192 MB LXC container on an AMD EPYC host - screenshots below.

Why WireGuard fails inside LXC

WireGuard lives in the Linux kernel. A VM (KVM) has its own kernel and can load the module itself. An LXC container shares the host's kernel and isn't allowed to load modules. So it depends entirely on the host:

Host has wireguard module loaded? Result in the container Yes Kernel WireGuard works at full speed No RTNETLINK answers: Not supported

The user-space alternatives (wireguard-go, boringtun) need /dev/net/tun passed into the container - which many hosts don't do either.

If you run the host yourself (Incus/LXD), the fix is two commands on the host:

modprobe wireguard && echo wireguard >> /etc/modules-load.d/wireguard.conf
incus profile device add default tun unix-char source = /dev/net/tun path = /dev/net/tun mode = 0666

If you don't run the host, ask your provider to load the wireguard module for containers.

The one-line test

Run this inside your container:

ip link add wgtest type wireguard && ip link del wgtest && echo "WIREGUARD OK"

WIREGUARD OK β†’ continue. Not supported β†’ the host doesn't allow it, no amount of apt installing will fix that.

Setup on a NAT VPS

The only NAT-specific part: WireGuard must listen on one of your forwarded UDP ports. In my case the range is 20001-20019, so I use 20001. Replace it with yours.

  1. Tools and keys

Only the user-space tools - the kernel part comes from the host, so don't install wireguard-dkms.

apt update && apt install -y wireguard-tools iptables qrencode curl
mkdir -p /etc/wireguard && cd /etc/wireguard && umask 077
wg genkey | tee server.key | wg pubkey > server.pub
wg genkey | tee phone.key | wg pubkey > phone.pub
  1. Forwarding
cat > /etc/sysctl.d/99-wireguard.conf << 'EOF'
net.ipv4.ip_forward = 1
net.ipv6.conf.all.forwarding = 1
net.ipv6.conf.eth0.accept_ra = 2
EOF
sysctl --system > /dev/null

The accept_ra = 2 line matters: turning on IPv6 forwarding normally makes Linux ignore router advertisements, which can silently kill your VPS's own IPv6.

  1. Server config
cat > /etc/wireguard/wg0.conf << EOF
[ Interface ]
Address = 10.66.66.1/24, fd66:66:66::1/64
ListenPort = 20001
PrivateKey = $( cat server.key )
PostUp = iptables -t nat -A POSTROUTING -s 10.66.66.0/24 -j MASQUERADE ; ip6tables -t nat -A POSTROUTING -s fd66:66:66::/64 -j MASQUERADE
PostDown = iptables -t nat -D POSTROUTING -s 10.66.66.0/24 -j MASQUERADE ; ip6tables -t nat -D POSTROUTING -s fd66:66:66::/64 -j MASQUERADE
[ Peer ]
PublicKey = $( cat phone.pub )
AllowedIPs = 10.66.66.2/32, fd66:66:66::2/128
EOF
chmod 600 wg0.conf
systemctl enable --now wg-quick@wg0

Clients get private IPv4 + ULA IPv6 addresses and are masqueraded out, so the tunnel works for both IPv4 and IPv6 sites even though the IPv4 is shared.

  1. Client config as a QR code
cat > phone.conf << EOF
[ Interface ]
PrivateKey = $( cat phone.key )
Address = 10.66.66.2/32, fd66:66:66::2/128
DNS = 1.1.1.1, 2606:4700:4700::1111
[ Peer ]
PublicKey = $( cat server.pub )
Endpoint = $( curl -4 -s ifconfig.co ) :20001
AllowedIPs = 0.0.0.0/0, ::/0
PersistentKeepalive = 25
EOF
qrencode -t ansiutf8 < phone.conf

Scan it in the WireGuard app ( + β†’ Scan from QR code ). The QR contains a private key - don't post it anywhere.

PersistentKeepalive = 25 is worth keeping on NAT setups: it stops the connection-tracking entries on both sides from expiring while the tunnel is idle.

Does it work?

On the server:

wg show

A recent latest handshake and growing transfer = done. Browsing to ifconfig.co from the phone now shows the VPS address:

Troubleshooting

No handshake at all

The port is wrong or blocked. ListenPort and the port in Endpoint must match and be inside your forwarded range. If you run ufw in the container: ufw allow 20001/udp.

Handshake works, but no websites load

Forwarding or NAT is missing. Check sysctl net.ipv4.ip_forward returns 1 and iptables -t nat -S shows the MASQUERADE rule.

Some sites load, others hang forever

Classic MTU issue. Add MTU = 1280 under [Interface] in the client config and reconnect.

ping google.com hangs on the VPS

ping prefers IPv6, and some routes filter ICMPv6 echo. Try ping -4 google.com - if that works, your network is fine.

Takeaways

On LXC, WireGuard support is a host decision - test with ip link add … type wireguard before debugging anything else. On NAT, the only change is picking a forwarded UDP port as ListenPort. Keep accept_ra = 2 when enabling IPv6 forwarding, and PersistentKeepalive on the client. 192 MB of RAM is plenty - WireGuard itself uses a few MB.

Questions or a setup that behaves differently? Drop it in the comments.

Read on DEV Community ↗ ← Back to News

Comments

No comments yet. Start the discussion.