Startseite » Installation und Konfiguration von OpenVPN

Installation und Konfiguration von OpenVPN

Lasst mich ehrlich sein: OpenVPN ist ein alter Hut und im Netz tummeln sich zig ähnliche Anleitungen zur Installation.

Vorweg: OpenVPN lässt sich mit Zertifikaten und mit einem PreSharedKey (PSK) verwenden. Mit einem PSK lässt sich allerdings nur ein einziger Client gleichzeitig verwenden. Daher gehe ich gleich zu den Zertifikaten über.

Installation

Fürs Raspberry Pi ist dazu nur folgendes notwendig:

sudo apt update && sudo apt upgrade
sudo apt install openvpn openssl bridge-utils

sudo sh -c "printf 'net.ipv4.ip_forward=1\n' > /etc/sysctl.conf"
sudo sh -c "printf '1' > /proc/sys/net/ipv4/ip_forward"

Zertifikate für OpenVPN generieren

Diese Vorbedingung ist auch schon das schwierigste Stück. Zertifikate manuell zu generieren ist mühselig. Insbesondere, wenn mehrere Zertifikate benötigt werden. Eine graphische Oberfläche dazu macht die Sache einfacher. Insbesondere, wenn es um die spätere Verwaltung von den Zertifikaten geht, find ich die Kommandozeile etwas unpraktisch. Geht aber mittels dem Paket openssl, welches wir gerade installiert haben.

XCA ist eine gute OpenSSL-GUI, welche meinen Ansprüchen genügt. Sie ist für Windows, Linux und Mac verfügbar und vereinfacht den Prozess um die notwendigen Zertifikate zu erstellen.

Welche Zertifikate wie benötigt werden, werde ich im Konfigurations-Schritt erklären.

Server-Konfiguration

Folgende Beispielkonfiguration erläutere ich und muss für deine Bedürfnisse angepasst werden.

TUN öffnet eine Verbindung auf OSI-Layer 3, TAP auf Layer 2. Das bedeutet, dass TAP mächtiger ist als TUN, Broadcasts und andere Besonderheiten weitervermittelt. Allerdings sind für die meisten Anwendungen TUN-Verbindungen ausreichend und vor allem sparsamer. Insbesondere mit Hinblick auf begrenzte Upload-Raten deiner Internet-Verbidnung ist der Geschwindigkeitsaspekt sehr wichtig.

#Use TCP on Port 25
proto tcp-server
port 25

#maximum size of OpenVPN-tunnel-packets
tun-mtu 1500

#Maximum Segment Size Fix for TCP-Packets
mssfix

#Use TAP-Device (Layer 2 tunneling), set Server-IP and configure DHCP-range for clients
#dev tap0
#  ifconfig 192.168.178.3 255.255.255.0
#  route 192.168.178.0 255.255.255.0
#  ifconfig-pool 192.168.178.90 192.168.178.99

#Alternative to TAP: TUN-device (Layer 3)
dev tun
  # Subnetz, welches der Server erzeugen soll 
  # (darf keines der vorhandenen privaten Netzwerke sein!)
  server 10.8.0.0 255.255.255.0 # Subnetz
  local 0.0.0.0 #Interface-Adresse, auf der der Server laufen soll
  push "route 192.168.178.0 255.255.255.0"

# -- Cryptography --
# this machine is the server for the certificate encryption
tls-server

# CA public key
ca /etc/openvpn/keys/ca.crt

#VPN-Server public key, signed by CA
cert /etc/openvpn/keys/box.crt

#VPN-Server private key
key /etc/openvpn/keys/box.key

#Diffie hellman parameters
dh /etc/openvpn/keys/dh.pem

#save passwort for private key, but do not cache it in memory
askpass /etc/openvpn/keys/static.key
auth-nocache

#set allowed ciphers for the connection
cipher AES-256-GCM
data-ciphers AES-256-GCM
data-ciphers-fallback AES-256-CBC
# -- End Cryptographie --

#this is the OpenVPN-Server
mode server

# maximum simultaneous clients
max-clients 5

# clients can talk with each other
client-to-client

