Wireguard with systemd and nftables
Hi;
I am trying to configure Wireguard VPN using systemd.networkd on a Server with NFTables as a firewall.
These are the files of my configuration:
I am still not able to connect a client successfully nor get internet access.
Can you point me in the right direction?
Thank you in advance.
I am trying to configure Wireguard VPN using systemd.networkd on a Server with NFTables as a firewall.
These are the files of my configuration:
/etc/systemd/network/def_if.network
[Match]
MACAddress=52:54:00:12:12:12
[Network]
Description=Underlying main ethernet interface
Bridge=br0
LinkLocalAddressing=no
/etc/systemd/network/br0.netdev
[NetDev]
Description=Main bridge interface
Name=br0
Kind=bridge
/etc/systemd/network/br0.network
[Match]
Name=br0
[Link]
MACAddress=52:54:00:12:12:12
[Network]
Description=Main bridge interface
Address=2a01:4f8:10a:68b::15/64
Address=88.99.30.87/27
Gateway=fe80::1
Gateway=148.251.52.1
IPv6AcceptRA=no
IPForward=yes
IPMasquerade=yes
/etc/systemd/network/wg0.netdev
[NetDev]
Name=wg0
Kind=wireguard
Description=Wireguard VPN
[WireGuard]
PrivateKey=sKEIBk773uHzTMEVayvzC0n7UslviO8yj/j7dU7+ANWc=
ListenPort=51820
[WireGuardPeer]
PublicKey=ImVcJRxLrjTEJ62rLwLEGxgT5E4iOA5zth9NDeappA0=
AllowedIPs=0.0.0.0/0,::/0
EndPoint=192.168.129.129/32 [fdf6:fd:f6:fdf6::aa13]/128
PresharedKey=OdGpK/xuTJtiH6Nc12xEQ99sooFJz3x8tveU84czrQ8=
/etc/systemd/network/wg0.network
[Match]
Name=wg0
[Network]
Address=192.168.129.220/24
Address=fdf6:fd:f6:fdf6::3003/72
DNS=192.168.129.129
IPForward=yes
IPMasquerade=yes
/etc/nftables/inet-filter.nft
table inet filter {
chain input {
type filter hook input priority 0; policy drop
ct state invalid drop
ct state established accept
ct state related accept
iifname lo accept;
iifname wg0 accept
ip protocol icmp icmp type { echo-request, destination-unreachable } counter packets 0 bytes 0 accept;
ip6 nexthdr ipv6-icmp accept
icmpv6 type { nd-neighbor-advert, nd-neighbor-solicit, nd-router-advert, destination-unreachable} ip6 hoplimit 255 counter accept
udp dport { 51820 } accept;
}
chain forward {
type filter hook forward priority 0; policy drop
iifname br0 oifname wg0 accept comment "wireguard wg0"
iifname wg0 oifname br0 accept comment "Wireguard wg0"
log
}
chain output {
type filter hook output priority 0; accept
oifname wg0 accept
oifname lo accept
log
}
I am still not able to connect a client successfully nor get internet access.
Can you point me in the right direction?
Thank you in advance.
Please also mark the comments that contributed to the solution of the article
Content-ID: 92586419613
Url: https://rootdb.com/forum/wireguard-with-systemd-and-nftables-92586419613.html
Printed on: March 28, 2025 at 18:03 o'clock
21 Comments
Latest comment
The above configuration is very weired and totally different from a standard Wireguard configuration. Why didnt you use the classical approach with a text config file in /etc/wireguard which is fully integrated into systemd as well?? It makes a WG setup much easier and quicker.
All steps are described HERE. Unfortunately in German but maybe a translation tool can help here.
You should also start in a strategic way which means setup your VPN connection first without nftables running to make sure you do not step into firewall issues.
In case the VPN connection runs fine you should activate the firewall. So you can make sure any possible malfunction could only be a firewall misconfiguration and NOT the VPN itself.
Your correct nftables setup is based on how your WG server looks like (interface etc.). You can find some good examples here:
https://www.procustodibus.com/blog/2021/11/wireguard-nftables/
But keep in mind: First VPN checks always without firewall.
All steps are described HERE. Unfortunately in German but maybe a translation tool can help here.
You should also start in a strategic way which means setup your VPN connection first without nftables running to make sure you do not step into firewall issues.
In case the VPN connection runs fine you should activate the firewall. So you can make sure any possible malfunction could only be a firewall misconfiguration and NOT the VPN itself.
Your correct nftables setup is based on how your WG server looks like (interface etc.). You can find some good examples here:
https://www.procustodibus.com/blog/2021/11/wireguard-nftables/
But keep in mind: First VPN checks always without firewall.
I have seen that this thread is marked as solved, but it is sadly not
Due to good reasons this can ONLY be done by YOU as the threadowner!If you do not want it to be marked as "solved" YOU can always change it by yourself using the Edit/Bearbeiten button!
I cannot use the peer as a gateway or dns.
What do you exactly mean with "gateway"? In general WG has only 2 options as all VPNs have it:- Split Tunneling = only relevant traffic is routed into the tunnel which is done by WG's own crypto routing and declared by the IP networks under "AllowedIPs"
- Gateway Redirect = All traffic is by default routed into the tunnel (default gateway) in case the peer is active and established.
Gateway redirect implies also a DNS traffic redirect, because it includes of course TCP and UDP 53 traffic as well as all other traffic. Here you have to take care by yourself that your configured DNS servers are fully reachable over the tunnel.
In case of split tunneling you declare an alternative DNS server who will be active when the peer is established with the extra "dns" parameter.
"ip r" as well as dig and host are your best friends (dnsutils, bindutils) to check this.
With ip r you cannot see the wireguard rules!!
Are you sure? If you issue a wg-quick down wg0
all WG related routing entries are gone in the main table even with the short ip r
. So i guess ip r
shows some basic WG routing rules as well. The longer version just shows some more detailed local connected interface, broad- and multicast rules on top but the major rules are shown with ip r
as well.