Cara menginstall OpenVPN pada Linux Ubuntu 16.04 (LTS)

Pada postingan kali ini saya akan coba memberikan tutorial membuat vpn sendiri pada linux server ubuntu. Dan yang perlu diperisapkan teman-teman agar dapat membut sebuah VPN server sendiri adalah sebuah server baik itu Colocation Server ataupun VPS (Virtual Private Server).

Jika telah server sudah tersedia, pastikan server terinstall dengan OS Linux Ubuntu 16.04 (LTS) karena pada saat membuat post ini saya menggunakan ubuntu versi 16.04.

Install OpenVPN Server

# apt update
# apt upgrade -y 
Reading package lists... Done
Building dependency tree
Reading state information... Done
Calculating upgrade... Done
The following NEW packages will be installed:
  linux-headers-4.4.0-189 linux-headers-4.4.0-189-generic linux-image-4.4.0-189-generic linux-modules-4.4.0-189-generic motd-news-config
The following packages will be upgraded:
  base-files bind9-host curl dnsutils grub-common grub-pc grub-pc-bin grub2-common libbind9-140 libcurl3-gnutls libdns-export162 libdns162 libisc-export160 libisc160 libisccc140 libisccfg140
  liblwres141 libx11-6 libx11-data linux-headers-generic linux-headers-virtual linux-image-virtual linux-virtual ubuntu-minimal ubuntu-server ubuntu-standard
26 upgraded, 5 newly installed, 0 to remove and 0 not upgraded.
Need to get 36.4 MB of archives.
After this operation, 147 MB of additional disk space will be used.
Do you want to continue? [Y/n]Y dan enter

Install OpenVPN dan easy-rsa

# apt-get install openvpn easy-rsa -y
# make-cadir ~/mc/openvpn-ca

Masuk keadalam direktori CA yang baru saja dibuat

# cd ~/mc/openvpn-ca

Konfigurasi CA

Silahkan hapus semua isi file vars pada folder CA dan masukan sesuai yang ada dibawah ini:

export EASY_RSA="`pwd`"
export OPENSSL="openssl"
export PKCS11TOOL="pkcs11-tool"
export GREP="grep"
export KEY_CONFIG=`$EASY_RSA/whichopensslcnf $EASY_RSA`
export KEY_DIR="$EASY_RSA/keys"
echo NOTE: If you run ./clean-all, I will be doing a rm -rf on $KEY_DIR
export PKCS11_MODULE_PATH="dummy"
export PKCS11_PIN="dummy"
export KEY_SIZE=2048
export CA_EXPIRE=3650
export KEY_EXPIRE=3650
export KEY_COUNTRY="US"
export KEY_PROVINCE="CA"
export KEY_CITY="SanFrancisco"
export KEY_ORG="Fort-Funston"
export KEY_EMAIL="me@myhost.mydomain"
export KEY_OU="MyOrganizationalUnit"
export KEY_NAME="servervpn"

Membuat Certificate Authority

# source vars
NOTE: If you run ./clean-all, I will be doing a rm -rf on /root/mc/openvpn-ca/keys # ./clean-all # ./build-ca

Pada saat membuat CA akan ada beberapa pertanyaan, namun karena sudah kita masuk tadi kedalam vars jadi kita tinggal enter saja sampai selesai

Membuat Certificate, Key dan Encryption Files

# ./build-key-server servervpn
Jika ada pertanyaan silahkanmemasukan y untuk menyetujuinya

Generate Diffie-Hellman. Silahkan tunggu karena ini memakan waktu beberapa menit

# ./build-dh

Generate HMAC

# openvpn --genkey --secret keys/ta.key

Membuat Client Certificate dan Key

# source vars
# ./build-key client
# source vars
# ./build-key-pass client

Setujui setiap pertanyaan dengan input y dan enter sampai semua pertanyaan selesai

Konfigurasi Service OpenVPN

# cd ~/openvpn-ca/keys
# cp ca.crt server.crt server.key ta.key dh2048.pem /etc/openvpn
# gunzip -c /usr/share/doc/openvpn/examples/sample-config-files/server.conf.gz |  tee /etc/openvpn/server.conf