# route all traffice through the tunnel
push "redirect-gateway def1 bypass-dhcp"
push "dhcp-option DNS 192.168.22.3"
push "dhcp-option DNS 192.168.22.1"

#daemon

# Options for Logging
status /var/log/openvpn-status.log
log-append /var/log/openvpn.log
#verb 5

# Ping every 10 Seconds to keep connection alive
keepalive 10 120

# binds openvpn to users without groups and permissions
# (User und groups have to exist)
user nobody
group nogroup
# keep certificates and routes in cache after disconnection. Use only if needed because of insufficient permissions
persist-tun
persist-key

Firewall

Auf einem raspberry wirst du häufig iptables als firewall einsetzen. Du benötigst bestimmte Einstellungen, um den Netzwerkverkehr zwischen deinen Interfaces zu routen.

#----------
# Connections already established
#----------
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

#----------
# OpenVPN-Server
#----------
#OpenVPN (bridged TAP)
#iptables -A INPUT   -i tap+ -p tcp --dport 588 -j ACCEPT
#iptables -A INPUT   -i br+  -p tcp --dport 588 -j ACCEPT
#iptables -A FORWARD -i br+  -j ACCEPT
#iptables -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT

#OpenVPN (TUN)
iptables -A INPUT   -p tcp --dport 588 -j ACCEPT
iptables -A INPUT   -i tun+ -j ACCEPT
iptables -A OUTPUT  -o tun+ -j ACCEPT
iptables -A FORWARD -i tun+ -j ACCEPT
iptables -A FORWARD -i tun+ -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -A FORWARD -o tun+ -m state --state RELATED,ESTABLISHED -j ACCEPT

Bridge zwischen eth0 und OpenVPN (nur für TAP nötig)

Eine Bridge wird nur im Zusammenhang mit einem TAP-Device (Layer 2) benötigt. Sie stellt Verbindungen zwischen dem echten und dem virtuellen Netzwerkinterface her.

Um die Bridge beim Start des Systems zu erzeugen, kannst du folgende Datei als /etc/openvpn/bridge anlegen.

#!/bin/bash

# Define physical ethernet interface to be bridged
# with TAP interface(s) above.
eth="eth0"
eth_ip="192.168.22.3"
eth_netmask="255.255.255.0"
eth_broadcast="192.168.22.255"
eth_gateway="192.168.22.1"
eth_mac="b8:27:eb:e1:cb:07"

# Define Bridge Interface
br="br0"

# Define list of TAP interfaces to be bridged together
tap="tap0"

# Check for the desired action (start/stop)
action="$1"

if [ "$action" = "start" ]; then
	for t in $tap; do
		openvpn --mktun --dev $t
	done

	brctl addbr $br
	brctl addif $br $eth

	for t in $tap; do
		brctl addif $br $t
	done

	for t in $tap; do
		ifconfig $t 0.0.0.0 promisc up
		iptables -A INPUT -i $t -j ACCEPT
	done

	iptables -A INPUT -i $br -j ACCEPT
	iptables -A FORWARD -i $br -j ACCEPT

	ifconfig $eth 0.0.0.0 promisc up

	ifconfig $br $eth_ip netmask $eth_netmask broadcast $eth_broadcast
	ip link set $br address $eth_mac
	route add default gw $eth_gateway $br

	systemctl start openvpn@openvpn.service

elif [ "$action" = "stop" ]; then
	systemctl stop openvpn@openvpn.service

	iptables -D INPUT -i $br -j ACCEPT
	iptables -D FORWARD -i $br -j ACCEPT

	ifconfig $br down
	brctl delbr $br

	for t in $tap; do
		openvpn --rmtun --dev $t
		iptables -D INPUT -i $t -j ACCEPT
	done

	ifconfig $eth $eth_ip netmask $eth_netmask broadcast $eth_broadcast
	route add default gw $eth_gateway $eth

else
    echo "Usage: $0 [start|stop]"
    exit 1
fi

Diese Datei muss ausführbar sein!

sudo chmod -R 755 /etc/openvpn/bridge

