Serveur WireGuard¶
1. Installation¶
sudo apt update
sudo apt install wireguard
2. Génération des clés¶
wg genkey | sudo tee /etc/wireguard/privatekey | wg pubkey | sudo tee /etc/wireguard/publickey
Afficher la clé privée (nécessaire pour la config) :
cat /etc/wireguard/privatekey
Identifier l'interface réseau active :
ip -o -4 route show to default | awk '{print $5}'
3. Configuration /etc/wireguard/wg0.conf¶
sudo nano /etc/wireguard/wg0.conf
[Interface]
Address = 10.0.0.1/24
ListenPort = 51820
PrivateKey = CLEF_PRIVEE_SERVEUR
PostUp = iptables -A FORWARD -i %i -j ACCEPT; iptables -t nat -A POSTROUTING -o INTERFACE_RESEAU -j MASQUERADE
PostDown = iptables -D FORWARD -i %i -j ACCEPT; iptables -t nat -D POSTROUTING -o INTERFACE_RESEAU -j MASQUERADE
[Peer]
PublicKey = CLE_PUBLIQUE_CLIENT_1
AllowedIPs = 10.0.0.2/32
[Peer]
PublicKey = CLE_PUBLIQUE_CLIENT_2
AllowedIPs = 10.0.0.3/32
| Variable | Comment l'obtenir |
|---|---|
CLEF_PRIVEE_SERVEUR |
cat /etc/wireguard/privatekey |
INTERFACE_RESEAU |
ip -o -4 route show to default \| awk '{print $5}' |
CLE_PUBLIQUE_CLIENT_X |
Fournie par chaque client |
Protéger les fichiers sensibles :
sudo chmod 600 /etc/wireguard/{privatekey,wg0.conf}
4. IP Forwarding¶
sudo nano /etc/sysctl.conf
net.ipv4.ip_forward=1
Vérification :
cat /proc/sys/net/ipv4/ip_forward
# Résultat attendu : 1
5. Démarrage¶
# Démarrer
sudo wg-quick up wg0
# Activer au démarrage du système
sudo systemctl enable wg-quick@wg0.service
| Action | Commande |
|---|---|
| Démarrer | sudo wg-quick up wg0 |
| Arrêter | sudo wg-quick down wg0 |
| Statut | sudo wg show |
| Activer au boot | sudo systemctl enable wg-quick@wg0.service |
Serveur derrière un routeur / box
Pensez à rediriger le port UDP 51820 vers l'IP interne du serveur. L'Endpoint côté client sera alors l'IP publique de la box.