Keamanan
Cara Setup WireGuard VPN di VPS: Aman, Cepat, dan Ringan
Baca juga
📚 Baca juga
WireGuard adalah protokol VPN modern yang dirancang untuk kecepatan, kemudahan konfigurasi, dan keamanan tingkat tinggi. Dibandingkan OpenVPN yang sudah berusia puluhan tahun, WireGuard menawarkan performa yang lebih baik dengan kode yang jauh lebih sedikit — hanya sekitar 4.000 baris dibanding OpenVPN yang lebih dari 100.000 baris.
## Mengapa WireGuard?
**Lebih Cepat dari OpenVPN.** WireGuard menggunakan ChaCha20 untuk enkripsi, yang lebih cepat di hardware tanpa AES-NI. Di testing, WireGuard seringkali 2-3x lebih cepat dari OpenVPN dalam hal throughput.
**Konfigurasi Sederhana.** Seluruh konfigurasi WireGuard muat dalam satu file dengan format yang mudah dibaca. Tidak perlu config file ratusan baris seperti OpenVPN.
**Kernel-level Performance.** WireGuard berjalan sebagai kernel module (bukan userspace seperti OpenVPN), sehingga overhead-nya sangat minimal.
**Mobile-Friendly.** WireGuard available di iOS, Android, Windows, macOS, dan Linux. Handshake cepat sehingga switching dari WiFi ke cellular nyaris tanpa delay.
## Arsitektur
Untuk setup yang akan kita bangun:
```
[Client (Laptop/HP)] <--加密--> [VPS (WireGuard Server)] <--路由--> [Internet]
```
VPS berfungsi sebagai VPN server. Semua traffic dari client dienkripsi dan melewati VPS. Internet melihat IP VPS, bukan IP client asli.
## Install WireGuard di VPS
### Ubuntu/Debian
```bash
apt update && apt install -y wireguard
```
### CentOS/RHEL
```bash
yum install -y epel-release elrepo-release
yum install -y kmod-wireguard wireguard-tools
```
### Aktifkan IP Forwarding
```bash
echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
sysctl -p
```
## Konfigurasi Server
### 1. Generate Keys
```bash
wg genkey | tee server_private.key | wg pubkey > server_public.key
chmod 600 server_private.key
```
### 2. Buat Config Server
```bash
cat > /etc/wireguard/wg0.conf << 'EOF'
[Interface]
PrivateKey =
Address = 10.0.0.1/24
ListenPort = 51820
PostUp = iptables -A FORWARD -i wg0 -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
PostDown = iptables -D FORWARD -i wg0 -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE
# Client 1
[PublicKey] =
AllowedIPs = 10.0.0.2/32
EOF
```
Ganti `` dan `` dengan key yang sudah digenerate. Ganti `eth0` dengan interface network VPS kamu (cek dengan `ip link`).
### 3. Start WireGuard
```bash
wg-quick up wg0
systemctl enable wg-quick@wg0
```
### 4. Generate Keys Client
```bash
wg genkey | tee client1_private.key | wg pubkey > client1_public.key
```
## Konfigurasi Client
### Linux/macOS
```bash
cat > ~/wireguard-client.conf << 'EOF'
[Interface]
PrivateKey =
Address = 10.0.0.2/24
DNS = 1.1.1.1, 8.8.8.8
[Peer]
PublicKey =
Endpoint = :51820
AllowedIPs = 0.0.0.0/0
PersistentKeepalive = 25
EOF
```
**Critical:** `AllowedIPs = 0.0.0.0/0` artinya SEMUA traffic melewati VPN (full tunnel). Jika hanya ingin akses ke VPS, gunakan `AllowedIPs = 10.0.0.1/32, /32`.
### Windows & Mobile
Download WireGuard client dari wireguard.com/install. Import config file yang sudah dibuat, atau scan QR code untuk mobile:
```bash
# Generate QR code untuk mobile
apt install -y qrencode
qrencode -t ansiutf8 < ~/wireguard-client.conf
```
## Firewall Rules
### iptables
```bash
iptables -A INPUT -p udp --dport 51820 -j ACCEPT
iptables -A INPUT -i wg0 -j ACCEPT
iptables -A FORWARD -i wg0 -o eth0 -j ACCEPT
iptables -A FORWARD -i eth0 -o wg0 -m state --state RELATED,ESTABLISHED -j ACCEPT
```
### UFW
```bash
ufw allow 51820/udp
ufw allow from 10.0.0.0/24
```
## Multi-Client Setup
Untuk menambah client baru, tambahkan section [Peer] baru di config server:
```
# Client 2
[PublicKey] =
AllowedIPs = 10.0.0.3/32
# Client 3
[PublicKey] =
AllowedIPs = 10.0.0.4/32
```
Setiap client mendapat IP unik di subnet `10.0.0.0/24`.
## Tips Performa
**MTU Optimization.** WireGuard default MTU adalah 1420. Jika ada packet loss, coba turunkan ke 1380:
```
[Interface]
MTU = 1380
```
**Kernel Module.** Pastikan WireGuard berjalan sebagai kernel module, bukan userspace:
```bash
lsmod | grep wireguard
```
**NAT Configuration.** Jika VPS menggunakan NAT (cloud provider), pastikan UDP port 51820 terbuka di security group/firewall provider.
**Keepalive.** `PersistentKeepalive = 25` penting untuk menjaga connection tetap hidup di belakang NAT. Tanpa ini, connection akan putus setelah beberapa menit.
## Troubleshooting
**Tidak bisa connect?**
1. Cek firewall: `iptables -L -n | grep 51820`
2. Cek port: `ss -ulnp | grep 51820`
3. Cek routing: `wg show`
4. Cek logs: `journalctl -u wg-quick@wg0`
**Connected tapi tidak bisa akses internet?**
1. Cek IP forwarding: `cat /proc/sys/net/ipv4/ip_forward` harus `1`
2. Cek NAT rules: `iptables -t nat -L -n`
3. Cek DNS client config
**DNS leak?**
Pastikan DNS di config client mengarah ke VPN atau DNS yang aman (1.1.1.1, 9.9.9.9).
WireGuard adalah masa depan VPN — cepat, aman, dan sederhana. Dengan setup yang benar, kamu punya encrypted tunnel ke VPS yang bisa kamu pakai untuk browsing aman dari WiFi publik, akses remote server, atau bahkan bypass geo-restriction.