Raspberry Pi mit WireGuard als VPN Server – bachmann-lan.de (2024)

Raspberry Pi mit WireGuard als VPN Server – bachmann-lan.de (1)

Aufbau und Setup

  • Raspberry Pi 3 mit Raspberry Pi OS Bullseye
  • der Pi hängt im LAN hinter dem Hauptrouter
  • LAN: 192.168.150.0/24 (PI: 192.168.150.200)
  • VPN Netz: 10.10.10.0/24 (VPN Server: 10.10.10.1, VPN Client1: 10.10.10.2, VPN Client2: 10.10.10.3)
  • Clients als Road Warrior (Android Smartphone und Windows 10 Notebook)

Installation

WireGuard auf dem Raspberry Pi installieren. (Zur Vereinfachung alles als root.)
WireGuard ist ab Linux-Kernel Version 5.6 im Kernel enthalten. Das Paket wireguard-dkms muss nicht mehr zwingend installiert werden.

$ modinfo wireguardfilename: /lib/modules/6.1.21-v7+/kernel/drivers/net/wireguard/wireguard.ko.xzalias: net-pf-16-proto-16-family-wireguardalias: rtnl-link-wireguardversion: 1.0.0author: Jason A. Donenfeld <Jason@zx2c4.com>description: WireGuard secure network tunnellicense: GPL v2srcversion: B5257F7548E6FF763F8DAFBdepends: libcurve25519-generic,udp_tunnel,ip6_udp_tunnel,libchacha20poly1305,ipv6,curve25519-neonintree: Yname: wireguardvermagic: 6.1.21-v7+ SMP mod_unload modversions ARMv7 p2v8

Raspberry Pi OS Bookworm

$ apt update$ apt upgrade$ apt install wireguard-tools iptables

Raspberry Pi OS Bullseye

$ apt update$ apt upgrade$ apt install wireguard-tools iptables

Raspbian Buster

$ apt-get update$ apt-get upgrade $ apt-get install raspberrypi-kernel-headers$ echo "deb http://deb.debian.org/debian/ unstable main" | tee --append /etc/apt/sources.list.d/unstable.list$ apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 04EE7237B7D453EC$ printf 'Package: *\nPin: release a=unstable\nPin-Priority: 150\n' | tee --append /etc/apt/preferences.d/limit-unstable$ apt-get update$ apt-get install wireguard $ reboot

Aktivieren von IPv4 forwarding in der /etc/sysctl.conf

# Uncomment the next line to enable packet forwarding for IPv4net.ipv4.ip_forward = 1

Den Pi neu starten und die Änderungen nochmal überprüfen.

$ sysctl net.ipv4.ip_forward net.ipv4.ip_forward = 1

Keys generieren

Die Erstellung der keys wird im Verzeichnis/etc/wireguarddurchgeführt.
Um sicherzustellen dass alle Dateien die richtigen Berechtigungen haben, muss die umask auf 077 gesetzt sein.

$ umask 077

Server Key generieren

private und public key für den Server

$ wg genkey | tee server-private.key | wg pubkey > server-public.key$ ls -l server*-rw------- 1 root root 45 Mar 31 10:38 server-private.key-rw------- 1 root root 45 Mar 31 10:38 server-public.key

Client Keys generieren

private und public key für die Clients

$ wg genkey | tee client1-private.key | wg pubkey > client1-public.key$ wg genkey | tee client2-private.key | wg pubkey > client2-public.key $ ls -l client*-rw------- 1 root root 45 Mar 31 10:41 client1-private.key-rw------- 1 root root 45 Mar 31 10:41 client1-public.key-rw------- 1 root root 45 Mar 31 10:41 client2-private.key-rw------- 1 root root 45 Mar 31 10:41 client2-public.key

Server Konfiguration

Dazu wird die Datei /etc/wireguard/wg0.conf erstellt. (Die neue VPN Schnittstelle ist wg0.)
Bei den iptables Regeln muss ggf. noch der Name der Netzwerkschnittstelle angepasst werden! Bei mir ist es eth0.