Außerdem wirst du eine service-Datei für systemctl anlegen wollen. Die kann so aussehen:

[Unit]
Description=starte/stoppe Bruecke mit Standadprofil
After=openvpn.service

[Service]
Type=simple
ExecStart=/etc/openvpn/bridge start
ExecStop=/etc/openvpn/bridge stop
WorkingDirectory=/etc/openvpn/
Restart=always

[Install]
WantedBy=multi-user.target

Start und Autostart

Natürlich willst du OpenVPN nicht jedes Mal nach einem Reboot manuell starten wollen. Folgende Befehle auf der Kommandozeile werden zunächst Die richtige Konfiguration auswählen und in /etc/default/openvpn hinterlegen.

Dabei gehe ich davon aus, dass bei TAP der Start der Konfiguration durch die Bridge durchgeführt wird und in OpenVPN mit dem Start erstmal keine Konfiguration geladen wird.

Mit TUN lasse ich alle Konfigurationen laden.

Starte dann deine Konfiguration. Wenn mit den Netzwerkeinstellungen etwas falsch gelaufen ist, wirst du dich aus deinem Pi aussperren (soweit du mit SSH verbunden bist). Aktiviere den Autostart (enable) also erst, wenn deine Konfiguration zu deiner Zufriedenheit läuft. Im Notfall macht so ein Reboot erstmal alles Rückgängig.

Mit TAP-Device:

sudo sed -i 's/#AUTOSTART="none"/AUTOSTART="none"/g' /etc/default/openvpn
sudo systemctl daemon-reload
sudo systemctl start openvpnBridged.service
#Only if start works, activate autostart:
sudo systemctl enable openvpnBridged.service

Mit TUN-Device:

sudo sed -i 's/#AUTOSTART="all"/AUTOSTART="all"/g' /etc/default/openvpn
sudo systemctl daemon-reload
sudo systemctl start openvpn.service
#Only if start works, activate autostart:
sudo systemctl enable openvpn.service

Client-Konfiguration

Dein Client könnte ein anderes Raspberry, ein Windows-Client, ein Android-Gerät oder ein iPhone sein. Je nach Art des Geräts und des verwendeten Clients wird diese Konfiguration an einem bestimmten Pfad gespeichert und die Zertifikatspfade müssen ebenso angepasst werden.

#hostname (or IP address) protocol and port of the OpenVPN server that the client should connect to
remote yourhost.selfip.com
port 25
proto tcp-client

#maximum size of OpenVPN-tunnel-packets
tun-mtu 1500

# -- Cryptography --
# this machine is the client for the certificate encryption
tls-client

#enforces server certificate verification
remote-cert-tls server

# CA public key
ca "ca.crt"

#VPN-Client public key, signed by CA
cert "Client01.pem"

#VPN-Client private key
key "Client01.key.pem"

#save passwort for private key, but do not cache it in memory
askpass "pass.txt"
auth-nocache

#set allowed ciphers for the connection
cipher AES-256-GCM
data-ciphers AES-256-GCM
data-ciphers-fallback AES-256-CBC
# -- End Cryptography --


#tunnel type is "tap". If you use TUN, use "tun"
#dev tap0
dev tun

#prevents binding to a specific local IP address
nobind

#use the remote gateway for all traffic
route-gateway 192.168.178.1

#instructs the server to provide configuration information to the client (push)
pull

# Ping every 10 Seconds to keep connection alive
keepalive 10 120

# keep certificates and routes in cache after disconnection. Use only if needed because of insufficient permissions
#Do not use it for your client, because you will not be able to simply reconnect, if your server IP changes
#persist-tun
#persist-key

2 Kommentare zu „Installation und Konfiguration von OpenVPN“

  1. Pingback: Smarthome weltweit steuern mit VPN - Smarthome DIY - Heimautomatisierung selbst gemacht

  2. Pingback: Obfuscation - Verbergen von VPN gegenüber Firewalls

Kommentar verfassen

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert

Diese Seite verwendet Akismet, um Spam zu reduzieren. Erfahre, wie deine Kommentardaten verarbeitet werden..

Translate »