Una VPN è anche nota come "Rete privata virtuale" è una tecnologia che consente di creare una rete privata sicura su Internet pubblico. Questo ti aiuterà a crittografare il tuo traffico Internet e proteggere la tua identità online. Viene utilizzato principalmente per connettere due reti aziendali in modo sicuro su Internet e consente di collegare una rete aziendale da casa.
In parole povere, una VPN connette il tuo cellulare, computer e tablet a un altro computer (server) su Internet e ti consente di accedere a Internet utilizzando l'Internet di quel computer e anche di accedere alla risorsa di rete locale aggirando la censura di Internet da remoto.
In questo tutorial, ti mostreremo come installare e configurare il server OpenVPN su CentOS 8.
Fase 1 – Disattiva SELinux
Prima di iniziare, è una buona idea disabilitare SELinux nel tuo sistema.
Per disabilitare SELinux, apri il file /etc/selinux/config:
nano /etc/selinux/config
Modifica la seguente riga:
SELINUX=disabled
Salva il file quando hai finito. Quindi, riavvia il sistema per applicare le modifiche a SELinux.
Passaggio 2:abilitare l'inoltro IP
Successivamente, dovrai abilitare l'inoltro IP nel tuo sistema. L'inoltro IP consente al tuo sistema operativo di accettare i pacchetti di rete in entrata e di inoltrarli all'altra rete se la destinazione è su un'altra rete.
Per abilitare l'inoltro IP, modificare il file /etc/sysctl.conf:
nano /etc/sysctl.conf
Aggiungi la seguente riga:
net.ipv4.ip_forward = 1
Salva il file quando hai finito. Quindi, esegui il comando seguente per applicare le modifiche:
sysctl -p
Fase 3:installa il server OpenVPN
Per impostazione predefinita, dovrai installare il repository EPEL nel tuo sistema per installare l'ultima versione di OpenVPN.
Eseguire il comando seguente per installare il repository ELEP:
dnf install epel-release -y
Una volta installato, esegui il seguente comando per installare l'ultima versione di OpenVPN:
dnf install openvpn -y
Una volta completata l'installazione, dovrai anche scaricare easy-rsa per la gestione dei certificati SSL.
Esegui il comando seguente per scaricare easy-rsa all'interno della directory /etc/openvpn.
cd /etc/openvpn wget https://github.com/OpenVPN/easy-rsa/releases/download/v3.0.6/EasyRSA-unix-v3.0.6.tgz
Quindi, esegui il comando seguente per estrarre il file scaricato:
tar -xvzf EasyRSA-unix-v3.0.6.tgz
Quindi, rinomina la directory estratta in easy-rsa:
mv EasyRSA-v3.0.6 easy-rsa
Passaggio 4:impostazione dell'autorità di certificazione
Easy RSA utilizza una serie di script per generare chiavi e certificati. Innanzitutto, dovrai configurare l'Autorità di certificazione sul tuo sistema.
Per farlo, cambia la directory in /etc/openvpn/easy-rsa e crea un nuovo file di configurazione di Easy RSA:
cd /etc/openvpn/easy-rsa nano vars
Aggiungi le seguenti righe includendo il tuo paese, città e indirizzo email preferito:
set_var EASYRSA "$PWD" set_var EASYRSA_PKI "$EASYRSA/pki" set_var EASYRSA_DN "cn_only" set_var EASYRSA_REQ_COUNTRY "INDIA" set_var EASYRSA_REQ_PROVINCE "Gujrat" set_var EASYRSA_REQ_CITY "Ahmedabad" set_var EASYRSA_REQ_ORG "Tecadmin CERTIFICATE AUTHORITY" set_var EASYRSA_REQ_EMAIL "[email protected]" set_var EASYRSA_REQ_OU "Tecadmin EASY CA" set_var EASYRSA_KEY_SIZE 2048 set_var EASYRSA_ALGO rsa set_var EASYRSA_CA_EXPIRE 7500 set_var EASYRSA_CERT_EXPIRE 365 set_var EASYRSA_NS_SUPPORT "no" set_var EASYRSA_NS_COMMENT "Tecadmin CERTIFICATE AUTHORITY" set_var EASYRSA_EXT_DIR "$EASYRSA/x509-types" set_var EASYRSA_SSL_CONF "$EASYRSA/openssl-easyrsa.cnf" set_var EASYRSA_DIGEST "sha256"
Salva il file quando hai finito.
Quindi, esegui il comando seguente per avviare la directory PKI.
./easyrsa init-pki
Uscita:
Note: using Easy-RSA configuration from: ./vars init-pki complete; you may now create a CA or requests. Your newly created PKI dir is: /etc/openvpn/easy-rsa/pki
Quindi, crea i certificati CA con il seguente comando:
./easyrsa build-ca
Dovresti ottenere il seguente output:
Note: using Easy-RSA configuration from: ./vars Using SSL: openssl OpenSSL 1.1.1c FIPS 28 May 2019 Enter New CA Key Passphrase: Re-Enter New CA Key Passphrase: Generating RSA private key, 2048 bit long modulus (2 primes) ....................................................................+++++ ..........................................................................................................................................+++++ e is 65537 (0x010001) Can't load /etc/openvpn/easy-rsa/pki/.rnd into RNG 140218549745472:error:2406F079:random number generator:RAND_load_file:Cannot open file:crypto/rand/randfile.c:98:Filename=/etc/openvpn/easy-rsa/pki/.rnd 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. ----- Common Name (eg: your user, host, or server name) [Easy-RSA CA]: CA creation complete and you may now import and sign cert requests. Your new CA certificate file for publishing is at: /etc/openvpn/easy-rsa/pki/ca.crt
Il comando precedente genererà due file denominati ca.key e ca.crt. Questi certificati verranno utilizzati per firmare i certificati del server e dei client.
Passaggio 5 – Genera file di certificato del server
Successivamente, dovrai generare una coppia di chiavi e una richiesta di certificato per il tuo server.
Eseguire il comando seguente per generare la chiave del server denominata tecadmin-server:
./easyrsa gen-req tecadmin-server nopass
Dovresti ottenere il seguente output:
Note: using Easy-RSA configuration from: ./vars Using SSL: openssl OpenSSL 1.1.1c FIPS 28 May 2019 Generating a RSA private key ...........................+++++ ...............................................................................................................................................................................................................................................................................................+++++ writing new private key to '/etc/openvpn/easy-rsa/pki/private/tecadmin-server.key.kOlBTwtY6a' ----- 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. ----- Common Name (eg: your user, host, or server name) [tecadmin-server]: Keypair and certificate request completed. Your files are: req: /etc/openvpn/easy-rsa/pki/reqs/tecadmin-server.req key: /etc/openvpn/easy-rsa/pki/private/tecadmin-server.key
Fase 6:firma la chiave del server utilizzando la CA
Successivamente, dovrai firmare la chiave tecadmin-server utilizzando il tuo certificato CA:
Esegui il comando seguente per firmare la chiave del server:
./easyrsa sign-req server tecadmin-server
Dovresti ottenere il seguente output:
Note: using Easy-RSA configuration from: ./vars Using SSL: openssl OpenSSL 1.1.1c FIPS 28 May 2019 You are about to sign the following certificate. Please check over the details shown below for accuracy. Note that this request has not been cryptographically verified. Please be sure it came from a trusted source or that you have verified the request checksum with the sender. Request subject, to be signed as a server certificate for 365 days: subject= commonName = tecadmin-server Type the word 'yes' to continue, or any other input to abort. Confirm request details: yes Using configuration from /etc/openvpn/easy-rsa/pki/safessl-easyrsa.cnf Enter pass phrase for /etc/openvpn/easy-rsa/pki/private/ca.key: Check that the request matches the signature Signature ok The Subject's Distinguished Name is as follows commonName :ASN.1 12:'tecadmin-server' Certificate is to be certified until Feb 16 05:00:50 2021 GMT (365 days) Write out database with 1 new entries Data Base Updated Certificate created at: /etc/openvpn/easy-rsa/pki/issued/tecadmin-server.crt
Successivamente, verifica il file del certificato generato con il seguente comando:
openssl verify -CAfile pki/ca.crt pki/issued/tecadmin-server.crt
Se tutto va bene, dovresti ottenere il seguente output:
pki/issued/tecadmin-server.crt: OK
Quindi, esegui il comando seguente per generare una chiave Diffie-Hellman forte da utilizzare per lo scambio di chiavi:
./easyrsa gen-dh
Dovresti ottenere il seguente output:
Note: using Easy-RSA configuration from: ./vars Using SSL: openssl OpenSSL 1.1.1c FIPS 28 May 2019 Generating DH parameters, 2048 bit long safe prime, generator 2 This is going to take a long time ....+................................+........................................+.....++*++*++*++* DH parameters of size 2048 created at /etc/openvpn/easy-rsa/pki/dh.pem
Dopo aver creato tutti i file del certificato, copiali nella directory /etc/openvpn/server/:
cp pki/ca.crt /etc/openvpn/server/ cp pki/dh.pem /etc/openvpn/server/ cp pki/private/tecadmin-server.key /etc/openvpn/server/ cp pki/issued/tecadmin-server.crt /etc/openvpn/server/
Passaggio 7:generazione del certificato client e del file chiave
Successivamente, dovrai generare la chiave e il file del certificato per il client.
Innanzitutto, esegui il comando seguente per creare il file della chiave del client:
./easyrsa gen-req client nopass
Dovresti vedere il seguente output:
Note: using Easy-RSA configuration from: ./vars Using SSL: openssl OpenSSL 1.1.1c FIPS 28 May 2019 Generating a RSA private key ......................................................+++++ ...+++++ writing new private key to '/etc/openvpn/easy-rsa/pki/private/client.key.e38GUtzHie' ----- 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. ----- Common Name (eg: your user, host, or server name) [client]: Keypair and certificate request completed. Your files are: req: /etc/openvpn/easy-rsa/pki/reqs/client.req key: /etc/openvpn/easy-rsa/pki/private/client.key
Quindi, firma la chiave client utilizzando il tuo certificato CA:
./easyrsa sign-req client client
Dovresti ottenere il seguente output:
Note: using Easy-RSA configuration from: ./vars Using SSL: openssl OpenSSL 1.1.1c FIPS 28 May 2019 You are about to sign the following certificate. Please check over the details shown below for accuracy. Note that this request has not been cryptographically verified. Please be sure it came from a trusted source or that you have verified the request checksum with the sender. Request subject, to be signed as a client certificate for 365 days: subject= commonName = client Type the word 'yes' to continue, or any other input to abort. Confirm request details: yes Using configuration from /etc/openvpn/easy-rsa/pki/safessl-easyrsa.cnf Enter pass phrase for /etc/openvpn/easy-rsa/pki/private/ca.key: Check that the request matches the signature Signature ok The Subject's Distinguished Name is as follows commonName :ASN.1 12:'client' Certificate is to be certified until Feb 16 05:11:19 2021 GMT (365 days) Write out database with 1 new entries Data Base Updated Certificate created at: /etc/openvpn/easy-rsa/pki/issued/client.crt
Quindi, copia tutto il certificato client e il file della chiave nella directory /etc/openvpn/client/:
cp pki/ca.crt /etc/openvpn/client/ cp pki/issued/client.crt /etc/openvpn/client/ cp pki/private/client.key /etc/openvpn/client/
Passaggio 8:configurazione del server OpenVPN
Quindi, crea un nuovo file di configurazione OpenVPN all'interno della directory /etc/openvpn/client/:
nano /etc/openvpn/server/server.conf
Aggiungi le seguenti righe:
port 1194 proto udp dev tun ca /etc/openvpn/server/ca.crt cert /etc/openvpn/server/tecadmin-server.crt key /etc/openvpn/server/tecadmin-server.key dh /etc/openvpn/server/dh.pem server 10.8.0.0 255.255.255.0 push "redirect-gateway def1" push "dhcp-option DNS 208.67.222.222" push "dhcp-option DNS 208.67.220.220" duplicate-cn cipher AES-256-CBC tls-version-min 1.2 tls-cipher TLS-DHE-RSA-WITH-AES-256-GCM-SHA384:TLS-DHE-RSA-WITH-AES-256-CBC-SHA256:TLS-DHE-RSA-WITH-AES-128-GCM-SHA256:TLS-DHE-RSA-WITH-AES-128-CBC-SHA256 auth SHA512 auth-nocache keepalive 20 60 persist-key persist-tun compress lz4 daemon user nobody group nobody log-append /var/log/openvpn.log verb 3
Salva il file quando hai finito.
Fase 9:avvia il servizio OpenVPN
OpenVPN è ora installato e configurato. Ora puoi avviare il servizio OpenVPN e abilitarlo all'avvio dopo il riavvio del sistema utilizzando il seguente comando:
systemctl start [email protected] systemctl enable [email protected]
Esegui il comando seguente per verificare lo stato del servizio OpenVPN:
systemctl status [email protected]
Dovresti ottenere il seguente output:
● [email protected] - OpenVPN service for server Loaded: loaded (/usr/lib/systemd/system/[email protected]; enabled; vendor preset: disabled) Active: active (running) since Mon 2020-02-17 00:29:26 EST; 39min ago Docs: man:openvpn(8) https://community.openvpn.net/openvpn/wiki/Openvpn24ManPage https://community.openvpn.net/openvpn/wiki/HOWTO Main PID: 32405 (openvpn) Status: "Initialization Sequence Completed" Tasks: 1 (limit: 12552) Memory: 1.9M CGroup: /system.slice/system-openvpn\x2dserver.slice/[email protected] └─32405 /usr/sbin/openvpn --status /run/openvpn-server/status-server.log --status-version 2 --suppress-timestamps --cipher AES-256-> Feb 17 00:29:26 centos8 systemd[1]: Starting OpenVPN service for server... Feb 17 00:29:26 centos8 systemd[1]: Started OpenVPN service for server.
Una volta avviato correttamente il servizio OpenVPN, creerà una nuova interfaccia di rete denominata tun0. Puoi verificarlo con il seguente comando:
ifconfig
Dovresti ottenere la nuova interfaccia tun0 nel seguente output:
eth0: flags=4163mtu 1500 inet 104.245.36.127 netmask 255.255.255.0 broadcast 104.245.36.255 inet6 fe80::200:68ff:fef5:247f prefixlen 64 scopeid 0x20 ether 00:00:68:f5:24:7f txqueuelen 1000 (Ethernet) RX packets 1926738 bytes 314886412 (300.2 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 174907 bytes 29557250 (28.1 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 lo: flags=73 mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10 loop txqueuelen 1000 (Local Loopback) RX packets 216 bytes 40041 (39.1 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 216 bytes 40041 (39.1 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 tun0: flags=4305 mtu 1500 inet 10.8.0.1 netmask 255.255.255.255 destination 10.8.0.2 inet6 fe80::4152:a673:b260:d9e6 prefixlen 64 scopeid 0x20 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 4 bytes 304 (304.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
Fase 10 – Genera file di configurazione client
Quindi, crea un nuovo file di configurazione del client OpenVPN denominato client.ovpn. Avrai bisogno di questo file per connettere il tuo server OpenVPN dal sistema client.
nano /etc/openvpn/client/client.ovpn
Aggiungi le seguenti righe:
client dev tun proto udp remote vpn-server-ip 1194 ca ca.crt cert client.crt key client.key cipher AES-256-CBC auth SHA512 auth-nocache tls-version-min 1.2 tls-cipher TLS-DHE-RSA-WITH-AES-256-GCM-SHA384:TLS-DHE-RSA-WITH-AES-256-CBC-SHA256:TLS-DHE-RSA-WITH-AES-128-GCM-SHA256:TLS-DHE-RSA-WITH-AES-128-CBC-SHA256 resolv-retry infinite compress lz4 nobind persist-key persist-tun mute-replay-warnings verb 3
Salva il file quando hai finito.
Fase 11:configura il routing utilizzando Firewalld
Innanzitutto, dovrai consentire il servizio OpenVPN tramite firewalld.
Per fare ciò, aggiungi il servizio OpenVPN e l'interfaccia tun0 all'area firewall attendibile:
firewall-cmd --permanent --add-service=openvpn firewall-cmd --permanent --zone=trusted --add-service=openvpn firewall-cmd --permanent --zone=trusted --add-interface=tun0
Quindi, aggiungi MASQUERADE nella zona predefinita:
firewall-cmd --add-masquerade firewall-cmd --permanent --add-masquerade
Quindi, esegui il comando seguente per mascherare il traffico Internet proveniente dalla rete VPN (10.8.0.0/24) all'interfaccia di rete locale del sistema (eth0).
tecadmin=$(ip route get 8.8.8.8 | awk 'NR==1 {print $(NF-2)}') firewall-cmd --permanent --direct --passthrough ipv4 -t nat -A POSTROUTING -s 10.8.0.0/24 -o $tecadmin -j MASQUERADE
Infine, esegui il comando seguente per implementare le modifiche:
firewall-cmd --reload
Passaggio 12:connetti OpenVPN dai client
Innanzitutto, accedi al computer client e installa il pacchetto OpenVPN con il seguente comando:
dnf install epel-release -y dnf install openvpn -y
Successivamente, dovrai scaricare i file di configurazione del client OpenVPN dal server OpenVPN al computer client.
Sul computer client, eseguire il comando seguente per scaricare tutto il file di configurazione del client:
scp -r [email protected]:/etc/openvpn/client .
Una volta scaricato, cambia la directory in client ed esegui il seguente comando per connetterti al server OpenVPN:
cd client openvpn --config client.ovpn
Dovresti vedere il seguente output:
Mon Feb 17 00:54:17 2020 VERIFY OK: depth=0, CN=tecadmin-server Mon Feb 17 00:54:17 2020 Control Channel: TLSv1.3, cipher TLSv1.3 TLS_AES_256_GCM_SHA384, 2048 bit RSA Mon Feb 17 00:54:17 2020 [tecadmin-server] Peer Connection Initiated with [AF_INET]104.245.36.127:1194 Mon Feb 17 00:54:18 2020 SENT CONTROL [tecadmin-server]: 'PUSH_REQUEST' (status=1) Mon Feb 17 00:54:18 2020 PUSH: Received control message: 'PUSH_REPLY,redirect-gateway def1,dhcp-option DNS 208.67.222.222,dhcp-option DNS 208.67.220.220,route 10.8.0.1,topology net30,ping 20,ping-restart 60,ifconfig 10.8.0.6 10.8.0.5,peer-id 0,cipher AES-256-GCM' Mon Feb 17 00:54:18 2020 OPTIONS IMPORT: timers and/or timeouts modified Mon Feb 17 00:54:18 2020 OPTIONS IMPORT: --ifconfig/up options modified Mon Feb 17 00:54:18 2020 OPTIONS IMPORT: route options modified Mon Feb 17 00:54:18 2020 OPTIONS IMPORT: --ip-win32 and/or --dhcp-option options modified Mon Feb 17 00:54:18 2020 OPTIONS IMPORT: peer-id set Mon Feb 17 00:54:18 2020 OPTIONS IMPORT: adjusting link_mtu to 1625 Mon Feb 17 00:54:18 2020 OPTIONS IMPORT: data channel crypto options modified Mon Feb 17 00:54:18 2020 Data Channel: using negotiated cipher 'AES-256-GCM' Mon Feb 17 00:54:18 2020 Outgoing Data Channel: Cipher 'AES-256-GCM' initialized with 256 bit key Mon Feb 17 00:54:18 2020 Incoming Data Channel: Cipher 'AES-256-GCM' initialized with 256 bit key Mon Feb 17 00:54:18 2020 ROUTE_GATEWAY 45.58.38.1/255.255.255.0 IFACE=eth0 HWADDR=00:00:2d:3a:26:d9 Mon Feb 17 00:54:18 2020 TUN/TAP device tun0 opened Mon Feb 17 00:54:18 2020 TUN/TAP TX queue length set to 100 Mon Feb 17 00:54:18 2020 /sbin/ip link set dev tun0 up mtu 1500 Mon Feb 17 00:54:18 2020 /sbin/ip addr add dev tun0 local 10.8.0.6 peer 10.8.0.5
Dopo una connessione riuscita, OpenVPN assegnerà un indirizzo IP al tuo sistema. Puoi verificarlo con il seguente comando:
ifconfig tun0
Uscita:
tun0: flags=4305mtu 1500 inet 10.8.0.6 netmask 255.255.255.255 destination 10.8.0.5 inet6 fe80::c6b:2644:5142:8e7f prefixlen 64 scopeid 0x20 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 2 bytes 96 (96.0 B) TX errors 0 dropped 0 overruns 0 carrier 0
Congratulazioni! hai installato e configurato con successo OpenVPN Server and Client sul server CentOS 8.