logo rss

Comment mettre en place un VPN WireGuard sur un serveur Debian 10 Buster

Intro

WireGuard est le nouveau VPN à la mode. Il est performant, sécurisé et facile à configurer.

Voyons comment le mettre en place dans un environnement Debian GNU/Linux serveur avec des clients Windows.

Schéma réseau

VPN WireGuard | Schéma Réseau
WireGuard Architecture
  • Serveur WireGuard :
    • OS : Debian gnu/linux 10 (buster)
    • Role : wireguard server + gateway
  • Client Windows :
    • OS : Windows 10

Serveur Debian (Partie I)

Installation

  • Ajouter les paquets non-stable :
root@host:~# echo 'deb http://deb.debian.org/debian/ unstable main' >> /etc/apt/sources.list.d/unstable.list
  • Utiliser les paquets non-stable seulement pour les packages non disponibles dans la default release :
root@host:~# printf 'Package: *\nPin: release a=unstable\nPin-Priority: 90\n' >> /etc/apt/preferences.d/limit-unstable
  • Installation de WireGuard :
root@host:~# apt update
root@host:~# apt install wireguard
  • Créer la clé privée et publique :
root@host:~# umask 077
root@host:~# wg genkey > wg-private.key
root@host:~# wg pubkey < wg-private.key > wg-public.key

Configuration

  • Copier le contenu de wg-private.key :
root@host:~# cat wg-private.key 
2GIURzIDBgI1Y+1Ei+i2C5kEOR53mH172MaidaVpD3M=
  • Éditer /etc/wireguard/wg0.conf et ajouter au niveau de la ligne PrivateKey la clé privée copiée précédemment :
# define the WireGuard service
[Interface]

# contents of file wg-private.key that was recently created
PrivateKey = 2GIURzIDBgI1Y+1Ei+i2C5kEOR53mH172MaidaVpD3M=

# UDP service port; 51820 is a common choice for WireGuard
ListenPort = 51820

Création de l'interface wg0

  • Éditer le fichier /etc/network/interfaces.d/wg0 :
# indicate that wg0 should be created when the system boots, and on ifup -a
auto wg0

# describe wg0 as an IPv4 interface with static address
iface wg0 inet static

        # static IP address 
        address 10.0.2.1/24

        # before ifup, create the device with this ip link command
        pre-up ip link add $IFACE type wireguard

        # before ifup, set the WireGuard config from earlier
        pre-up wg setconf $IFACE /etc/wireguard/$IFACE.conf

        # after ifdown, destroy the wg0 interface
        post-down ip link del $IFACE

Client Windows

Télécharger la dernière version du logiciel sur le site officiel https://wireguard.com/, et l'installer.

  • Ouvrir WireGuard VPN :
VPN WireGuard | Windows client, menu démarrer, programme wireguard
  • Ajouter un nouveau profil :
VPN WireGuard | Windows, client wireguard, ajouter un profil
  • Depuis Debian récupérer la clé publique :
root@host:~# cat wg-public.key
hlKy6azGCB0uVbCdkW8Htx23k57iWzOFJRLAYHTx5wU=
  • Configurer le profil :
VPN WireGuard | Windows client, éditer profil
[Interface]
PrivateKey = qPGa8vQc8lAxwyuaqXqUZwSEkFnLJI2LAglZ2aIFC0g=
Address = 10.0.2.2/24

[Peer]
PublicKey = hlKy6azGCB0uVbCdkW8Htx23k57iWzOFJRLAYHTx5wU=
AllowedIPs = 0.0.0.0/1, 128.0.0.0/1
Endpoint = 192.168.1.131:51820
  • Activer la connexion :
VPN WireGuard | Windows client, activer le profil

Serveur Debian (Partie II)

WireGuard

  • Activer WireGuard :
root@host:~# ifup wg0
  • Copier la clé publique Windows 10 :
VPN WireGuard | Windows client, copie de la clé publique
  • Depuis le serveur Debian, autoriser la clé publique du client :
root@host:~# wg set wg0 peer CLIENT_PUBLIC_KEY allowed-ips 0.0.0.0/0
  • Depuis Windows pinguer l'adresse ip de l'interface wg0 de notre serveur debian pour vérifier que ça fonctionne :
VPN WireGuard | Windows client, ping vers l'interface wg0
  • Pour que l'ajout des clients soit persistents et résiste à un redémarrage du serveur, éditer le fichier /etc/network/interfaces.d/wg0 et ajouter :
 # indicate that wg0 should be created when the system boots, and on ifup -a
auto wg0

# describe wg0 as an IPv4 interface with static address
iface wg0 inet static

        # static IP address 
        address 10.0.2.1/24

        # before ifup, create the device with this ip link command
        pre-up ip link add $IFACE type wireguard

        # before ifup, set the WireGuard config from earlier
        pre-up wg setconf $IFACE /etc/wireguard/$IFACE.conf

        # after ifdown, destroy the wg0 interface
        post-down ip link del $IFACE
        # allowed clients
	up wg set wg0 peer CLIENT01_PUBLIC_KEY allowed-ips 0.0.0.0/0
	up wg set wg0 peer CLIENT02_PUBLIC_KEY allowed-ips 0.0.0.0/0

Mode routeur

Activer l'ip forwarding

  • Éditer le fichier /etc/sysctl.conf :
net.ipv4.ip_forward = 1
  • Exécuter :
root@host:~# sysctl -p /etc/sysctl.conf
  • Vérifier que l'ip_forward est activé :
root@host:~# cat /proc/sys/net/ipv4/ip_forward
1

Règle iptables NAT

  • Identifier le nom de l'interface de son réseau local :
VPN WireGuard | Serveur Debian, ip addr sh
  • Entrer la règle masquerade suivante pour rendre le réseau interne joignable depuis notre client Windows :
root@host:~# iptables -t nat -A POSTROUTING -s 10.0.2.0/24 -o ens224 -j MASQUERADE
  • On pourra aussi ajouter ces règles de filtrage :
root@host:~# iptables -I INPUT -p udp -m udp --dport 51820 -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT
root@host:~# iptables -I OUTPUT -p udp -m udp --sport 51820 -m state --state ESTABLISHED,RELATED -j ACCEPT

Règles nftables NAT

  • Entrer les règles masquerade suivantes pour rendre le réseau interne joignable depuis notre client Windows :
root@host:~# nft add table ip NAT
root@host:~# nft add chain ip NAT my_masquerade '{ type nat hook postrouting priority 100; }'
root@host:~# nft add rule NAT my_masquerade ip saddr { 10.0.2.0/24 } oifname ens224 masquerade
  • On pourra aussi ajouter ces règles de filtrage :
root@host:~# nft add rule ip filter INPUT udp dport 51820 ct state new,established counter accept
root@host:~# nft add rule ip filter OUTPUT udp sport 51820 ct state established counter accept

Test depuis Windows

  • Pinguer n'importe quel hôte interne :
VPN WireGuard | Windows client, ping vers hôte interne

Dépannage

  • Activer le mode debug de WireGuard :
root@host:~# modprobe wireguard && echo module wireguard +p > /sys/kernel/debug/dynamic_debug/control
  • Voir le syslog en temps réel :
root@host:~# tail -f /var/log/syslog
  • Désactiver le mode debug de WireGuard :
root@host:~# modprobe wireguard && echo module wireguard -p > /sys/kernel/debug/dynamic_debug/control

Sources

Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Contact :

adresse mail de contact