Openvpn

De The Linux Craftsman
Aller à la navigation Aller à la recherche

Préparation

Dans un premier temps, il faudra avoir une connexion à Internet, utiliser un serveur DNS et désactiver SELinux.

Pour ceux qui auraient manqué des étapes, les voici:

Assurez-vous d'avoir installé le dépôt EPEL car OpenVPN vient de cette source.

Une fois ces étapes effectuées, entrons dans le vif du sujet !

Partie serveur

Installation

Premièrement, installons les packages nécessaires:

# yum -y install openvpn easy-rsa

OpenVPN est livré avec un fichier de configuration d'exemple que nous allons copier dans le bon répertoire:

# cp /usr/share/doc/openvpn-*/sample/sample-config-files/server.conf /etc/openvpn

Configuration

Éditons le fichier maintenant qu'il est au bon endroit:

# vi /etc/openvpn/server.conf

Nous allons dé-commenter la ligne suivante qui permet de router le trafic qui vient du client à travers le tunnel VPN:

push "redirect-gateway def1 bypass-dhcp"

La ligne suivante doit être décommentée car elle permet de préciser quel serveur DNS le client doit utiliser. Si vous avez un serveur DNS sur votre réseau, mettez son IP ici, sinon utilisez le serveur de Google:

push "dhcp-option DNS 8.8.8.8"

Les lignes suivantes doivent être décommenteés pour signifier à OpenVPN d'utiliser l'utilisateur et le group nobody. Cela permet d'augmenter la sécurité.

user nobody
group nobody

Pour résumer:

port 1194
proto udp
dev tun
ca ca.crt
cert server.crt
key server.key  # This file should be kept secret
dh dh2048.pem
server 10.8.0.0 255.255.255.0
ifconfig-pool-persist ipp.txt
push "redirect-gateway def1 bypass-dhcp"
push "dhcp-option DNS 8.8.8.8"
keepalive 10 120
comp-lzo
user nobody
group nobody
persist-key
persist-tun
log openvpn.log
status openvpn-status.log
verb 3

Création des clés

Mise en place

Le serveur utilise un certificat pour monter un tunnel SSL avec le client. Cela nécessite une autorité de certification, pour délivrer des certificats, une paire certificat/clé pour le serveur et une paire certificat/clé pour le client.

Nous allons utiliser une librairie qui nous permet de créer tous les fichiers dont nous allons avoir besoin: easy-rsa.

Tout d'abord, créons un dossier pour stocker tous ces fichiers:

# mkdir -p /etc/openvpn/easy-rsa/keys

Copions les fichiers nécessaire:

# cp -rf /usr/share/easy-rsa/2.0/* /etc/openvpn/easy-rsa

Informations du certificat

Nous allons inscrire les informations du certificat dans le fichier /etc/openvpn/easy-rsa/vars pour ne pas avoir à les saisir à chaque fois:

export KEY_COUNTRY="FR"
export KEY_PROVINCE="Herault"
export KEY_CITY="Juvignac"
export KEY_ORG="Tala Informatique"
export KEY_EMAIL="root@tala.informatique.fr"
export KEY_OU="Informatique"

Création de l'autorité de certification

# cd /etc/openvpn/easy-rsa
# source ./vars
NOTE: If you run ./clean-all, I will be doing a rm -rf on /etc/openvpn/easy-rsa/keys
# ./clean-all
# ./build-ca
Generating a 2048 bit RSA private key
...................................................+++
...................................................................................+++
writing new private key to 'ca.key'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [FR]:
State or Province Name (full name) [Herault]:
Locality Name (eg, city) [Juvignac]:
Organization Name (eg, company) [Tala Informatique]:
Organizational Unit Name (eg, section) [Informatique]:
Common Name (eg, your name or your server's hostname) [Tala Informatique CA]:
Name [EasyRSA]:
Email Address [root@tala.informatique.fr]:

Génération du certificat serveur

Pour le serveur, ne pas mettre de mot de passe lorsque l'on vous pose la question.

# ./build-key-server server
Generating a 2048 bit RSA private key
.............+++
.........+++
writing new private key to 'server.key'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [FR]:
State or Province Name (full name) [Herault]:
Locality Name (eg, city) [Juvignac]:
Organization Name (eg, company) [Tala Informatique]:
Organizational Unit Name (eg, section) [Informatique]:
Common Name (eg, your name or your server's hostname) [server]:
Name [EasyRSA]:
Email Address [root@tala.informatique.fr]:

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
Using configuration from /etc/openvpn/easy-rsa/openssl-1.0.0.cnf
Check that the request matches the signature
Signature ok
The Subject's Distinguished Name is as follows
countryName           :PRINTABLE:'FR'
stateOrProvinceName   :PRINTABLE:'Herault'
localityName          :PRINTABLE:'Juvignac'
organizationName      :PRINTABLE:'Tala Informatique'
organizationalUnitName:PRINTABLE:'Informatique'
commonName            :PRINTABLE:'server'
name                  :PRINTABLE:'EasyRSA'
emailAddress          :IA5STRING:'root@tala.informatique.fr'
Certificate is to be certified until Oct 14 17:16:17 2025 GMT (3650 days)
Sign the certificate? [y/n]:y


1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entries
Data Base Updated

Nous allons également avoir besoin de clés Diffie Hellman pour procéder à la vérification des certificats de manière sécurisé. Pour les générer, nous allons utiliser le script build-dh et copier tous les fichiers dans /etc/openvpn:

# ./build-dh
Generating DH parameters, 2048 bit long safe prime, generator 2
This is going to take a long time
...

# cd /etc/openvpn/easy-rsa/keys
# cp dh2048.pem ca.crt server.crt server.key /etc/openvpn

Démarrage et enregistrement du serveur

# service openvpn start
# chkconfig openvpn on

Activation du routage

iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
iptables -F FORWARD
service iptables save
vi /etc/sysctl.conf
       net.ipv4.ip_forward = 1

Génération du certificat client

cd /etc/openvpn/easy-rsa
./build-key client
Generating a 2048 bit RSA private key
...........+++
............+++
writing new private key to 'client.key'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [FR]:
State or Province Name (full name) [Herault]:
Locality Name (eg, city) [Juvignac]:
Organization Name (eg, company) [Tala Informatique]:
Organizational Unit Name (eg, section) [Informatique]:
Common Name (eg, your name or your server's hostname) [client]:
Name [EasyRSA]:
Email Address [root@tala.informatique.fr]:

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
Using configuration from /etc/openvpn/easy-rsa/openssl-1.0.0.cnf
Check that the request matches the signature
Signature ok
The Subject's Distinguished Name is as follows
countryName           :PRINTABLE:'FR'
stateOrProvinceName   :PRINTABLE:'Herault'
localityName          :PRINTABLE:'Juvignac'
organizationName      :PRINTABLE:'Tala Informatique'
organizationalUnitName:PRINTABLE:'Informatique'
commonName            :PRINTABLE:'client'
name                  :PRINTABLE:'EasyRSA'
emailAddress          :IA5STRING:'root@tala.informatique.fr'
Certificate is to be certified until Oct 14 17:22:18 2025 GMT (3650 days)
Sign the certificate? [y/n]:y


1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entries
Data Base Updated

Partie cliente

Configuring OpenVPN Client

https://openvpn.net/index.php/open-source/downloads.html