GNU/Linux >> Belajar Linux >  >> Linux

Cara Menginstal dan Mengonfigurasi Server OpenVPN di Ubuntu 20.04

OpenVPN adalah solusi VPN sumber terbuka untuk bisnis yang menggunakan teknik jaringan pribadi virtual untuk membuat koneksi point-to-point yang aman dengan fasilitas akses jarak jauh. OpenVPN adalah teknologi tepercaya yang digunakan oleh banyak VPN untuk memastikan data apa pun yang dikirim melalui internet dienkripsi dan bersifat pribadi.

Secara sederhana, OpenVPN memungkinkan Anda untuk terhubung ke perangkat lain dalam satu jaringan aman. Ini adalah lintas platform dan dapat berupa Windows, Mac, Android, iOS, dan Linux. OpenVPN menawarkan serangkaian fitur yang kaya termasuk:

  • Bandwidth Tidak Terbatas
  • Sakelar Server Tidak Terbatas
  • Penggunaan Multi-Perangkat
  • Tidak Ada Penyimpanan Log
  • Pilihan Protokol
  • Beberapa Koneksi Serentak

Dalam tutorial ini, kita akan belajar cara menginstal dan mengkonfigurasi server dan klien OpenVPN pada VPS Ubuntu 20.04.

Prasyarat

  • VPS Ubuntu 20.04 baru di Platform Cloud Atlantic.net.
  • Kata sandi root dikonfigurasi di server Anda.

Langkah 1:Buat Server Cloud Atlantic.Net

Pertama, masuk ke Server Cloud Atlantic.Net Anda. Buat server baru, pilih Ubuntu 20.04 sebagai sistem operasi, dengan setidaknya 1GB RAM. Hubungkan ke Server Cloud Anda melalui SSH dan masuk menggunakan kredensial yang disorot di bagian atas halaman.

Setelah Anda masuk ke server Ubuntu 20.04 Anda, jalankan perintah berikut untuk memperbarui sistem dasar Anda dengan paket terbaru yang tersedia.

apt-get update -y

Langkah 2:Aktifkan Penerusan IP

Selanjutnya, Anda harus mengaktifkan penerusan IP di sistem Anda sehingga OpenVPN dapat merutekan lalu lintas melalui VPN dengan benar.

Anda dapat mengaktifkan penerusan IP dengan mengedit file /etc/sysctl.conf:

nano /etc/sysctl.conf

Batalkan komentar pada baris berikut:

net.ipv4.ip_forward = 1

Simpan file setelah Anda selesai, lalu jalankan perintah berikut untuk menerapkan perubahan:

sysctl -p

Langkah 3:Instal Server OpenVPN

apt-get install openvpn -y

Setelah penginstalan selesai, Anda dapat melanjutkan ke langkah berikutnya.

Langkah 4:Bangun Otoritas Sertifikat

Untuk mengatur otoritas sertifikat dan infrastruktur PKI, Anda perlu mengunduh EasyRSA di sistem Anda. Anda dapat mengunduhnya dengan perintah berikut:

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

Setelah diunduh, ekstrak file yang diunduh dengan perintah berikut:

tar -xvzf EasyRSA-unix-v3.0.6.tgz

Selanjutnya, pindahkan direktori hasil ekstrak ke /etc/openvpn/ dengan perintah berikut:

mv EasyRSA-v3.0.6 /etc/openvpn/easy-rsa

Selanjutnya, ubah direktori ke /etc/openvpn/easy-rsa dan buat file konfigurasi EasyRSA:

cd /etc/openvpn/easy-rsa
nano vars

Tambahkan baris berikut termasuk negara, kota, dan alamat email pilihan Anda:

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    "Gujarat"
set_var EASYRSA_REQ_CITY        "Junagadh"
set_var EASYRSA_REQ_ORG         "Atlantic CERTIFICATE AUTHORITY"
set_var EASYRSA_REQ_EMAIL     "[email protected]"
set_var EASYRSA_REQ_OU          "Atlantic 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            "Atlantic CERTIFICATE AUTHORITY"
set_var EASYRSA_EXT_DIR         "$EASYRSA/x509-types"
set_var EASYRSA_SSL_CONF        "$EASYRSA/openssl-easyrsa.cnf"
set_var EASYRSA_DIGEST          "sha256"

Simpan file setelah Anda selesai.

Selanjutnya, inisiasi direktori PKI menggunakan perintah berikut:

./easyrsa init-pki

Anda akan mendapatkan output berikut:

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

Selanjutnya, buat sertifikat CA dengan perintah berikut:

./easyrsa build-ca

Anda akan mendapatkan output berikut:

Note: using Easy-RSA configuration from: ./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)
Can't load /etc/openvpn/easy-rsa/pki/.rnd into RNG
139636302492992: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

Perintah di atas akan menghasilkan dua file bernama ca.key dan ca.crt. Sertifikat ini akan digunakan untuk menandatangani sertifikat server dan klien Anda.

Langkah 5:Buat File Sertifikat Server

Selanjutnya, Anda perlu membuat keypair dan permintaan sertifikat untuk server Anda.

Jalankan perintah berikut untuk menghasilkan kunci server bernama atlantic-server:

./easyrsa gen-req atlantic-server nopass

Anda akan melihat output berikut:

Note: using Easy-RSA configuration from: ./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/private/atlantic-server.key.IMonKybM0y'
-----
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) [atlantic-server]:

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

Ini akan menghasilkan kunci pribadi dan file permintaan sertifikat untuk server.

Langkah 6:Masuk Server Key Menggunakan CA

Selanjutnya, Anda perlu menandatangani kunci server atlantic menggunakan sertifikat CA Anda:

Anda dapat menandatangani kunci server menggunakan perintah berikut:

./easyrsa sign-req server atlantic-server

Anda akan melihat output berikut:

Note: using Easy-RSA configuration from: ./vars

Using SSL: openssl OpenSSL 1.1.1f  31 Mar 2020

You are about to sign the following certi
ficate.
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                = atlantic-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:'atlantic-server'
Certificate is to be certified until Jun 29 11:43:05 2021 GMT (365 days)

Write out database with 1 new entries
Data Base Updated

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

Selanjutnya, verifikasi file sertifikat yang dihasilkan dengan perintah berikut:

openssl verify -CAfile pki/ca.crt pki/issued/atlantic-server.crt

Anda akan mendapatkan output berikut:

pki/issued/atlantic-server.crt: OK

Selanjutnya, jalankan perintah berikut untuk menghasilkan kunci Diffie-Hellman yang kuat untuk digunakan untuk pertukaran kunci:

./easyrsa gen-dh

Anda akan mendapatkan output berikut:

Note: using Easy-RSA configuration from: ./vars

Using SSL: openssl OpenSSL 1.1.1f  31 Mar 2020
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

Setelah membuat semua file sertifikat, salin ke direktori /etc/openvpn/server/:

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

Langkah 7:Buat Sertifikat Klien dan File Kunci

Selanjutnya, Anda perlu membuat file kunci dan sertifikat untuk klien.

Pertama, jalankan perintah berikut untuk membuat file kunci klien:

./easyrsa gen-req client nopass

Anda akan mendapatkan output berikut:

Note: using Easy-RSA configuration from: ./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/private/client.key.JmBal6cmr8'
-----
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

Selanjutnya, tanda tangani kunci klien menggunakan sertifikat CA Anda:

./easyrsa sign-req client client

Anda akan mendapatkan output berikut:

Note: using Easy-RSA configuration from: ./vars

Using SSL: openssl OpenSSL 1.1.1f  31 Mar 2020

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 Jun 29 11:46:58 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

Selanjutnya, salin semua sertifikat klien dan file kunci ke direktori /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/

Langkah 8:Konfigurasi Server OpenVPN

Pada titik ini, semua sertifikat dan file kunci sudah siap. Selanjutnya, buat file konfigurasi OpenVPN baru di dalam direktori /etc/openvpn/:

nano /etc/openvpn/server.conf

Tambahkan baris berikut sesuai dengan sertifikat dan jalur kunci Anda:

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

Simpan file ketika Anda selesai, kemudian mulai layanan OpenVPN dan aktifkan untuk memulai setelah sistem reboot menggunakan perintah berikut:

systemctl start [email protected]
systemctl enable [email protected]

Jalankan perintah berikut untuk memverifikasi status layanan OpenVPN:

systemctl status [email protected]

Anda akan mendapatkan output berikut:

Loaded: loaded (/lib/systemd/system/[email protected]; disabled; vendor preset: enabled)
Active: active (running) since Mon 2020-06-29 11:48:25 UTC; 7s ago
Docs: man:openvpn(8)
https://community.openvpn.net/openvpn/wiki/Openvpn24ManPage
https://community.openvpn.net/openvpn/wiki/HOWTO
Main PID: 2868 (openvpn)
Status: "Initialization Sequence Completed"
Tasks: 1 (limit: 2353)
Memory: 2.0M
CGroup: /system.slice/system-openvpn.slice/[email protected]
└─2868 /usr/sbin/openvpn --daemon ovpn-server --status /run/openvpn/server.status 10 --cd /etc/openvpn --script-security 2 --conf>

Jun 29 11:48:25 vpnserver systemd[1]: Starting OpenVPN connection to server...
Jun 29 11:48:25 vpnserver systemd[1]: Started OpenVPN connection to server.

Setelah layanan OpenVPN berhasil dimulai, itu akan membuat antarmuka jaringan baru bernama tun0. Anda dapat memverifikasinya dengan perintah berikut:

ip a show tun0

Anda harus mendapatkan antarmuka baru tun0 dalam output berikut:

4: tun0: <POINTOPOINT,MULTICAST,NOARP,UP,LOWER_UP> 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::5f83:99a:30d:eb0/64 scope link stable-privacy
valid_lft forever preferred_lft forever

Langkah 9:Buat File Konfigurasi Klien

Selanjutnya, Anda perlu membuat file konfigurasi klien OpenVPN bernama client.ovpn. Anda memerlukan file ini untuk menghubungkan server OpenVPN Anda dari sistem klien.

nano /etc/openvpn/client/client.ovpn

Tambahkan baris berikut:

client
dev tun
proto udp
remote your-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

Simpan file setelah Anda selesai.

Langkah 10:Instal dan Konfigurasi Klien OpenVPN

Selanjutnya, Anda perlu menginstal OpenVPN pada sistem klien dan menghubungkan ke server OpenVPN.

Pertama, masuk ke mesin klien dan instal paket OpenVPN dengan perintah berikut:

apt-get install openvpn -y

Selanjutnya, Anda perlu menyalin file konfigurasi klien OpenVPN dari server OpenVPN ke mesin klien.

Pada mesin klien, jalankan perintah berikut untuk mengunduh semua file konfigurasi klien:

scp -r [email protected]:/etc/openvpn/client .

Setelah semua file disalin, ubah direktori ke klien dan jalankan perintah berikut untuk terhubung ke server OpenVPN:

cd client
openvpn --config client.ovpn

Setelah Anda terhubung ke server OpenVPN, Anda akan melihat output berikut:

Jun 29 11:48:27 2020 TCP/UDP: Preserving recently used remote address: 
[AF_INET]69.87.218.145:1194
Jun 29 11:48:27 2020 Socket Buffers: R=[212992->212992] S=[212992->212992]
Jun 29 11:48:27 2020 UDP link local: (not bound)
Jun 29 11:48:27 2020 UDP link remote: [AF_INET]69.87.218.145:1194
Jun 29 11:48:27 2020 TLS: Initial packet from [AF_INET]69.87.218.145:1194, sid=6d27e1cb 
524bd8cd
Jun 29 11:48:27 2020 VERIFY OK: depth=1, CN=Easy-RSA CA
Jun 29 11:48:27 2020 VERIFY OK: depth=0, CN=atlantic-server
Jun 29 11:48:27 2020 Control Channel: TLSv1.3, cipher TLSv1.3 TLS_AES_256_GCM_SHA384, 
2048 bit RSA
Jun 29 11:48:27 2020 [atlantic-server] Peer Connection Initiated with 
[AF_INET]69.87.218.145:1194
Jun 29 11:48:27 2020 SENT CONTROL [atlantic-server]: 'PUSH_REQUEST' (status=1)
Jun 29 11:48:27 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'
Jun 29 11:48:27 2020 OPTIONS IMPORT: timers and/or timeouts modified
Jun 29 11:48:27 2020 OPTIONS IMPORT: --ifconfig/up options modified
Jun 29 11:48:27 2020 OPTIONS IMPORT: route options modified

Setelah koneksi berhasil, OpenVPN akan menetapkan alamat IP ke sistem Anda. Anda dapat memeriksanya dengan perintah berikut:

ip a show tun0

Keluaran:

4: tun0: <POINTOPOINT,MULTICAST,NOARP,UP,LOWER_UP> mtu 1500 qdisc fq_codel 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::7226:57b1:f101:313b/64 scope link stable-privacy
valid_lft forever preferred_lft forever

Anda juga dapat memeriksa log server OpenVPN untuk memverifikasi status koneksi menggunakan perintah berikut:

tail -f /var/log/openvpn.log

Kesimpulan

Selamat! Anda telah berhasil mengatur server VPN dengan OpenVPN pada Ubuntu 20.04 VPS. Anda sekarang dapat mengakses internet dengan aman dan melindungi identitas, lokasi, dan lalu lintas Anda. Kunjungi dokumentasi resmi OpenVPN untuk informasi lebih lanjut.


Linux
  1. Cara menginstal dan mengkonfigurasi Server OpenVPN di Debian 10

  2. Cara Menginstal dan Mengkonfigurasi Server DHCP di Ubuntu 20.04

  3. Cara Menginstal Dan Mengkonfigurasi Server Redis Di Ubuntu

  1. Cara Memasang dan Mengonfigurasi Server Akses OpenVPN

  2. Cara Menginstal dan Mengonfigurasi Server NFS di Ubuntu 20.04

  3. Cara Menginstal dan Mengkonfigurasi VNC di Ubuntu 20.04

  1. Cara Menginstal dan Mengkonfigurasi VNC di Ubuntu 18.04

  2. Cara Menginstal dan Mengonfigurasi Server NFS di Ubuntu 18.04

  3. Cara Menginstal dan Mengkonfigurasi DHCP di Ubuntu 18.04