# 2 - Via l'utilisation du package Linux

### Via l'utilisation du package Wireguard

##### <span style="text-decoration: underline;">Sur le client</span>

<span style="text-decoration: underline;">**1/**</span> Tout d'abord vous devez installer wireguard sur la machine :

```bash
sudo apt-get install wireguard
```

Puis se déplacer dans le répertoire de wireguard, par défaut le dossier est vide :

```bash
root@linux: cd /etc/wireguard/
root@linux: ls -al
total 28
drwx------   2 root root  4096 juin  13 12:03 ./
drwxr-xr-x 166 root root 12288 juin  13 11:24 ../
```

<span style="text-decoration: underline;">**2/**</span> Maintenant nous allons générer la clé privé et la clé publique :

```bash
wg genkey | tee privatekey | wg pubkey | tee publickey
```

Nous avons maintenant 2 fichiers dans le dossier wireguard :

```bash
root@linux: wg genkey | tee privatekey | wg pubkey | tee publickey
WKtF71pM6wObswaXkbxJgwPhk8a6lqrPb9oKFjaGOmM=
root@linux: ls -al
total 28
drwx------   2 root root  4096 juin  14 15:55 .
drwxr-xr-x 166 root root 12288 juin  13 11:24 ..
-rw-r--r--   1 root root    45 juin  14 15:55 privatekey
-rw-r--r--   1 root root    45 juin  14 15:55 publickey
```

**<span style="text-decoration: underline;">3/</span>** Pour continuer la configuration il faut noter le contenu des 2 clés générés :

```
root@linux: cat privatekey 
iApdu05JqNGSp/r7PSpJ5Zqxs4kWSR6qYv9onitvsmo=

root@linux: cat publickey 
WKtF71pM6wObswaXkbxJgwPhk8a6lqrPb9oKFjaGOmM=
```

<p class="callout danger">Attention ne partagez jamais votre clé privé !!</p>

Clé privé : iApdu05JqNGSp/r7PSpJ5Zqxs4kWSR6qYv9onitvsmo=  
Clé publique : WKtF71pM6wObswaXkbxJgwPhk8a6lqrPb9oKFjaGOmM=

<span style="text-decoration: underline;">**4/a)**</span> Création du fichier de configuration Wireguard client :

```bash
touch wg0.conf
```

<span style="text-decoration: underline;">**4/b)**</span> Préparer dans un bloc note la configuration suivante :

Il faut bien veiller à remplacer les variables :

- `<private_key_client>` -&gt; Clé privé généré précédemment
- `<range_ip_vpn>` -&gt; Adresse IP VPN du client
- `<public_key_server>` -&gt; Clé publique du serveur (A récupérer sur le serveur)
- `<ip_server>` -&gt; Adresse IP du serveur
- `<port_server>` -&gt; Port écoutant sur le serveur

```bash
[Interface]
PrivateKey = <private_key_client>
Address = <range_ip_vpn>

[Peer]
###Public of the WireGuard VPN Server
PublicKey = <public_key_server>

### IP and Port of the WireGuard VPN Server
Endpoint = <ip_server>:<port_server>

### Allow all traffic
AllowedIPs = 0.0.0.0/0
```

<span style="text-decoration: underline;">**4/c)**</span> Editer le fichier de configuration en collant la configuration complété :

```bash
nano wg0.conf
# ou
vim wg0.conf
```

Ce qui donne :

```bash
[Interface]
PrivateKey = iApdu05JqNGSp/r7PSpJ5Zqxs4kWSR6qYv9onitvsmo=
Address = 192.168.10.15/24

[Peer]
###Public of the WireGuard VPN Server
PublicKey = <public_key_server>

### IP and Port of the WireGuard VPN Server
Endpoint = vpn.domaine-name.com:51820

### Allow all traffic
AllowedIPs = 0.0.0.0/0
```

<p class="callout info">Le reste de la configuration continue sur la partie serveur</p>

##### <span style="text-decoration: underline;">Sur le serveur</span>

> `wg0` est l'interface référençant tous les appareils pouvant se connecter au VPN.

Préparer la commande suivante :

```bash
wg set wg0 peer <public_key_client> allowed-ips <adress_ip_vpn_client>
```

Ce qui donne :

```bash
wg set wg0 peer WKtF71pM6wObswaXkbxJgwPhk8a6lqrPb9oKFjaGOmM= allowed-ips 192.168.10.15
```

Puis exécuter la commande.

Pour être sur du bon fonctionnement de la commande précédente, nous pouvons lister les clients enregistré dans le fichier de conf du serveur :

```bash
wg show wg0
```

Maintenant il faut confirmer et enregistrer la modification du fichier de configuration :

```bash
wg-quick save wg0
```

#####   


##### <span style="text-decoration: underline;">Sur le client</span>

Nous avons créer le client et nous l'avons enregistré dans la configuration serveur. Maintenant il nous reste plus qu'a démarrer le VPN sur le client :

```
root@linux: wg-quick up wg0
[#] ip link add wg0 type wireguard
[#] wg setconf wg0 /dev/fd/63
[#] ip -4 address add 10.13.13.6/32 dev wg0
[#] ip link set mtu 1420 up dev wg0
[#] wg set wg0 fwmark 51820
[#] ip -4 route add 0.0.0.0/0 dev wg0 table 51820
[#] ip -4 rule add not fwmark 51820 table 51820
[#] ip -4 rule add table main suppress_prefixlength 0
[#] sysctl -q net.ipv4.conf.all.src_valid_mark=1
[#] nft -f /dev/fd/63
```

**Vérification du fonctionnement du VPN côté client :**

```
ifconfig
```

**Vérification du fonctionnement du nouveau client côté serveur :**

```bash
wg show wg0
```