# Wireguard VPN erstellen

<p class="callout info">Ganz einfach Möglichkeit ist es, das VPN als PiVPN zu installieren</p>

### Installation

```bash
curl -L https://install.pivpn.io | bash
```

Yes, that's it! It is \*almost\* that simple.  
To elaborate a little more, you will want to [install Raspberry Pi OS Lite](https://www.raspberrypi.org/documentation/installation/installing-images/README.md) on a Raspberry pi, we strongly recommend using the latest [Raspberry Pi OS Lite](https://www.raspberrypi.org/software/operating-systems/#raspberry-pi-os-32-bit) image but the normal Raspberry Pi OS image will work as well, preferably enable ssh access and then begin.  
After install, you may need to open a port on your router.  
There is a (now slightly outdated) guided walkthrough of the install available [here](https://www.sitepoint.com/setting-up-a-home-vpn-using-your-raspberry-pi/).  
More information is also available on the [PiVPN GitHub](https://github.com/pivpn/pivpn)

### Konfiguration eines Clients

##### Generating Private and Public Keys

WireGuard works by encrypting the connection using a pair of cryptographic keys. The key pair is used by passing the public key to the other party, which can then encrypt its message so that it can only be decrypted with the corresponding private key. To secure two-way communication, each side must have its own private and public keys, since each pair provides only one-way messaging.

Generate a client public and private key pair by running the following command:

`pivpn -add`

die public keys befinden sich im Ordner `/home/pivpn/config`

## Wireguard Client konfigurieren

<p class="callout info">Diese Vorgehensweise für einen Wireguard Client funkioniert und wurde erfolgreich auf Kali getestet</p>

```bash
sudo apt install wireguard resolvconf # Software installieren

sudo cp /media/kali/persitence/tails.conf /etc/wireguard/wg0-client-01.conf # Configdatei speichern

# Berechtigungen zuordnen
sudo chown root:root /etc/wireguard/wg0-client-01.conf
sudo chmod 644 /etc/wireguard/wg0-client-01.conf
sudo chmod 755 /etc/wireguard

# Starten
sudo wg-quick up /etc/wireguard/wg0-client-01.conf

# Stoppen
sudo wg-quick down /etc/wireguard/wg0-client-01.conf

#Status
sudo wg
```

## Wireguard automatisch starten

Um eine **WireGuard-Verbindung nach dem Systemstart automatisch und ohne erneute Eingabe des sudo-Passworts** herzustellen, gibt es eine etablierte Methode über systemd. Dafür sind Root-Rechte *bei der Einrichtung* einmalig nötig, danach aber nicht mehr bei jedem Start. Das Einrichten geht wie folgt:

**1. Konfiguration absichern**

Lege deine WireGuard-Konfiguration als z. B. `/etc/wireguard/wg0.conf` an und prüfe die Berechtigungen (`root:root`, nur für root lesbar)

**2. Systemd-Service aktivieren**

Aktiviere den automatischen Start mittels:

```bash
sudo systemctl enable wg-quick@wg0.service 
```

<div class="w-full md:max-w-[90vw]" id="bkmrk-"><div class="codeWrapper text-textMainDark selection:text-super selection:bg-super/10 bg-offset my-md relative flex flex-col rounded font-mono text-sm font-thin"></div></div>Ersetze `wg0` durch den Namen deiner Konfigurationsdatei ohne `.conf`.

Optional: Starte WireGuard sofort mi

```bash
sudo systemctl start wg-quick@wg0 
```

Ab sofort baut das System beim Booten die Verbindung ohne Passwortabfrage auf – das Service läuft mit Root-Rechten im Hintergrund

<div class="w-full md:max-w-[90vw]" id="bkmrk--1"><div class="codeWrapper text-textMainDark selection:text-super selection:bg-super/10 bg-offset my-md relative flex flex-col rounded font-mono text-sm font-thin"></div></div>**3. Kein sudo beim Verbindungsaufbau nach Boot erforderlich**

- Nach der Einrichtung ist **keine** weitere Passworteingabe (sudo) nötig, sofern die Schnittstelle beim Systemstart aktiviert wird
- Möchtest du WireGuard auch manuell ohne Passwort (z. B. per Script oder Terminal) steuern, kann *zusätzlich* der folgende Schritt nötig werden: