Artikelkategori: 7
Skriven av mdkdio 2018-04-06 14:49
url: 

OpenVPN Installation

OBS! Artikel kan vara i behov av redigering, då den har blivit uppdaterad några timmar efter att jag postade detta.
Det är mest stavningsfel som är fixade, men artikeln ska i alla fall kontrolleras vid tillfälle. /mdkdio 28/4-18


OpenVPN

VPN = Virtual Private Network

Klient & Server konfiguration:

Nedan kommer Klient och Server konfiguration markeras enligt [Server] och [Klient],
om information/konfiguration kräver detta (är olika beroende på om det är en
installation för Klient eller Server).

Innan vi fortsätter, så ska det nämnas att, för de som använt OpenVPN tidigare i Slackware Linux
kanske kommer ihåg att man behövde ladda ned alla paket och compilera dessa själv.

Det behovet finns inte längre, då Slackware Linux har alla de paket man behöver för installation
på DVD'n (eller CD) under 'l' (lzo) och 'n' (openvpn) mapparna...

Så har du en "full installation" av Slackware Linux, så är du redo att fortsätta.
Om inte, så är det bara att installera från DVD'n (installpkg) eller via slackpkg.

OBS! Denna guide är bara en "grund" för installation av OpenVPN, kort och gott, för att få
OpenVPN att fungera på din server/klient.

Mer detaljerad information rörande säkerhet etc finns att läsa på http://openvpn.net


Steg 1. Installation:

Om du är osäker på om OpenVPN är installerad,så kan du enkelt kolla detta genom att köra
följande kommando i en terminal.

ls /var/log/packages/openvpn*
I mitt fall, får jag följande "svar"
/var/log/packages/openvpn-2.3.17-x86_64-1_slack14.2

Alternativt så kan du använda slackpkg

slackpkg search openvpn

Och (i mitt fall) få följande svar..

"Looking for openvpn* in package list. Please wait... DONE
The list below shows all packages with name matching "openvpn*".
[ Status  ]        [ Repository   ]         [ Package                                             ]
   installed          patches                      openvpn-2.3.17-x86_64-1_slack14.2         
  uninstalled(masked)      slackware64                  openvpn-2.3.11-x86_64-1"

Saknas OpenVPN är det bara att installera, antingen från DVD eller via slackpkg (rekomenderas)

slackpkg install openvpn

(vilket naturligtvis kräver internet access).

Steg 2. Minimum behov:

Server och Klient datorer (eller mobiltelefon som klient). Dessa måste vara anslutna till internet via olika
nätverksanslutningar.

Steg 2.1 Server DNS:

En URL används normalt som adress för servern, men är inget krav, då IP adressen till din server räcker.
Dock är det rekommenderat att använda en URL för anslutning från internet, speciellt om din server är ansluten
via dynamisk IP (DHCP), vilket är vanligast.
En lösning på detta kan vara Noip2 vilket finns som paket via slackbuilds.org (https://slackbuilds.org/repository/14.2/network/noip2/)
som använder www.no-ip.com tjänsten,vilken är gratis.

Steg 2.2 Server information:
hostnamn: server1
IP:  192.168.100.100/255.255.255.0
URL: servernamn.no-ip.org
Network Interface: eth0
Steg 2.3 Klient information:
hostname: client1
IP: 192.168.1.101/255.255.255.0
Network Interface: wlan0
Steg 2.4 Administrator rättigheter:

Det krävs administrator rättigheter för att konfigurera OpenVPN. Detta gäller både Server och Klient.
I den här guiden utgår vi ifrån att du har root access.

Steg 2.5 Möjliga begränsningar och möjliga lösningar för klient med WiFi anslutning:

Tillgången till två routers kanske inte är möjlig. Men om du har en mobiltelefon/smartphone med 3G/4G så kan du t.ex.
använda "Portabel WiFi Hot Spot" funktionen för att ansluta klienten till internet.

Eller, varför inte fråga grannen om tillgång till internet via deras router? Om nu den möjligheten finns.

Ytterligare alternativ är WiFi access via publika nätverk, så som bibliotek, restauranger etc. Men tänk då på att dessa
nätverk kan använda en brandvägg. Eller att dessa nätverk kanske inte tillåter användande för anslutning via VPN.
Så kolla detta först :)

Steg 3. Skapa en publik nyckel infrastruktur (PKI - Public Key Infrastructure) via easy-rsa skript:

En PKI kan skapas på vilken dator som helst med en VPN installation. Men vettigast är att göra detta både på servern och klienten
sedan båda behöver detta. En enkel metod är att använda easy-rsa skriptet.
Vilket kan laddas ned via följande kommandon:

 git clone git://github.com/OpenVPN/easy-rsa

och sedan arkivera det för framtida användning

tar cvf easy-rsa.tar easy-rsa
3.1 Skapa nyckeln och certifikat för servern:

Följ följande steg för att skapa nycklar och cerfifikat på din server:

cd easy-rsa/easyrsa3

Skapa PKI och CA:

./easyrsa init-pki
./easyrsa build-ca

Skapa och verifiera ett lösenord och ange ett namn för servern. I denna artikel anges servernamn för enkelhetens skull, som server1.
Men du kan ange ett annat namn som passar dig bättre.

Skapa sedan begäran:

./easyrsa gen-req server1

Du kommer nu att begäras om ett nytt lösenord och bekräfta detta. Samt bekräfta server namnet.
Nu kan du fortsätta och signera din begäran:

./easyrsa sign-req server server1

Bekräfta genom att ange 'yes', ange sedan ditt CA lösenord

Skapa sedan två nyckel-filer:

cd /etc/openvpn/certs/
openssl dhparam -out dh2048.pem 2048
cd /etc/openvpn/keys/
/usr/sbin/openvpn --genkey --secret ta-key
3.2 Skapa sedan nycklar och certifikat för din klient:

Följande steg är för din klient.
Du kommer att behöva easy-rsa skriptet, så du kan kopiera easy-rsa tar filen från din server, och packa upp det:

tar xvf easy-rsa.tar

Skapa sedan PKI och CA:

cd easy-rsa/easyrsa3
./easyrsa init-pki
./easyrsa gen-req client1

Du kommer nu att begäras om ytterligare ett lösenord, och bekräfta detta och klient namnet.
I denna artikel anges klient namnet för enkelhetens skull, som client1.
Men du kan ange ett annat namn som passa dig bättre.

Kopiera pki/reqs/client1.req till servern.

3.2.1 Signera klientens begäran på servern

I denna artikel utgår vi ifrån att klientens fil (client1.req) har kopierats till /root/openvpn/ mappen på servern.
Nu kan du fortsätta med import och signera client1's begäran:

cd /root/easy-rsa/easyrsa3
./easyrsa import-req /root/openvpn/client1.req client1
./easyrsa sign-req client client1

Du blir nu ombedd att bekräfta begäran med "yes" och server1's CA lösenord.

Kopiera den genererade client1.crt filen tillbaka till klienten
(root/easy-rsa/easyrsa3/pki/issued/client1.crt)

4. Konfigurera servern:

Kopiera följande filer, skapade av easy-rsa skriptet till respektive mapp under /etc/openvpn mappen:

cp /root/easy-rsa/easyrsa3/pki/ca-crt > /etc/openvpn/certs/
cp /root/easy-rsa/easyrsa3/pki/issued/server1.crt > /etc/openvpn/certs/
cp /root/easy-rsa/easyrsa3/pki/private/server1.crt > /etc/openvpn/keys/

Kopiera exempel konfigurations filen server.conf från openvpn installationen till openvpn's konfigurations mapp.
Du finner denna antingen på Slackware DVD'n eller via din favorit slackware server (sunet eller annan 'mirror').
Alternativt kan du använda paketet som du kan hämta från openvpn.net.
I följande exempel använder jag ftp.slackware.com

cd /tmp/
wget -c > ftp://ftp.slackware.com/pub/slackware/slackware/source/n/openvpn/openvp-*,tar.?z
cd /usr/src/
tar xvf /tmp/openvpn-*.tar.?z

Kopiera filen server.conf som finns i nyss uppackade käll-paket till openvpn's konfigurations mapp.

cp openvpn-*/sample/sample-config-files/server.conf > /etc/openvpn/

Redigera följande rader i /etc/openvpn/server.conf filen.

Från dessa rader:

ca ca.crt
cert server.crt
key server.key # This file should be kept secret

dh dh1024.pem
;tls-auth ta.key 0 # This file is secret

;user nobody
;group nobody

;log-append openvpn.log

Till:

ca /etc/openvpn/certs/ca.crt
cert /etc/openvpn/certs/server1.crt
key /etc/openvpn/keys/server1.key # This file should be kept secret

dh  /etc/openvpn/certs/dh2048.pem

tls-auth /etc/openvpn/keys/ta.key 0 # This file is secret

user nobody
group nobody

log-append /var/log/openvpn.log

Slutligen lägger du till följande i /etc/openvpn/server.conf:

# If you want to use OpenVPN as a daemon, uncomment this line.
# Generally speaking, servers should run OpenVPN as a daemon
daemon

Hela server.conf filen borde nu se ut som följer (Exempel baserad på guiden ovan):

#################################################
# Sample OpenVPN 2.0 config file for#
# multi-client server. #
# #
# This file is for the server side #
# of a many-clients <-> one-server #
# OpenVPN configuration. #
# #
# OpenVPN also supports #
# single-machine <-> single-machine #
# configurations (See the Examples page #
# on the web site for more info). #
# #
# This config should work on Windows #
# or Linux/BSD systems.  Remember on #
# Windows to quote pathnames and use #
# double backslashes, e.g.: #
# "C:Program FilesOpenVPNconfigfoo.key" #
# #
# Comments are preceded with '#' or ';' #
########################################################################
# Which local IP address should OpenVPN
# listen on? (optional)
;local a.b.c.d

# Which TCP/UDP port should OpenVPN listen on?
# If you want to run multiple OpenVPN instances
# on the same machine, use a different port
# number for each one. You will need to
# open up this port on your firewall.
port 1194
  # TCP or UDP server?
;proto tcp
proto udp

# "dev tun" will create a routed IP tunnel,
# "dev tap" will create an ethernet tunnel.
# Use "dev tap0" if you are ethernet bridging
# and have precreated a tap0 virtual interface
# and bridged it with your ethernet interface.
# If you want to control access policies
# over the VPN, you must create firewall
# rules for the the TUN/TAP interface.
# On non-Windows systems, you can give
# an explicit unit number, such as tun0.
# On Windows, use "dev-node" for this.
# On most systems, the VPN will not function
# unless you partially or fully disable
# the firewall for the TUN/TAP interface.
;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 or higher,
# you may need to selectively disable the
# Windows firewall for the TAP adapter.
# Non-Windows systems usually don't need this.
;dev-node MyTap

# SSL/TLS root certificate (ca), certificate
# (cert), and private key (key).  Each client
# and the server must have their own cert and
# key file.  The server and all clients will
# use the same ca file.
#
# See the "easy-rsa" directory for a series
# of scripts for generating RSA certificates
# and private keys.  Remember to use
# a unique Common Name for the server
# and each of the client certificates.
#
# Any X509 key management system can be used.
# OpenVPN can also use a PKCS #12 formatted key file
# (see "pkcs12" directive in man page).
ca /etc/openvpn/certs/ca.crt
cert /etc/openvpn/certs/server1.crt
key /etc/openvpn/keys/server1.key  # This file should be kept secret

# Diffie hellman parameters.
# Generate your own with:
#   openssl dhparam -out dh1024.pem 1024
# Substitute 2048 for 1024 if you are using
# 2048 bit keys.
dh /etc/openvpn/certs/dh2048.pem

# Configure server mode and supply a VPN subnet
# for OpenVPN to draw client addresses from.
# The server will take 10.8.0.1 for itself,
# the rest will be made available to clients.
# Each client will be able to reach the server
# on 10.8.0.1. Comment this line out if you are
# ethernet bridging. See the man page for more info.
server 10.8.0.0 255.255.255.0

# Maintain a record of client <-> virtual IP address
# associations in this file.  If OpenVPN goes down or
# is restarted, reconnecting clients can be assigned
# the same virtual IP address from the pool that was
# previously assigned.
ifconfig-pool-persist ipp.txt

# Configure server mode for ethernet bridging.
# You must first use your OS's bridging capability
# to bridge the TAP interface with the ethernet
n# NIC interface.  Then you must manually set the
# IP/netmask on the bridge interface, here we
# assume 10.8.0.4/255.255.255.0.  Finally we
# must set aside an IP range in this subnet
# (start=10.8.0.50 end=10.8.0.100) to allocate
# to connecting clients.  Leave this line commented
# out unless you are ethernet bridging.
;server-bridge 10.8.0.4 255.255.255.0 10.8.0.50 10.8.0.100