[Interface]Address = 10.10.10.1/24ListenPort = 51820PrivateKey = <server-private.key einfügen>PostUp = iptables -A FORWARD -i %i -j ACCEPT; iptables -A FORWARD -o %i -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADEPostDown = iptables -D FORWARD -i %i -j ACCEPT; iptables -D FORWARD -o %i -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE# Client1 Smartphone[Peer]PublicKey = <client1-public.key einfügen>AllowedIPs = 10.10.10.2/32# Client2 Notebook[Peer]PublicKey = <client2-public.key einfügen>AllowedIPs = 10.10.10.3/32

Client Konfiguration

Für jeden Client erstelle ich eine eigene Konfigurationsdatei.

/etc/wireguard/client1.conf

[Interface]PrivateKey = <client1-private.key einfügen>Address = 10.10.10.2DNS = 192.168.150.20[Peer]PublicKey = <server-public.key einfügen>Endpoint = vpn.your-public-server.net:51820AllowedIPs = 0.0.0.0/0, 192.168.150.0/24PersistentKeepalive = 25

/etc/wireguard/client2.conf

[Interface]PrivateKey = <client2-private.key einfügen>Address = 10.10.10.3DNS = 192.168.150.20[Peer]PublicKey = <server-public.key einfügen>Endpoint = vpn.your-public-server.net:51820AllowedIPs = 0.0.0.0/0, 192.168.150.0/24PersistentKeepalive = 25

Als DNS Server nutze ich meinen eigenen im lokalen LAN und mit 0.0.0.0/0 wird alles (der komplette traffic) durch das VPN geroutet.
Da ich mich hinter einem NAT befinde, wird mit PersistentKeepalive = 25 versucht die Verbindung aufrecht zu halten.

WireGuard starten

