Artikel ini menjelaskan bagaimana cara setup private network mesh topologi dengan software WireGuard
Studi kasus:
Saya punya punya 4 Linux VPS yang berbeda, saya ingin membuat private network dengan software wireguard. Jadi nanti VPS 1-4 mempunyai IP Private: 192.168.1.1 – 192.168.1.4
Persyaratan:
- Sistem operasi yang digunakan pada setiap VPS ialah Ubuntu
- Root / akses sudo
- Open port UDP 51820 pada setiap VPS
Step 1: Install Wireguard
Install software wireguard pada setiap VPS dengan cara
sudo apt update
sudo apt install wireguard -y
Lalu pada setiap VPS, dibuat private key dan public key nya dengan cara command berikut:
wg genkey | tee privatekey | wg pubkey > publickey
Masukkan private key dan public key sesuai dengan tabel berikut:
VPS | IP Public | Internal IP | Private Key | Public Key |
---|---|---|---|---|
VPS1 | <VPS1_PUBLIC_IP> | 192.168.1.1 | <VPS1_private_key> |
|
VPS2 | <VPS2_PUBLIC_IP> | 192.168.1.2 |
|
|
VPS3 | <VPS3_PUBLIC_IP> | 192.168.1.3 |
|
|
VPS4 | <VPS4_PUBLIC_IP> | 192.168.1.4 |
|
|
Untuk melihat isi dari file private.key dan public.key, bisa menggunakan command cat
cat private.key
cat public.key
Untuk memulai mengkonfigurasikan wireguard, buatlah file config wg0.conf didalam direktori /etc/wireguard
nano /etc/wireguard/wg0.conf
Isian wg0.conf ialah sebagai berikut:
[Interface]
PrivateKey = <VPS1_PRIVATE_KEY>
Address = 192.168.1.1/24
ListenPort = 51820
[Peer]
PublicKey = <VPS2_PUBLIC_KEY>
Endpoint = <VPS2_PUBLIC_IP>:51820
AllowedIPs = 192.168.1.2/32
PersistentKeepalive = 25
[Peer]
PublicKey = <VPS3_PUBLIC_KEY>
Endpoint = <VPS3_PUBLIC_IP>:51820
AllowedIPs = 192.168.1.3/32
PersistentKeepalive = 25
[Peer]
PublicKey = <VPS4_PUBLIC_KEY>
Endpoint = <VPS4_PUBLIC_IP>:51820
AllowedIPs = 192.168.1.4/32
PersistentKeepalive = 25
Perlu dicatat, untuk konfig wg0.conf pada VPS2, VPS3 dan VPS4. Isian [Interface] berbeda-beda.
Contoh pada isian wg0.conf didalam VPS2, [Interface] ialah milik VPS2. Sisanya [Peer] bisa dicopy paste karena isian PUBLIC_KEY dan PUBLIC_IP sama.
[Interface]
PrivateKey = <VPS2_PRIVATE_KEY>
Address = 192.168.1.2/24
ListenPort = 51820
[Peer]
PublicKey = <VPS1_PUBLIC_KEY>
Endpoint = <VPS1_PUBLIC_IP>:51820
AllowedIPs = 192.168.1.2/32
PersistentKeepalive = 25
[Peer]
PublicKey = <VPS3_PUBLIC_KEY>
Endpoint = <VPS3_PUBLIC_IP>:51820
AllowedIPs = 192.168.1.3/32
PersistentKeepalive = 25
[Peer]
PublicKey = <VPS4_PUBLIC_KEY>
Endpoint = <VPS4_PUBLIC_IP>:51820
AllowedIPs = 192.168.1.4/32
PersistentKeepalive = 25
Buat konfig wg0.conf juga pada VPS3 dan VPS4, lalu setelah itu aktifkan IP Forwarding pada masing-masing VPS. Commandnya ialah sebagai berikut:
echo "net.ipv4.ip_forward=1" | sudo tee -a /etc/sysctl.conf
sudo sysctl -p
Setelah itu config wireguard wg0 bisa diaktifkan dan dinyalakan dengan command sebagai berikut:
sudo systemctl enable wg-quick@wg0
sudo systemctl start wg-quick@wg0
Bisa dicoba juga ping antar VPS agar memastikan private network berjalan
ping 192.168.1.1
ping 192.168.1.2
ping 192.168.1.3
ping 192.168.1.4
Selamat, pivate Network dengan Wireguard telah selesai dibuat!