# Configure server mode for ethernet bridging
# using a DHCP-proxy, where clients talk
# to the OpenVPN server-side DHCP server
# to receive their IP address allocation
# and DNS server addresses.  You must first use
# your OS's bridging capability to bridge the TAP
# interface with the ethernet NIC interface.
# Note: this mode only works on clients (such as
# Windows), where the client-side TAP adapter is
# bound to a DHCP client.
;server-bridge

# Push routes to the client to allow it
# to reach other private subnets behind
# the server.  Remember that these
# private subnets will also need
# to know to route the OpenVPN client
# address pool (10.8.0.0/255.255.255.0)
# back to the OpenVPN server.
;push "route 192.168.10.0 255.255.255.0"
;push "route 192.168.20.0 255.255.255.0"

# To assign specific IP addresses to specific
# clients or if a connecting client has a private
# subnet behind it that should also have VPN access,
# use the subdirectory "ccd" for client-specific
# configuration files (see man page for more info).

# EXAMPLE: Suppose the client
# having the certificate common name "Thelonious"
# also has a small subnet behind his connecting
# machine, such as 192.168.40.128/255.255.255.248.
# First, uncomment out these lines:
;client-config-dir ccd
;route 192.168.40.128 255.255.255.248
# Then create a file ccd/Thelonious with this line:
#   iroute 192.168.40.128 255.255.255.248
# This will allow Thelonious' private subnet to
# access the VPN.  This example will only work
# if you are routing, not bridging, i.e. you are
# using "dev tun" and "server" directives.

# EXAMPLE: Suppose you want to give
# Thelonious a fixed VPN IP address of 10.9.0.1.
# First uncomment out these lines:
;client-config-dir ccd
;route 10.9.0.0 255.255.255.252
# Then add this line to ccd/Thelonious:
#   ifconfig-push 10.9.0.1 10.9.0.2

# Suppose that you want to enable different
# firewall access policies for different groups
# of clients.  There are two methods:
# (1) Run multiple OpenVPN daemons, one for each
#     group, and firewall the TUN/TAP interface
#     for each group/daemon appropriately.
# (2) (Advanced) Create a script to dynamically
#     modify the firewall in response to access
#     from different clients.  See man
#     page for more info on learn-address script.
;learn-address ./script

# If enabled, this directive will configure
# all clients to redirect their default
# network gateway through the VPN, causing
# all IP traffic such as web browsing and
# and DNS lookups to go through the VPN
# (The OpenVPN server machine may need to NAT
# or bridge the TUN/TAP interface to the internet
# in order for this to work properly).
;push "redirect-gateway def1 bypass-dhcp"

# Certain Windows-specific network settings
# can be pushed to clients, such as DNS
# or WINS server addresses.  CAVEAT:
# http://openvpn.net/faq.html#dhcpcaveats
# The addresses below refer to the public
# DNS servers provided by opendns.com.
;push "dhcp-option DNS 208.67.222.222"
;push "dhcp-option DNS 208.67.220.220"

# Uncomment this directive to allow different
# clients to be able to "see" each other.
# By default, clients will only see the server.
# To force clients to only see the server, you
# will also need to appropriately firewall the
# server's TUN/TAP interface.
;client-to-client

# Uncomment this directive if multiple clients
# might connect with the same certificate/key
# files or common names.  This is recommended
# only for testing purposes.  For production use,
# each client should have its own certificate/key
# pair.
#
# IF YOU HAVE NOT GENERATED INDIVIDUAL
# CERTIFICATE/KEY PAIRS FOR EACH CLIENT,
# EACH HAVING ITS OWN UNIQUE "COMMON NAME",
# UNCOMMENT THIS LINE OUT.
;duplicate-cn

# The keepalive directive causes ping-like
# messages to be sent back and forth over
# the link so that each side knows when
# the other side has gone down.
# Ping every 10 seconds, assume that remote
# peer is down if no ping received during
# a 120 second time period.
keepalive 10 120

