(gelöst) VPN Wireguard
von scriptorius- SNIPPET_TEXT:
-
- (1) Ddclient Installation auf Server
- Bei desec.io einen dyndns account erstellt; dann:
- # apt install curl -y
- # apt install ddclient -y
- > es erfolgt eine Abfrage, am Ende sollte es folgendermaßen konfiguriert sein:
- # nano /etc/ddclient.conf
- >
- protocol=dyndns2
- usev6=if, if=enp3s0
- ssl=yes
- use=cmd, cmd='curl https://checkipv6.dedyn.io/'
- server=update6.dedyn.io
- login=xxx.dedyn.io
- password='***'
- xxx.dedyn.io
- # nano /etc/default/ddclient
- >
- …
- run_dhclient="false"
- …
- run_ipup="false"
- …
- run_daemon="true"
- …
- daemon_interval="300"
- # service ddclient restart
- # ddclient -force
- # cd /root/
- # crontab -e
- > # ganz unten einfügen
- 05 08 * * * /usr/sbin/ddclient --force
- ---------------------------------------------
- (2) Wireguard Installation
- # nano /etc/apt/sources.list.d/unstable.list
- >
- deb http://deb.debian.org/debian/ unstable main
- # nano /etc/apt/preferences.d/limit-unstable
- >
- Package: *
- Pin: release a=unstable
- Pin-Priority: 90
- # apt update
- # apt install wireguard --dry-run
- (etwas über 60 Pakete sollten es ; dann:)
- # apt install wireguard -y
- ---------------------------------------------------
- (3) Schlüssel erzeugen für Server-Konfiguration und den ersten Client (Smartphone)
- # cd /etc/wireguard
- # umask 077
- # wg genkey > server.key # erstellen
- # wg genkey > client1.key # erstellen
- Server-Schlüssel:
- Geheim:
- # cat /etc/wireguard/server.key # anzeigen
- # cat /etc/wireguard/server.key | wg pubkey # anzeigen, muss nicht eigens erstellt werden
- Client-Schlüssel:
- Geheim:
- # cat /etc/wireguard/client1.key # anzeigen
- Öffentlich:
- # cat /etc/wireguard/client1.key | wg pubkey # anzeigen, muss nicht eigen erstellt werden
- -------------------------------------------------------------------
- (4) Erstellen der Server- und Clientkonfigfiles
- Anfang: Server-Konfiguration auf Server
- # cat /etc/wireguard/server.key
- > angezeigten privaten Server-Schlüssel kopieren
- # touch /etc/wireguard/wg0.conf
- # nano /etc/wireguard/wg0.conf
- >
- [Interface]
- Address = 10.66.66.1/24, fd42:42:42::1/64
- PostUp = iptables -A FORWARD -i wg0 -j ACCEPT; iptables -t nat -A POSTROUTING -o enp3s0 -j MASQUERADE; ip6tables -A FORWARD -i wg0 -j ACCEPT; ip6tables -t nat -A POSTROUTING -o enp3s0 -j MASQUERADE
- PostDown = iptables -D FORWARD -i wg0 -j ACCEPT; iptables -t nat -D POSTROUTING -o enp3s0 -j MASQUERADE; ip6tables -D FORWARD -i wg0 -j ACCEPT; ip6tables -t nat -D POSTROUTING -o enp3s0 -j MASQUERADE
- ListenPort = 51820
- PrivateKey = >privater Server-Schlüssel hier eintragen<
- Client-Konfiguration auf Server:
- # mkdir /etc/wireguard/clients
- # touch /etc/wireguard/clients/client1.conf
- # cat /etc/wireguard/client1.key
- # cat /etc/wireguard/server.key | wg pubkey
- # nano /etc/wireguard/clients/client1.conf
- >
- [Interface]
- PrivateKey = >privater Client-Schlüssel<
- Address = 10.66.66.2/24, fd42:42:42::2/64
- DNS = 176.103.130.130,176.103.130.131
- [Peer]
- PublicKey = >öffentlicher Server-Schlüssel<
- Endpoint = xxx.dedyn.io:51820
- AllowedIPs = 0.0.0.0/0, ::/0
- Abschluss: Server-Konfiguration auf Server
- # nano /etc/wireguard/wg0.conf
- > # unter [Interface] einfügen
- [Peer]
- PublicKey = >öffentlicher Client-Schlüssel<
- AllowedIPs = 10.66.66.2/32, fd42:42:42::2/128
- ------------------------------------------------------
- (5) Wireguard-Konfiguration auf server starten und Start automatisieren
- # chown -v root:root /etc/wireguard/wg0.conf
- # chmod -v 600 /etc/wireguard/wg0.conf
- # wg-quick up wg0
- # systemctl enable wg-quick@wg0.service
- ANMERKUNG:
- Ab jetzt vor jeder Konfiguration:
- # wg-quick down wg0
- Nach jeder Konfiguration:
- # wg-quick up wg0
- ----------------------------------------------------------
- (6) Forwarding auf Server konfigurieren
- # touch /etc/sysctl.d/wireguard.conf
- # nano /etc/sysctl.d/wireguard.conf
- >
- net.ipv4.ip_forward=1
- net.ipv6.conf.all.forwarding=1
- # sysctl -p /etc/sysctl.d/wireguard.conf
- # sysctl --system
- [Die folgenden Zeilen, die bereit oben in /etc/wireguard/wg0.conf eingefügt wurden sind für ein forwarding notwendig:
- PostUp = iptables -A FORWARD -i wg0 -j ACCEPT; iptables -t nat -A POSTROUTING -o enp3s0 -j MASQUERADE; ip6tables -A FORWARD -i wg0 -j ACCEPT; ip6tables -t nat -A POSTROUTING -o enp3s0 -j MASQUERADE
- PostDown = iptables -D FORWARD -i wg0 -j ACCEPT; iptables -t nat -D POSTROUTING -o enp3s0 -j MASQUERADE; ip6tables -D FORWARD -i wg0 -j ACCEPT; ip6tables -t nat -D POSTROUTING -o enp3s0 -j MASQUERADE]
- ------------------------------------------------------
- (7) In der fritzbox Route setzen
- Mögliche Route: fritzbox > Heimnetz > Netzwerk > Netzwerkeinstellungen > ipv4-Routen:
- 10.66.66.0 - 255.255.255.0 - 192.168.178.45
- --------------------------------------------------
- (8) Auf dem WG-Server die Rückroute setzen:
- # ip route add 192.168.178.0/24 via 10.66.66.1 dev wg0 mtu 1420
- --------------------------------------------------
- (9) QR-Coder für Smartphone-Client erzeugen
- # apt install qrencode -y
- # cat /etc/wireguard/clients/client1.conf | qrencode -t ansiutf8
- -----------------------------------------------------
- (10) DNS-Server auf dem WG-Server
- # apt install dnsutils
- # apt install unbound unbound-host -y
- # curl -o /var/lib/unbound/root.hints https://www.internic.net/domain/named.cache
- # chown unbound:unbound /var/lib/unbound/root.hints
- # nano /etc/unbound/unbound.conf
- > # unten einfügen nach: include: "/etc/unbound/unbound.conf.d/*.conf"
- server:
- num-threads: 4
- #Enable logs
- verbosity: 1
- #list of Root DNS Server
- root-hints: "/var/lib/unbound/root.hints"
- #Respond to DNS requests on all interfaces
- interface: 0.0.0.0
- max-udp-size: 3072
- #Authorized IPs to access the DNS Server
- access-control: 0.0.0.0/0 refuse
- access-control: 127.0.0.1 allow
- access-control: 10.66.66.0/24 allow
- #not allowed to be returned for public internet names
- private-address: 10.66.66.0/24
- # Hide DNS Server info
- hide-identity: yes
- hide-version: yes
- #Limit DNS Fraud and use DNSSEC
- harden-glue: yes
- harden-dnssec-stripped: yes
- harden-referral-path: yes
- #Add an unwanted reply threshold to clean the cache and avoid when possible a DNS Poisoning
- unwanted-reply-threshold: 10000000
- #Have the validator print validation failures to the log.
- val-log-level: 1
- # systemctl disable systemd-resolved
- # systemctl stop systemd-resolved
- # systemctl enable unbound
- # systemctl start unbound
- Unbound testen:
- # nslookup google.com 127.0.0.1
- # nslookup google.com 10.66.66.1
- # unbound-checkconf
- ------------------------------------------------------------
- (11) Firewall einrichten:
- Als # eingeben:
- iptables -A INPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
- iptables -A FORWARD -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
- iptables -A INPUT -p udp -m udp --dport 51820 -m conntrack --ctstate NEW -j ACCEPT
- iptables -A INPUT -s 10.66.66.0/24 -p tcp -m tcp --dport 53 -m conntrack --ctstate NEW -j ACCEPT
- iptables -A INPUT -s 10.66.66.0/24 -p udp -m udp --dport 53 -m conntrack --ctstate NEW -j ACCEPT
- iptables -A OUTPUT -p udp -m udp --sport 51820 -j ACCEPT
- iptables -I INPUT -p udp --dport 51820 -j ACCEPT
- iptables -I INPUT -p tcp --dport xxx -j ACCEPT # xxx=ssh Port
- ip6tables -A INPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
- ip6tables -A FORWARD -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
- ip6tables -A INPUT -p udp -m udp --dport 51820 -m conntrack --ctstate NEW -j ACCEPT
- ip6tables -A INPUT -s fd42:42:42::/64 -p tcp -m tcp --dport 53 -m conntrack --ctstate NEW -j ACCEPT
- ip6tables -A INPUT -s fd42:42:42::/64 -p udp -m udp --dport 53 -m conntrack --ctstate NEW -j ACCEPT
- ip6tables -A OUTPUT -p udp -m udp --sport 51820 -j ACCEPT
- ip6tables -I INPUT -p udp --dport 51820 -j ACCEPT
- ip6tables -I INPUT -p tcp --dport xxx -j ACCEPT # xxx=ssh Port
- # apt install iptables-persistent
- -----------------------------------------------------------
- (12) Einen zweiten Client2 (Notebook) einrichten
- Wirguard auf Client2 installieren wie oben auf dem server:
- # nano /etc/apt/sources.list.d/unstable.list
- >
- deb http://deb.debian.org/debian/ unstable main
- # nano /etc/apt/preferences.d/limit-unstable
- >
- Package: *
- Pin: release a=unstable
- Pin-Priority: 90
- # apt update
- # apt install wireguard --dry-run
- (etwas über 60 Pakete sollten es ; dann:)
- # apt install wireguard -y
- (12.1) Konfiguration auf Client2 erstellen
- # cd /etc/wireguard
- # umask 077
- # wg genkey > client2.key # erstellen
- Client2-Schlüssel:
- Geheim:
- # cat /etc/wireguard/client2.key # anzeigen
- # cat /etc/wireguard/client2.key | wg pubkey # anzeigen, muss nicht eigens erstellt werden
- # touch /etc/wireguard/wg0.conf
- # nano /etc/wireguard/wg0.conf
- >
- [Interface]
- PrivateKey = >privater Client2-Schlüssel<
- MTU = 1420 # bei ipv6 mindestens 1280
- Address = 10.66.66.3/24, fd42:42:42::3/64
- DNS = 176.103.130.130,176.103.130.131
- [Peer]
- PublicKey = >öffentlicher Server-Schlüssel<
- Endpoint = xxx.dedyn.io:51820
- AllowedIPs = 0.0.0.0/0, ::/0
- Abschluss: Server-Konfiguration auf Server > neuen peer für client2 erstellen
- # nano /etc/wireguard/wg0.conf
- > # unter ersten [Peer] einfügen
- [Peer]
- PublicKey = >öffentlicher Client2-Schlüssel<
- AllowedIPs = 10.66.66.3/32, fd42:42:42::3/128
- wireguard auf client2 starten mit:
- # chown -v root:root /etc/wireguard/wg0.conf
- # chmod -v 600 /etc/wireguard/wg0.conf
- # wg-quick up wg0
- # systemctl enable wg-quick@wg0.service
- ANMERKUNG:
- Ab jetzt vor jeder Konfiguration:
- # wg-quick down wg0
- Nach jeder Konfiguration:
- # wg-quick up wg0
Quellcode
Hier kannst du den Code kopieren und ihn in deinen bevorzugten Editor einfügen. PASTEBIN_DOWNLOAD_SNIPPET_EXPLAIN