Masukan ini pada file /etc/openvpn/server.conf

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 "redirect-gateway autolocal def1"
#push "redirect-gateway def1"
push "dhcp-option DNS 8.8.8.8"
push "dhcp-option DNS 8.8.4.4."
;client-to-client
keepalive 10 120
tls-auth ta.key 0
cipher AES-128-CBC
auth SHA256
user nobody
group nogroup
persist-key
persist-tun
status openvpn-status.log
verb 3
explicit-exit-notify 1
Allow IP Forwarding
Edit file /etc/sysctl.conf dan cari #net.ipv4.ip_forward=1. Disini hapus tanda # sebelum net.ipv4.ip_forward kemudian simpan dan jalankan perintah berikut
# sysctl -p

Start danEnable OpenVPN Service

# systemctl start openvpn@server
# systemctl enable openvpn@server

Periksa apakah openvpn service sudah berjalan dengan baik

# service openvpn@server status
● openvpn@server.service - OpenVPN connection to server
   Loaded: loaded (/lib/systemd/system/openvpn@.service; enabled; vendor preset: enabled)
   Active: active (running) since Wed 2020-09-02 10:03:33 WIB; 1h ago
     Docs: man:openvpn(8)
           https://community.openvpn.net/openvpn/wiki/Openvpn23ManPage
           https://community.openvpn.net/openvpn/wiki/HOWTO
  Process: 28366 ExecStart=/usr/sbin/openvpn --daemon ovpn-%i --status /run/openvpn/%i.status 10 --cd /etc/openvpn --script-security 2 --config /etc/openvpn/%i.conf --writepid /run/openvpn/%i.pid (code=e
 Main PID: 28370 (openvpn)
   CGroup: /system.slice/system-openvpn.slice/openvpn@server.service
           └─28370 /usr/sbin/openvpn --daemon ovpn-server --status /run/openvpn/server.status 10 --cd /etc/openvpn --script-security 2 --config /etc/openvpn/server.conf --writepid /run/openvpn/server.pid

Konfigurasi Client Openvpn

# mkdir -p ~/mc/client-ovpn/files
# chmod 700 ~/mc/client-ovpn/files

Buat file base pada direktori openvpn  /mc/client-ovpn/base.conf yang barusan dibuat serta masukan conf script dibawah ini

client
dev tun
proto udp
resolv-retry infinite
nobind
user nobody
group nogroup
persist-key
persist-tun
remote-cert-tls server
tls-auth ta.key 1
cipher AES-128-CBC
auth SHA256
key-direction 1
verb 3
# script-security 2
# up /etc/openvpn/update-resolv-conf
# down /etc/openvpn/update-resolv-conf

Buat file untuk mempermudah membuat file ovpn ~/mc/client-openvpn/buat.sh

#!/bin/bash
# First argument: Client identifier
KEY_DIR=~/openvpn-ca/keys
OUTPUT_DIR=~/client-configs/files
BASE_CONFIG=~/client-configs/base.conf
cat ${BASE_CONFIG} \
    <(echo -e '') \
    ${KEY_DIR}/ca.crt \
    <(echo -e '
\n') \
    ${KEY_DIR}/${1}.crt \
    <(echo -e '
\n') \
    ${KEY_DIR}/${1}.key \
    <(echo -e '
\n') \
    ${KEY_DIR}/ta.key \
    <(echo -e '
') \
    > ${OUTPUT_DIR}/${1}.ovpn

Jadikan file buat.sh menjadi executable dengan perintah berikut

# chmod +X buat.sh
atau
# chmod 700 buat.sh

Untuk membuat file .ovpn tinggal menjalankan perintah dibawah ini

# ./buat.sh client

Kemudian periksa folder

# ls -lah /root/mc/client-ovpn/files

Download file yang ada didalam folder diatas dan import ke OpenVPN connect, setelah berhasil di import silahkan konek dan jika tidak ada kesalahan maka anda telah berhasil terhubung ke VPN yang telah kita buat.