# For extra security beyond that provided
# by SSL/TLS, create an "HMAC firewall"
# to help block DoS attacks and UDP port flooding.
#
# Generate with:
#   openvpn --genkey --secret ta.key
#
# The server and each client must have
# a copy of this key.
# The second parameter should be '0'
# on the server and '1' on the clients.
tls-auth /etc/openvpn/keys/ta.key 0 # This file is secret

# Select a cryptographic cipher.
# This config item must be copied to
# the client config file as well.
;cipher BF-CBC        # Blowfish (default)
;cipher AES-128-CBC   # AES
;cipher DES-EDE3-CBC  # Triple-DES

# Enable compression on the VPN link.
# If you enable it here, you must also
# enable it in the client config file.
comp-lzo

# The maximum number of concurrently connected
# clients we want to allow.
;max-clients 100

# It's a good idea to reduce the OpenVPN
# daemon's privileges after initialization.
#
# You can uncomment this out on
# non-Windows systems.
user nobody
group nobody

# The persist options will try to avoid
# accessing certain resources on restart
# that may no longer be accessible because
# of the privilege downgrade.
persist-key
persist-tun

# Output a short status file showing
# current connections, truncated
# and rewritten every minute.
status openvpn-status.log

# By default, log messages will go to the syslog (or
# on Windows, if running as a service, they will go to
# the "Program FilesOpenVPNlog" directory).
# Use log or log-append to override this default.
# "log" will truncate the log file on OpenVPN startup,
# while "log-append" will append to it.  Use one
# or the other (but not both).
;log         openvpn.log
log-append  /var/log/openvpn.log

# Set the appropriate level of log
# file verbosity.
#
# 0 is silent, except for fatal errors
# 4 is reasonable for general usage
# 5 and 6 can help to debug connection problems
# 9 is extremely verbose
verb 3

# Silence repeating messages.  At most 20
# sequential messages of the same message
# category will be output to the log.
;mute 20

# If you want to use OpenVPN as a daemon, uncomment this line.
# Generally speaking, servers should run OpenVPN as a daemon
daemon
 

OBS! kommentarer i server.conf filen kan antingen börja med # eller ;
För att hjälpa dig med att lägga till parametrar används # för att
kommentera ut text, och ; för att kommentera ut konfigurations linjer.

Kopiera rc.openvpn nedan och placera innehållet i en fil under /etc/rc.d/

T.ex. nano /etc/rc.d/rc.openvpn
Ctrl+Shift+v
Ctrl+x
y
Enter/Retur

#!/bin/sh
#
# /etc/rc.d/rc.openvpn
#
# Start/stop/restart the openvpn server.
#
ovpn_start() {
  if [ -x /usr/sbin/openvpn -a -r /etc/openvpn/server.conf ]; then
    echo "Starting OpenVPN:  /usr/sbin/openvpn server.conf"
    /usr/sbin/openvpn /etc/openvpn/server.conf
  fi
}

ovpn_stop() {
  killall openvpn
}

ovpn_restart() {
  ovpn_stop
  sleep 2
  ovpn_start
}

case "$1" in
'start')
  ovpn_start
  ;;
'stop')
  ovpn_stop
  ;;
'restart')
  ovpn_restart
  ;;
*)
  echo "Usage: $0 {start|stop|restart}"
esac
 

Gör sedan filen exekverbar/körbar:

chmod 755 /etc/rc.d/rc.openvpn

När du sedan har startat openvpn på din server, kontrollera så att openvpn fungerar som det ska,
genom att läsa /var/log/openvpn.log filen
t.ex. genom:

cat /var/log/openvpn.log
5. Port konfiguration / Öppna port:

Du behöver "skicka vidare" trafiken från porten du valt för OpenVPN vidare till din server. För att göra detta, måste du konfigurera din server med en fast IP adress.
Och konfigurera din router.
Du kan använda netconfig, wicd eller network-manager för ange fast IP adress i Slackware. Sedan får du läsa manualen för din router för att konfigurera reservering av
IP adress för din server, och port konfiguration (port forwarding).
För våran grund instalaltion av OpenVPN kommer UDP porten att vara 1194.

Om du inte hittar din manual för din router får du söka efter en via internet. Men en bra början är http://portforward.com

6. Konfigurera din klient:

På din klient, gör följande för att konfigurera den.
Ladda ned openvpn paketet som tidigare bekrevs i steg (vilket?). Fortsätt sedan med att kopiera de konfigurations filer som är rel. till klienter:

