GNU/Linux >> Linux Esercitazione >  >> Debian

Come configurare un server OpenVPN su Debian 10

Una rete privata virtuale (VPN) è una connessione sicura e crittografata tra due reti e singoli utenti che protegge la tua connessione Internet e la privacy online. OpenVPN è un protocollo VPN gratuito e open source che implementa tecniche per creare un punto a punto sicuro in configurazioni instradate. È multipiattaforma e compatibile con tutti i principali sistemi operativi.

In questo tutorial, ti mostreremo come installare e configurare OpenVPN sul server Debian 10.

Prerequisiti

  • Debian 10 VPS (useremo il nostro piano NVMe 2 VPS)
  • Accesso all'account utente root (o accesso a un account amministratore con privilegi root)

Passaggio 1:accedi al server e aggiorna i pacchetti del sistema operativo del server

Innanzitutto, accedi al tuo server Debian 10 tramite SSH come utente root:

ssh root@IP_Address -p Port_number

Dovrai sostituire "IP_Address" e "Port_number" con il rispettivo indirizzo IP e numero di porta SSH del tuo server. Inoltre, se necessario, sostituisci "root" con il nome utente dell'account amministratore.

Prima di iniziare, devi assicurarti che tutti i pacchetti del sistema operativo Debian installati sul server siano aggiornati. Puoi farlo eseguendo i seguenti comandi:

apt-get update -y
apt-get upgrade -y

Fase 2:installa OpenVPN e EasyRSA

Per impostazione predefinita, OpenVPN è incluso nel repository predefinito di Debian. Puoi installarlo con il seguente comando:

apt-get install openvpn -y

Una volta installato il pacchetto OpenVPN, dovrai scaricare EasyRSA sul tuo sistema.

EasyRSA è un'utilità della riga di comando per creare e gestire una CA PKI. Ti consente di generare più tipi di certificati.
Viene utilizzato per creare un'autorità di certificazione radice e richiedere e firmare certificati per OpenVPN.

Puoi scaricare l'ultima versione di EasyRSA dal repository Git usando il seguente comando:

wget https://github.com/OpenVPN/easy-rsa/releases/download/v3.0.8/EasyRSA-3.0.8.tgz

Una volta completato il download, estrai il file scaricato utilizzando il seguente comando:

tar -xvzf EasyRSA-3.0.8.tgz

Quindi, copia la directory estratta nella directory OpenVPN:

cp -r EasyRSA-3.0.8 /etc/openvpn/easy-rsa

A questo punto, OpenVPN e EasyRSA sono installati nel tuo server.

Fase 3:crea l'autorità di certificazione

Successivamente, dovrai creare l'Autorità di certificazione (CA) per OpenVPN.

Innanzitutto, cambia la directory in EasyRSA con il seguente comando:

cd /etc/openvpn/easy-rsa

Successivamente, dovrai creare un file vars all'interno di questo. Un file vars è un semplice file che Easy-RSA creerà per la configurazione.

Puoi crearlo con il seguente comando:

nano vars

Aggiungi le seguenti righe secondo le tue esigenze:

set_var EASYRSA                 "$PWD"
set_var EASYRSA_PKI             "$EASYRSA/pki"
set_var EASYRSA_DN              "cn_only"
set_var EASYRSA_REQ_COUNTRY     "USA"
set_var EASYRSA_REQ_PROVINCE    "Newyork"
set_var EASYRSA_REQ_CITY        "Newyork"
set_var EASYRSA_REQ_ORG         "ROSE CERTIFICATE AUTHORITY"
set_var EASYRSA_REQ_EMAIL    "[email protected]"
set_var EASYRSA_REQ_OU          "ROSE 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    "ROSE 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 e chiudi il file quando hai finito.

Quindi, avvia l'infrastruttura a chiave pubblica con il seguente comando:

./easyrsa init-pki

Dovresti ottenere il seguente output:

Note: using Easy-RSA configuration from: /etc/openvpn/easy-rsa/vars

init-pki complete; you may now create a CA or requests.
Your newly created PKI dir is: /etc/openvpn/easy-rsa/pki

Successivamente, dovrai eseguire il comando build-ca per creare i file ca.crt e ca.key. Puoi eseguirlo con il seguente comando:

./easyrsa build-ca nopass

Ti verranno poste diverse domande come mostrato di seguito:

Note: using Easy-RSA configuration from: /etc/openvpn/easy-rsa/vars
Using SSL: openssl OpenSSL 1.1.1f  31 Mar 2020

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)
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]:vpnserver

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

Passaggio 4:generazione del certificato del server e dei file chiave

Successivamente, dovrai utilizzare il comando gen-req seguito dal nome comune per generare la chiave del server.

./easyrsa gen-req vpnserver nopass

Dovresti vedere il seguente output:

Note: using Easy-RSA configuration from: /etc/openvpn/easy-rsa/vars
Using SSL: openssl OpenSSL 1.1.1f  31 Mar 2020
Generating a RSA private key
.......................................................+++++
....+++++
writing new private key to '/etc/openvpn/easy-rsa/pki/easy-rsa-1428.Angtmh/tmp.C9prw4'
-----
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) [vpnserver]:

Keypair and certificate request completed. Your files are:
req: /etc/openvpn/easy-rsa/pki/reqs/vpnserver.req
key: /etc/openvpn/easy-rsa/pki/private/vpnserver.key

Successivamente, dovrai firmare la chiave vpnserver utilizzando il tuo certificato CA. Puoi farlo con il seguente comando:

./easyrsa sign-req server vpnserver

Dovresti ottenere il seguente output:

Check that the request matches the signature
Signature ok
The Subject's Distinguished Name is as follows
commonName            :ASN.1 12:'vpnserver'
Certificate is to be certified until Feb  6 14:38:52 2022 GMT (365 days)

Write out database with 1 new entries
Data Base Updated

Certificate created at: /etc/openvpn/easy-rsa/pki/issued/vpnserver.crt

Successivamente, dovrai generare una chiave Diffie-Hellman forte da utilizzare per lo scambio di chiavi. Puoi generarlo con il seguente comando:

./easyrsa gen-dh

Fase 5:copia tutto il certificato e il file chiave

Successivamente, dovrai copiare tutto il certificato e il file della chiave nella directory /etc/openvpn/server/. Puoi copiare usando il seguente comando:

cp pki/ca.crt /etc/openvpn/server/
cp pki/dh.pem /etc/openvpn/server/
cp pki/private/vpnserver.key /etc/openvpn/server/
cp pki/issued/vpnserver.crt /etc/openvpn/server/

Fase 6:crea il certificato client e il file chiave

Successivamente, dovrai generare un certificato e un file chiave per il sistema client.

Puoi crearlo con il seguente comando:

./easyrsa gen-req vpnclient nopass

Dovresti ottenere il seguente output:

Note: using Easy-RSA configuration from: /etc/openvpn/easy-rsa/vars
Using SSL: openssl OpenSSL 1.1.1f  31 Mar 2020
Generating a RSA private key
....+++++
.................................+++++
writing new private key to '/etc/openvpn/easy-rsa/pki/easy-rsa-1563.TeOf5v/tmp.i4YxLz'
-----
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) [vpnclient]:

Keypair and certificate request completed. Your files are:
req: /etc/openvpn/easy-rsa/pki/reqs/vpnclient.req
key: /etc/openvpn/easy-rsa/pki/private/vpnclient.key

Quindi, firma la chiave client con il seguente comando:

./easyrsa sign-req client vpnclient

Dovresti ottenere il seguente output:

Signature ok
The Subject's Distinguished Name is as follows
commonName            :ASN.1 12:'vpnclient'
Certificate is to be certified until Feb  6 14:43:18 2022 GMT (365 days)

Write out database with 1 new entries
Data Base Updated

Certificate created at: /etc/openvpn/easy-rsa/pki/issued/vpnclient.crt

Quindi, copia tutti i certificati e le chiavi del client nella directory /etc/openvpn/client/.

cp pki/ca.crt /etc/openvpn/client/
cp pki/issued/vpnclient.crt /etc/openvpn/client/
cp pki/private/vpnclient.key /etc/openvpn/client/

Passaggio 7:configura il server OpenVPN

A questo punto, il certificato e la chiave sia del server che del client sono pronti. Ora dovrai creare un file di configurazione OpenVPN e definire tutti i certificati e le chiavi.

nano /etc/openvpn/server.conf

Aggiungi le seguenti righe:

port 1194
proto udp
dev tun
ca /etc/openvpn/server/ca.crt
cert /etc/openvpn/server/vpnserver.crt
key /etc/openvpn/server/vpnserver.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 nogroup
log-append /var/log/openvpn.log
verb 3