$ wg-quick up wg0[#] ip link add wg0 type wireguard[#] wg setconf wg0 /dev/fd/63[#] ip address add 10.10.10.1 dev wg0[#] ip link set mtu 1420 up dev wg0[#] iptables -A FORWARD -i wg0 -j ACCEPT; iptables -A FORWARD -o wg0 -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

WireGuard beim Systemstart automatisch laden.

$ systemctl enable wg-quick@wg0Created symlink /etc/systemd/system/multi-user.target.wants/wg-quick@wg0.service → /lib/systemd/system/wg-quick@.service.

WireGuard Status

Status der aktiven wg0 Schnittstelle.

$ wginterface: wg0 public key: 9EHJpPuO59RsFbejPZacyZ34TkT7Exas/ZKQsAfTU0U= private key: (hidden) listening port: 51820peer: mSOXtoZPSCoZL48u9IZGlpov5T4jAwZ7yhETTDosHVU= allowed ips: 10.10.10.2/32peer: 9q9PAKC5MUNaF4QmcH5hoqwpWoX2R4/KewvLi0SebmQ= allowed ips: 10.10.10.3/32

Firewall Konfiguration

DerUDPPort51820 muss an den internen VPN Server weitergeleitet werden. (hier für meinen MikroTik Router)

/ip firewall filteradd action=accept chain=forward dst-port=51820 protocol=udp/ip firewall natadd action=dst-nat chain=dstnat dst-port=51820 in-interface=wan protocol=udp to-addresses=192.168.150.200 to-ports=51820

Road Warrior – Android Client

Für Android nehme ich die offizielle WireGuard App aus dem Google Play Store.

Die Einstellungen der App kann mittels einer Datei, QR-Code oder manuell erfolgen. Auf dem Server wird ein QR-Code für den Client1 erstellt.

$ apt install -y qrencode

QR-Code für den Client1 erstellen.

$ qrencode -t ansiutf8 < client1.conf

Abscannen und fertig. Ohne die mühselige tipperei auf dem Smartphone.

Road Warrior – Windows Client

Für Windows nehme ich den offiziellen WireGuard Client.
Nach der Installation die (auf dem Server erzeugte) client2.conf importieren

und die VPN Verbindung aktivieren.

WireGuard updaten

Ist nich mehr notwendig.

Das hat mich schon viel Nerven gekostet, weil nach einem Kernel Update des Systems wireguard-dkms nicht mehr kompiliert und mit einer Fehlermeldung abbricht.
Sollte es mal Probleme geben, kann man folgendes versuchen.

# Variante A (nach jedem Kernel Update)$ dpkg-reconfigure wireguard-dkms# Variante B$ apt remove wireguard*$ apt install bc libncurses5-dev$ apt install wireguard

Mittlerweile kompiliere ich mir dir WireGuard Kernel Module und das wg Tool selber. Ist super einfach und schnell erledigt.
WireGuard nicht aus den Repos installieren und ggf. installierte Pakete vorher deinstallieren!

# Toolchain installieren$ apt-get install libmnl-dev libelf-dev linux-headers-$(uname -r) build-essential pkg-config# WireGuard empfiehlt den aktuellen Snapshot zu verwenden$ wget https://git.zx2c4.com/WireGuard/snapshot/WireGuard-0.0.2019mmdd.tar.xz# Kernel Module und das wg Tool kompilieren (dauert noch keine 2 Minuten)$ cd WireGuard-0.0.2019mmdd/src$ make# als root installieren$ make install# neustarten und fertig$ reboot

WireGuard Compiling the Kernel Module from Source | current Snapshot versions

Die installierte Version kann man gut mit dmesg herausfinden.

$ dmesg | grep wireguard[ 26.708518] wireguard: WireGuard 1.0.0 loaded. See www.wireguard.com for information.[ 26.708537] wireguard: Copyright (C) 2015-2019 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.# etwas kompakter$ dmesg | awk '/WireGuard/ {print $4,$5}'WireGuard 1.0.0

Fazit

Im Vergleich zu OpenVPN ist die Einrichtung und Konfiguration, trotz der langen Anleitung, in wenigen Minuten erledigt.
Der Tunnelaufbau und das Routing funktionierten auf Anhieb reibungslos und schnell.

17.05.2019 – Nach ca. 6 Wochen bin ich immer noch mehr als zufrieden! Schneller Tunnelaufbau und immer eine stabile Verbindung!

Links

Raspberry Pi mit WireGuard als VPN Server – bachmann-lan.de (2024)

FAQs

What is the best VPN server for Raspberry Pi? ›

NordVPN: The best VPN for Raspberry Pi. NordVPN has a command-line app for Debian that works on Raspberry Pi OS. Features include native ad blocking, malware filtering, servers in 60+ countries, a kill switch, double VPN, and obfuscation.

How do I setup a WireGuard VPN server? ›

Go to [VPN] > [VPN Server] > enable and click [WireGuard® VPN] > click add button. 4. For general devices like laptops or phones, you can just click the Apply button.

Can WireGuard run on Raspberry Pi? ›

In this tutorial, we setup a WireGuard client on a Raspberry Pi 4 running Raspbian OS Bullseye (64-bit). Before following this tutorial, you should already have a working WireGuard server running.

Which is more secure WireGuard or OpenVPN? ›

While WireGuard is generally faster, OpenVPN provides heavier security. The differences between these two protocols are also what make up their defining features. We've taken a closer look at each so you can really understand how they work for you.

Which server should I choose for VPN? ›

Selecting a VPN server location that is closer to you in range increases the likelihood of better speeds and optimal performance. That is because when your server location is far away, it normally takes time for the data to travel from the server to your device.

Is Raspberry Pi powerful enough for VPN? ›

Using a Raspberry Pi is a cheap way of setting up a virtual private network (VPN) that can stay online 24/7 without consuming a large amount of power. It's small and powerful enough to handle a few connections at a time making it great for private use at home.

Is WireGuard a good VPN? ›

Is the WireGuard VPN protocol secure? WireGuard is a very secure protocol. While it uses shorter cryptographic keys than some previous protocols, it still provides strong encryption. A longer key takes more time to crack, but it would still take millions of years to brute force WireGuard's encryption keys.

Is WireGuard VPN free? ›

WireGuard is originally open source and can be used for free, absolutely.

Which routers support WireGuard VPN? ›

What Are The Best WireGuard-Supporting Wi-Fi Routers?
  • Mesh Routers. $284.99.
  • TP-Link BE19000 Archer BE800 Wi-Fi 7 FlashRouter. $689.99 Regular Price $799.99.
  • Asus ROG Rapture GT6 Mesh FlashRouter. $514.99 Regular Price $599.99.

Can I use Raspberry Pi as VPN server? ›

Yes, and this tutorial provides a detailed, step-by-step process for using a Raspberry Pi—a cost-effective and compact computing solution—to set up a corporate VPN server. In many applications with concerns about data security and privacy, setting up a dedicated VPN (Virtual Private Network) server is essential.

How to install WireGuard VPN on Raspberry Pi? ›

Installing everything we will need for a wireguard connections is as simple as running:
  1. sudo apt-get install wireguard wireguard-tools. ...
  2. sudo add-apt-repository ppa:wireguard/wireguard sudo apt update sudo apt install wireguard wireguard-tools. ...
  3. sudo -i cd /etc/wireguard umask 077.

Why not to use WireGuard? ›

Why you shouldn't use WireGuard. WireGuard prioritizes speed, ease of use, and network security, but, some might say, at the expense of privacy. WireGuard does lack some standard features and practices many other protocols offer to enhance user privacy protection, such as: Dynamic IP addresses.

Does WireGuard hide IP? ›

As explained above WireGuard does not allocate a dynamic IP address to the VPN user. And, it indefinitely stores user IP addresses on the VPN server until the server reboots. So, there is no anonymity and privacy in WireGuard.

Which VPN is better than WireGuard? ›

In short, OpenVPN TCP is more effective at bypassing censorship than WireGuard, because WireGuard can only be used with UDP.

What is the best port for WireGuard? ›

The port used by the peer for WireGuard traffic. The default port is 51820 if left empty. If the Endpoint is empty, this value is ignored. An interval, in seconds, at which an empty packet is sent to the peer to keep the session active.

Can you host a VPN on a Raspberry Pi? ›

A Raspberry Pi VPN server is also capable of a lot. With your own hosted VPN server on Raspberry Pi, you can access your local computer network from any internet connection.

Is there a VPN for Raspberry Pi? ›

Meshnet lets you use your Raspberry Pi as a VPN server. Once set up, connected devices can route their online traffic through your Raspberry Pi to access the internet using its IP address — a great option if you're traveling abroad and want to access your resources as if you were at home.

How do I create a VPN server on my Raspberry Pi? ›

How to turn a Raspberry Pi into a VPN server
  1. Run PiVPN.
  2. Tell PiVPN whether you set up a static address (a DHCP reservation).
  3. Choose between OpenVPN or WireGuard.
  4. Select a communication protocol. ...
  5. Set the VPN port. ...
  6. Set the DNS provider.

Top Articles
Latest Posts
Article information

Author: Rev. Porsche Oberbrunner

Last Updated:

Views: 6336

Rating: 4.2 / 5 (73 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Rev. Porsche Oberbrunner

Birthday: 1994-06-25

Address: Suite 153 582 Lubowitz Walks, Port Alfredoborough, IN 72879-2838

Phone: +128413562823324

Job: IT Strategist

Hobby: Video gaming, Basketball, Web surfing, Book restoration, Jogging, Shooting, Fishing

Introduction: My name is Rev. Porsche Oberbrunner, I am a zany, graceful, talented, witty, determined, shiny, enchanting person who loves writing and wants to share my knowledge and understanding with you.