cp /usr/src/openvpn-*/sample/sample-config-files/client.conf > /etc/openvpn/

Redigera följande rader i /etc/openvpn/client.conf:

remote my-server-1 1194

;user nobody
;group nobody

ca ca.crt
cert client.crt
key client.key

;tls-auth ta.key 1

 

Till följande:

remote servervpn.no-ip.org 1194

user nobody
group nobody

ca /etc/openvpn/certs/ca.crt
cert /etc/openvpn/certs/client1.crt
key /etc/openvpn/keys/client1.key

tls-auth /etc/openvpn/keys/ta.key 1
 

OBS! kommentarer i client.conf filen kan antingen börja med # eller ;
För att hjälpa dig med att lägga till parametrar används # för att
kommentera ut text, och ; för att kommentera ut konfigurations linjer.

Du behöver denna fil, skapad via klientens easy-rsa skript:
/root/easy-rsa/easyrsa3/pki/private/client1.key

Och följande från servern's easy-rsa skript:

ca.crt
ta.key

Och du behöver dessutom client1.crt skapad på servern, tidigare förklarad i steg (?) ovan

Placera dessa filer enligt anvisning i client.conf filen. Så ca.crt och client1.crt ska till /etc/openvpn/certs/ medans client1.key och ta.key
ska till /etc/openvpn/keys/

Och i client.conf filen, kommentera ut linjen:

ns-cert-type server

Och lägg till raden:

remote-cert-tls server

client.conf filen ser nu ut som enligt nedan:

##############################################
# Sample client-side OpenVPN 2.0 config file #
# for connecting to multi-client server.     #
#                                            #
# This configuration can be used by multiple #
# clients, however each client should have   #
# its own cert and key files.                #
#                                            #
# On Windows, you might want to rename this  #
# file so it has a .ovpn extension           #
##############################################
# 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.
# On most systems, the VPN will not function
# unless you partially or fully disable
# the firewall for the TUN/TAP interface.
;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,
# you may need to disable the firewall
# for the TAP adapter.
;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 servervpn.no-ip.org 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

