Setup WireGuard VPN

WireGuard adalah protokol VPN yang bersifat open-source yang dirancang untuk memberikan koneksi yang lebih aman, cepat, dan sederhana dibandingkan protokol VPN tradisional seperti OpenVPN atau IPSec. Dikembangkan oleh Jason A. Donenfeld dan pertama kali dirilis pada 2016, WireGuard kini menjadi bagian dari kernel Linux, menjadikannya populer berkat efisiensi, kecepatan, dan keandalannya.

Dalam panduan ini, kita akan menginstall dan mengonfigurasi WireGuard berdasarkan detail workstation berikut.

Workstation A

  • eth0 : 192.168.160.131/24
  • wg0 : 10.0.0.1/24

Workstation B

  • eth0 : 192.168.160.129/24
  • wg0 : 10.0.0.2/24

Install WireGuard

Install WireGuard sesuai distribusi OS yang digunakan.

Linux RPM

dnf -y install epel-release elrepo-release
dnf -y install wireguard-tools kmod-wireguard

Linux DEB

apt -y install wireguard

Using WireGuard

Enable modul kernel WireGuard.

modprobe wireguard

wg

Generate private key pada kedua Workstation.

wg genkey > privatekey

Selanjutnya buat interface wg0 pada kedua Workstation.

Workstation A

ip link add wg0 type wireguard
ip addr add 10.0.0.1/24 dev wg0 
wg set wg0 private-key ./private
ip link set wg0 up

Workstation B

ip link add wg0 type wireguard
ip addr add 10.0.0.2/24 dev wg0
wg set wg0 private-key ./private
ip link set wg0 up

Kemudian cek public key pada kedua Workstation untuk digunakan sebagai peer.

Workstation A

# wg show
interface: wg0
  public key: /FDHZRntW7H3WNOmzLWWo0d5UGicyt+BOvl0Khynsik=
  private key: (hidden)
  listening port: 45960

Workstation B

# wg show
interface: wg0
  public key: JSi9B7Vic3nfPuPVLyerYEqR13yFIY5sUk2FNaaL42Y=
  private key: (hidden)
  listening port: 33497

Lalu hubungkan Workstation A dan Workstation B dengan cara berikut.

Workstation A

wg set wg0 peer JSi9B7Vic3nfPuPVLyerYEqR13yFIY5sUk2FNaaL42Y= \
 endpoint 192.168.160.129:33497 \
 persistent-keepalive 25 \
 allowed-ips 10.0.0.2/32

Workstation B

wg set wg0 peer /FDHZRntW7H3WNOmzLWWo0d5UGicyt+BOvl0Khynsik= \
 endpoint 192.168.160.131:45960 \
 persistent-keepalive 25 \
 allowed-ips 10.0.0.1/32

Lakukan test ping untuk memastikan kedua Workstation terhubung.

Workstation A

# ping -c4 10.0.0.2
PING 10.0.0.2 (10.0.0.2) 56(84) bytes of data.
64 bytes from 10.0.0.2: icmp_seq=1 ttl=64 time=1.12 ms
64 bytes from 10.0.0.2: icmp_seq=2 ttl=64 time=0.754 ms
64 bytes from 10.0.0.2: icmp_seq=3 ttl=64 time=0.959 ms
64 bytes from 10.0.0.2: icmp_seq=4 ttl=64 time=0.702 ms

Workstation B

# wg show
interface: wg0
  public key: JSi9B7Vic3nfPuPVLyerYEqR13yFIY5sUk2FNaaL42Y=
  private key: (hidden)
  listening port: 33497

peer: /FDHZRntW7H3WNOmzLWWo0d5UGicyt+BOvl0Khynsik=
  endpoint: 192.168.160.131:45960
  allowed ips: 10.0.0.1/32
  latest handshake: 30 seconds ago
  transfer: 4.48 KiB received, 3.05 KiB sent
  persistent keepalive: every 25 seconds

wg-quick

Anda juga dapat menggunakan wg-quick untuk memudahkan proses setup interface WireGuard.

Generate private key dan public key pada kedua Workstation.

wg genkey | tee privatekey | wg pubkey > publickey

Kemudian buat konfigurasi WireGuard pada kedua Workstation seperti contoh berikut.

Workstation A

nano /etc/wireguard/server.conf
[Interface]
Address = 10.0.0.1
ListenPort = 45960
PrivateKey = YKAPzFfqrUBkiw18kGCOuJTkuCV8ldXB03gLJkCNH1w=

[Peer]
PublicKey = JSi9B7Vic3nfPuPVLyerYEqR13yFIY5sUk2FNaaL42Y=
AllowedIPs = 10.0.0.2/32
Endpoint = 192.168.160.129:33497

Workstation B

nano /etc/wireguard/client.conf
[Interface]
Address = 10.0.0.2
ListenPort = 33497
PrivateKey = 6LQjtoSje8BjYXISKzE+/YwhTw8VRO0bEHYOMKCnHEo=

[Peer]
PublicKey = /FDHZRntW7H3WNOmzLWWo0d5UGicyt+BOvl0Khynsik=
AllowedIPs = 10.0.0.1/32
Endpoint = 192.168.160.131:45960
PersistentKeepalive = 25

Aktifkan WireGuard pada kedua Workstation.

Workstation A

wg-quick up server

Workstation B

wg-quick up client

VPN

Berikut adalah konfigurasi WireGuard sebagai VPN.

Aktifkan IP Forwarding pada Workstation A.

echo "net.ipv4.ip_forward=1" | tee -a /etc/sysctl.conf
sudo sysctl -p

Kemudian konfigurasi agar Workstation A menjadi server VPN.

[Interface]
Address = 10.0.0.1/24
ListenPort = 51820
PrivateKey = AEi/B7B0ulkppXRN4WWnyrZIg3x8o+fOQquiL9Gt43o=
PostUp = iptables -A FORWARD -i %i -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
PostDown = iptables -D FORWARD -i %i -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE

[Peer]
PublicKey = xtLLY1oDdDdVBGxnKafexOO2mx63WySv1qUf9+oVUjw=
AllowedIPs = 10.0.0.2/32

Selanjutnya konfigurasi klien VPN pada Workstation B seperti berikut.

[Interface]
Address = 10.0.0.2/32
PrivateKey = SFFCKS3L4iIrvYLaQOU1RQo+P0uw0VojYILKxFurdVg=
DNS = 1.1.1.1

[Peer]
PublicKey = 48XNnKVRJjXk892Ku7DO7CzU6B2OehgRnd/0ln8FryM=
AllowedIPs = 0.0.0.0/0
Endpoint = 192.168.160.129:51820
PersistentKeepalive = 25

Aktifkan WireGuard pada kedua Workstation.

Workstation A

wg-quick up server

Workstation B

wg-quick up client