Salva e chiudi il file, quindi avvia il servizio OpenVPN e abilitalo all'avvio al riavvio del sistema:

systemctl start openvpn@server
systemctl enable openvpn@server

Se tutto va bene, verrà creata una nuova interfaccia. Puoi verificarlo usando il seguente comando:

ip a show tun0

Dovresti ottenere il seguente output:

4: tun0:  mtu 1500 qdisc fq_codel state UNKNOWN group default qlen 100
    link/none 
    inet 10.8.0.1 peer 10.8.0.2/32 scope global tun0
       valid_lft forever preferred_lft forever
    inet6 fe80::153d:f29:39a2:571a/64 scope link stable-privacy 
       valid_lft forever preferred_lft forever

Passaggio 8:abilita l'inoltro IP

L'inoltro IP consente al sistema operativo di accettare i pacchetti di rete in entrata e di inoltrarli all'altra rete. Puoi abilitarlo con il seguente comando:

nano /etc/sysctl.conf

Decommenta o aggiungi la seguente riga:

net.ipv4.ip_forward = 1

Salva il file quindi applica le modifiche alla configurazione con il seguente comando:

sysctl -p

Passaggio 9:installa e configura il client OpenVPN

Successivamente, dovrai installare il client OpenVPN su un altro sistema e connetterti al server OpenVPN.

Innanzitutto, installa OpenVPN con il seguente comando:

apt-get install openvpn -y

Una volta installato, copia tutti i certificati e le chiavi del client dal server OpenVPN alla macchina del client. Puoi farlo con il seguente comando:

scp -r root@vpn-server-ip:/etc/openvpn/client .

Quindi, cambia la directory in client e crea un file di configurazione del client:

cd client
nano client.ovpn

Aggiungi le seguenti righe:

client
dev tun
proto udp
remote vpn-server-ip 1194
ca ca.crt
cert vpnclient.crt
key vpnclient.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 e chiudi il file, quindi connettiti al tuo server OpenVPN con il seguente comando:

openvpn --config client.ovpn

Una volta stabilita la connessione, dovresti ottenere il seguente output:

Sat Feb  6 14:53:50 2021 SENT CONTROL [vpnserver]: 'PUSH_REQUEST' (status=1)
Sat Feb  6 14:53:50 2021 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'
Sat Feb  6 14:53:50 2021 OPTIONS IMPORT: timers and/or timeouts modified
Sat Feb  6 14:53:50 2021 OPTIONS IMPORT: --ifconfig/up options modified
Sat Feb  6 14:53:50 2021 OPTIONS IMPORT: route options modified

Puoi verificare l'interfaccia OpenVPN sul computer client con il seguente comando:

ip a show tun0

Dovresti ottenere il seguente output:

4: tun0:  mtu 1500 qdisc pfifo_fast state UNKNOWN group default qlen 100
    link/none 
    inet 10.8.0.6 peer 10.8.0.5/32 scope global tun0
       valid_lft forever preferred_lft forever
    inet6 fe80::9206:94d7:8fb2:6b21/64 scope link stable-privacy 
       valid_lft forever preferred_lft forever

Ovviamente, non devi installare OpenVPN se utilizzi uno dei nostri servizi di hosting OpenVPN gestito, nel qual caso puoi semplicemente chiedere ai nostri esperti amministratori Linux di installarlo per te. Sono disponibili 24 ore su 24, 7 giorni su 7 e si prenderanno immediatamente cura della tua richiesta.

PS . Se ti è piaciuto questo post su come installare OpenVPN su Ubuntu, condividilo con i tuoi amici sui social network usando i pulsanti a sinistra o lascia semplicemente una risposta qui sotto. Grazie.


Debian
  1. Come configurare un server Samba su Debian 10 Buster

  2. Come configurare un server LAMP su Debian 10 Buster

  3. Come installare e configurare OpenVPN Server su Debian 10

  4. Impostare un IP statico su Debian 11 - Come farlo?

  5. Come configurare un server SFTP su Debian 11 Server

Come configurare un server di posta con PostfixAdmin su Debian 11

Come configurare un server di posta con Modoboa su Debian 10

Come impostare un IP statico su Debian 11

Come configurare il server Rsyslog Debian 10/11

Come configurare server e client NTP su Debian 11

Come installare il server OpenVPN su Debian 11