# If you are connecting through an
# HTTP proxy to reach the actual OpenVPN
# server, put the proxy server/IP and
# port number here.  See the man page
# if your proxy server requires
# authentication.
;http-proxy-retry # retry on connection failures
;http-proxy [proxy server] [proxy port #]

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

# SSL/TLS parms.
# See the server config file for more
# description.  It's best to use
# a separate .crt/.key file pair
# for each client.  A single ca
# file can be used for all clients.
ca /etc/openvpn/certs/ca.crt
cert /etc/openvpn/certs/client1.crt
key /etc/openvpn/keys/client1.key

# Verify server certificate by checking
# that the certicate has the nsCertType
# field set to "server".  This is an
# important precaution to protect against
# a potential attack discussed here:
#  http://openvpn.net/howto.html#mitm
#
# To use this feature, you will need to generate
# your server certificates with the nsCertType
# field set to "server".  The build-key-server
# script in the easy-rsa folder will do this.
;ns-cert-type server
remote-cert-tls server

# If a tls-auth key is used on the server
# then every client must also have the key.
tls-auth /etc/openvpn/keys/ta.key 1

# Select a cryptographic cipher.
# If the cipher option is used on the server
# then you must also specify it here.
;cipher x

# 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
#
7. Testa VPN

På servern:

/etc/rc.d/rc.openvpn start

Ange Serverns PEM lösenord när du får besked om att göra detta...

På klienten:

/usr/sbin/openvpn /etc/openvpn/client.conf

Ange klientens PEM lösenord när du får besked om att göra detta

För att stoppa OpenVPN på din klient, Helt enkelt tryck ned Ctrl+C

På båda datorerna ska du nu se ett nytt nätverks gränssnitt. Något i stil med detta:

# /sbin/ifconfig tun0
tun0: flags=4305  mtu 1500
        inet 10.8.0.1  netmask 255.255.255.255  destination 10.8.0.2
        unspec 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00  txqueuelen 100  (UNSPEC)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

Naturligtvis kan du pinga servern från klienten (eller tvärt om):
T.ex. från klienten:

# ping -c 3 10.8.0.1
PING 10.8.0.1 (10.8.0.1) 56(84) bytes of data.
64 bytes from 10.8.0.1: icmp_req=1 ttl=64 time=2888 ms
64 bytes from 10.8.0.1: icmp_req=2 ttl=64 time=1997 ms
64 bytes from 10.8.0.1: icmp_req=3 ttl=64 time=1324 ms

--- 10.8.0.1 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 1999ms
rtt min/avg/max/mdev = 1324.475/2070.293/2888.429/640.527 ms, pipe 3

För att starta openvpn automatiskt vid uppstart av servern, inkludera följande rader i /etc/rc.d/rc.local

# Start the OpenVPN Server
if [ -x /etc/rc.d/rc.openvpn-server ]; then
  /etc/rc.d/rc.openvpn-server start
fi
8. IP Routing

Fram tills nu har vi skapat en tunnel enhet både på servern och på klienten, kallad tun0, vilken endast kan ses via dessa maskiner.
Men mer arbete krävs för att styra klientens anslutning via tun0 och sedan till den WAN anslutning som din server har.

8.1 Server konfiguration
Aktivera IP forwarding:
chmod +x /etc/rc.d/rc.ip_forward
/etc/rc.d/rc.ip_forward start

Nu är IP forwarding aktiverad, och kommer att aktiveras automatiskt vid omstart.

Skapa en mapp med namnet ccd under /etc/openvpn

mkdir /etc/openvpn/ccd

Skapa en fil med samma namn som din klient (i detta fall client1) och ange följande linje i /etc/openvpn/ccd/client1

iroute 192.168.1.0 255.255.255.0

Byt ut 192.168.1.0 255.255.255.0 till den IP etc som din klient använder

Redigera även /etc/openvpn/server.conf och lägg till följande rader:

push "route 192.168.200.0 255.255.255.0"

client-config-dir ccd
 route 192.168.1.0 255.255.255.0

push "redirect-gateway def1 bypass-dhcp"

push "dhcp-option DNS 208.67.222.222"
push "dhcp-option DNS 208.67.228.220"

Naturligtvis byter du ut 192.168.200.0 255.255.255.0 med serverns rätta adress, och 192.168.1.0 255.255.255.0 med klientens rätta adress.

208.67.222.222 och 208.67.220.220 är OpenDNS IP adresser. Fram till nu har DNS push konfigurationen inte fungerat. Du kan antingen använda
klientens orginal DNS servrar eller annars kan du redigera /etc/resolv.conf filen manuellt.
Men det är värt att prova med DNS servrar listade av klientent. Normalt är dessa tillgängliga från serverns nätverk, så du behöver inte göra något.

Nästa steg, så behöver du konfigurera en del IPtables NAT forwarding på servern (endast!). Du kan göra detta genom att rensa iptables:

iptables -F

Och sedan:

iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -0 eth0 -j MASQUERADE

I Slackware kan en sådan rad redan finnas i /etc/rc.firewall och /etc/rc.d/rc.inet2 kör denna varje gång du startar om din server OM den sistnämnda är exekverbar.
Du behöver inte lägga till något i /etc/rc.d/rc.local

De exakta rader som du behöver inkludera beror på om du redan har lagt till egna iptable filter kedjor eller regler, men här utgår vi ifrån att så inte är fallet.

Som tidigare förklarat, som minimum behöver du endast enge förljande rader i /etc/rc.d/rc.firewall

#!/bin/sh
iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE

Men och andra sidan, gillar du nog en bättre brandvägg och är åt minstonde lite bekant med iptables, förslag här är att du använder följande skript att vara inkluderat i din /etc/rc.d/rc.firewall fil.
Kommentarerna i skriptet bör hjälpa dig lite på vägen, och med vilken påverkan dessa har på din server.

#!/bin/bash                                                                                                                                           
# Start/stop/restart/status the firewall                                                                                                             
                                                                                                                                                     
IPT=/usr/sbin/iptables # This will provide some portability                                                                                          
                                                                                                                                                     
firewall_start() {                                                                                                                                   
                                                                                                                                                     
  # flush the iptables                                                                                                                               
  echo -e "Starting the firewall ....c"                                                                                                             
  $IPT -F                                                                                                                                            
                                                                                                                                                      
  # policies                                                                                                                                         
  $IPT -P OUTPUT DROP                                                                                                                                
  $IPT -P INPUT DROP                                                                                                                                 
  $IPT -P FORWARD DROP

  $IPT -N SERVICES # services is a custom chain

  # allowed output
  $IPT -A OUTPUT -o lo -j ACCEPT
  $IPT -A OUTPUT -o eth0 -j ACCEPT
  $IPT -A OUTPUT -o tun0 -j ACCEPT

  # allowed inputs
  #$IPT -A INPUT -i lo -j ACCEPT # uncomment if the host is a desktop
  $IPT -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT # allow responses
  $IPT -A INPUT -j SERVICES # append the services chain to the input

  # allowed forwarding for openVPN
  $IPT -A FORWARD -i eth0 -o tun0 -m state --state ESTABLISHED,RELATED -j ACCEPT
  $IPT -A FORWARD -s 10.8.0.0/24 -o eth0 -j ACCEPT

  # masquerade the openvpn network
  $IPT -t nat -A POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE

  # allow sshd on the default tcp port 22
  #$IPT -A SERVICES -p tcp --dport 22 -j ACCEPT # Uncomment to allow sshd

  # allow openvpn for the non-default tcp port 443
  $IPT -A SERVICES -p tcp --dport 443 -j ACCEPT
 
  echo "done."
}

firewall_stop() {
  echo -e "Stopping the firewall ....c"

  # polcies (permissive)
  $IPT -P OUTPUT ACCEPT
  $IPT -P INPUT ACCEPT
  $IPT -P FORWARD ACCEPT

  # flush the iptables
  $IPT -F

  # delete the services custom chain
  $IPT -X SERVICES
  echo "done."
}

firewall_status() {
  $IPT -vL
}

case "$1" in
'start')
  firewall_start
  ;;
'stop')
  firewall_stop
  ;;
'restart')
  firewall_stop
  firewall_start
  ;;
'status')
  firewall_status
  ;;
*)
  echo "Usage $0 start|stop|restart|status"
esac
 

Gör din firewall rc fil exekverbar:

chmod +x /etc/rc.d/rc.firewall

och starta den...

/etc/rc.d/rc.firewall start

Starta om OpenVPN tjänsten på servern:

/etc/rc.d/rc.openvpn restart

Och anslut från din klient:

/usr/bin/openvpn /etc/openvpn/client.conf
9. Brandväggar

I tidigare kapitel hänvisade vi till en brandvägg du kanske använder för att skydda din OpenVPN server. Men detta kapitel hänvisar till brandväggar på din klients nätverk,
som kan blokera VPN anslutningen genom att blockera tafik via UDP port 1194.

För att kunna ta dig igenom klientens brandvägg kanske du behöver ändra porten till 443 i stället - vilken normalt är reserverad för https.
Eller om du använder TCP i stället för UDP kan också hjälpa. För att göra dessa ändringar behöver du göra ändringar i /etc/openvpn/server.conf filen på servern.

från:

port 1194
proto udp

till:

port 443
proto tcp

och /etc/openvpn/client.conf på klienten.

från:

proto udp
remote servervpn.no-ip.org 1194

till:

proto tcp
remote servervpn.no-ip.org 443

Serverns brandvägg skript måste också ändras. Ändra följande rader:

från:

# allow vpn on the default udp port 1194
$IPT -A SERVICES -p udp --dport 1194 -j ACCEPT

till:

# allow vpn on the custom tcp port 443
$IPT -A SERVICES -p tcp --dport 443 -j ACCEPT

Du behöder dessutom ändra i din routers port forwarding inställningar till TCP port 443.

Källa för denna artikel:
https://docs.slackware.com/playground:openvpn_-_how_to_set_up_a_slackware_server_and_a_slackware_client
Ny (redigerad) version (se OBS i början av denna artikel):
http://docs.slackware.com/howtos:network_services:openvpn (Gjord av Chris Abela 6/4-18 21:19 UTC)

Vilken i sin tur har följande källor:
http://en.wikipedia.org/wiki/OpenVPN
https://wiki.archlinux.org/index.php/OpenVPN
http://slackwiki.com/OpenVPN_smcr_2012
http://www.no-ip.com/

Orginal artikel skriven av Chris Abela
Svensk översättning gjord av mdkdio 2018-04-02 slackwarelinux.se

×