OpenVPN und Gentoo


Der heutige Artikel dient für mich auch als Gedächtnisstütze, wenn ich in Zukunft einmal wieder die OpenVPN Konfiguration anfassen muss.

Da ich ja in Kürze nach China aufbreche und dort die große Firewall of China den Internettraffic regelt, habe ich mich entschlossen auf meinem Server einfach einen VPN laufen zu lassen. Dieser kann dann den Internet Traffic für mich und meine Freundin regeln, außerdem habe ich so die Möglichkeit Backups unserer Fotos und Videos der Reise abzulegen, falls irgendwas passieren sollte.

Die folgenden Schritte beziehen sich auf Gentoo Linux, für andere Distributionen sind bei der Installation der Pakete entsprechend andere Schritte erforderlich.

Wir installieren die benötigten Pakete und deren Abhängigkeiten

root # emerge openvpn easy-rsa

Als Erstes ist es erforderlich – falls noch nicht geschehen – eine neue PKI (Puplic Key Infrastructure) zu installieren. Hierzu gibt es eine ausführliche englischsprachige Anleitung im Gentoo wiki, daher gehe ich darauf nicht mehr explizit ein.

Im nächsten Schritt wird der OpenVPN Server konfiguriert und gestartet.
Die openvpn.conf Datei die als Beispiel angefügt ist, muss noch entsprechend angepasst werden.

# server binding port 
port
4711
# openvpn protocol, could be tcp / udp / tcp6 / udp6
proto udp
# tun/tap device
dev tun0
# keys configuration, use generated keys
ca example/ca.crt
cert example/example.crt
key example/example.key
dh example/dh2048.pem

# optional tls-auth key to secure identifying
tls-auth example/ta.key 0
# OpenVPN 'virtual' network infomation, network and mask
server
10.100.0.0 255.255.255.0
# persistent device and key settings
persist-key
persist-tun
ifconfig-pool-persist ipp.txt
# pushing route tables
push "route 10.100.0.0 255.255.255.0"
push "dhcp-option DNS 8.8.8.8

# push "dhcp-option DNS 192.168.1.1"
# connection
keepalive 10 120
comp-lzo
user nobody
group nobody
# logging
status openvpn-status.log
log /etc/openvpn/openvpn.log
verb 4


Anschließend kann der Server gestartet werden und wir kümmern uns um das Routing, damit wir mit unseren Clients auch einen Internet Zugang haben.

Zuerst aktivieren wir das generelle IP Forwarding:

root # echo 1 > /proc/sys/net/ipv4/ip_forward

Im nächsten Schritt definieren wir die NAT Regeln für iptables um das Forwarding vom internen auf das externe Device zu aktivieren. Hier ist wichtig statt eth0 und eth1 die entsprechenden Adapterbezeichnungen einzutragen. Diese lauten normalerweise enp*S* für den Netzwerkadapter, bzw. tun0 für unser tunneling Device.

root # /sbin/iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
root # /sbin/iptables -A FORWARD -i eth0 -o eth1 -m state --state RELATED,ESTABLISHED -j ACCEPT
root # /sbin/iptables -A FORWARD -i eth1 -o eth0 -j ACCEPT

Da nun die Serverseite komplett konfiguriert ist, geht es jetzt an die Konfiguration der Clients:

Openvpn nutzt einen Config File, welcher alle notwendigen Einstellungen und in unserem Fall auch alle Authentifizierungen an den Server sendet, sodass die Eingabe von Passwörtern nicht notwendig ist.
Achtung! Diese Dateien dürfen unter KEINEN Umständen in die falschen Hände gelangen und müssen IMMER mit absoluter Sorgfalt gehandhabt werden. Solltet ihr den Verdacht haben, dass eure Zertifikate kompromittiert wurden, müsst ihr diese umgehend widerrufen und durch neue ersetzen.

# Specify that we are a client and that we
# will be pulling certain config file directives
# from the server.
client
# Use the same setting as you are using on
# the server.
;dev tap
dev tun
# Windows needs the TAP-Win32 adapter name
# from the Network Connections panel
# if you have more than one. On XP SP2,
;dev-node MyTap
# Are we connecting to a TCP or
# UDP server? Use the same setting as
# on the server.
;proto tcp
proto udp
# The hostname/IP and port of the server.
# You can have multiple remote entries
# to load balance between the servers.
remote my-server-1 1194
;remote my-server-2 1194
# Choose a random host from the remote
# list for load-balancing. Otherwise
# try hosts in the order specified.
;remote-random
# Keep trying indefinitely to resolve the
# host name of the OpenVPN server. Very useful
# on machines which are not permanently connected
# to the internet such as laptops.
resolv-retry infinite
# Most clients don't need to bind to
# a specific local port number.
nobind
# Downgrade privileges after initialization (non-Windows only)
;user nobody
;group nobody
# Try to preserve some state across restarts.
persist-key
persist-tun

# Wireless networks often produce a lot
# of duplicate packets. Set this flag
# to silence duplicate packet warnings.
mute-replay-warnings

# Verify server certificate by checking that the
# certicate has the correct key usage set.
remote-cert-tls server
# If a tls-auth key is used on the server
# then every client must also have the key.
tls-auth ta.key 1
# Select a cryptographic cipher.
AES-256-CBC
# Enable compression on the VPN link.
# Don't enable this unless it is also
# enabled in the server config file.
comp-lzo
# Set log file verbosity.
verb 3
# Silence repeating messages
;mute 20
#Insert content of your ca.crt file below
<ca>
</ca>
#Insert content of your example.crt file below
<cert>
</cert>
#Insert content of your USER.key file below
<key>
</key>
#We set key-direction 1 for iOS Devices, otherwise
# they won’t recognize the static key
<key-direction 1>
#Insert content of your ta.key file below
<tls-auth>
</tls-auth>

Abschließend braucht ihr nur noch die *.ovpn Dateien an die Benutzer, bzw. Devices auszugeben und fertig ist die Laube.

,

Schreibe einen Kommentar

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

Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden.