WireGuard là VPN protocol hiện đại, nhanh hơn OpenVPN 3-4 lần, code chỉ 4,000 dòng (so với 600,000 dòng của OpenVPN). Setup trong 5 phút, performance cao, security tốt. Bài viết này hướng dẫn setup WireGuard VPN server trên Linux và kết nối từ Windows, macOS, iOS, Android.

WireGuard là gì?

WireGuard là VPN protocol mã nguồn mở, được tích hợp sẵn vào Linux kernel từ version 5.6. So với OpenVPN và IPSec:

  • Nhanh hơn: 3-4x throughput so với OpenVPN.
  • Bảo mật hơn: Mã hóa hiện đại (Curve25519, ChaCha20, Poly1305).
  • Đơn giản hơn: Cấu hình chỉ vài dòng, không cần certificates.
  • Nhẹ hơn: Code chỉ 4,000 dòng, dễ audit.

Kiến trúc VPN

Trước khi setup, hiểu mô hình:

  • VPN Server: Server Linux chạy WireGuard, có IP public.
  • VPN Client: Laptop, điện thoại kết nối vào server.
  • Tunnel: Khi kết nối, client có IP riêng trong mạng VPN (10.0.0.x).
  • Use case: Truy cập server từ xa, bypass firewall, bảo mật public WiFi.

1. Cài đặt WireGuard Server

Trên Ubuntu/Debian

apt update && apt install wireguard -y

Trên CentOS/RHEL

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

Trên Proxmox LXC Container

Nếu chạy trong LXC, cần enable nesting và tính năng TUN:

# Trên Proxmox host, sửa LXC config
nano /etc/pve/lxc/XXX.conf

# Thêm vào cuối file
lxc.apparmor.profile: unconfined
lxc.cgroup2.devices.allow: c 10:200 rwm
lxc.mount.auto: "proc:rw sys:rw"

Sau đó bên trong LXC:

apt update && apt install wireguard -y

2. Tạo Keys

WireGuard dùng cryptographic keys để xác thực. Mỗi peer (server + client) cần 1 key pair.

# Tạo thư mục config
mkdir -p /etc/wireguard
cd /etc/wireguard

# Tạo server keys
wg genkey | tee server_private.key | wg pubkey > server_public.key

# Tạo client 1 keys
wg genkey | tee client1_private.key | wg pubkey > client1_public.key

# Set permissions
chmod 600 *.key

Xem các key đã tạo:

# Server private key (GIỮ BÍ MẬT)
cat server_private.key

# Server public key (chia sẻ cho client)
cat server_public.key

# Client private key (chia sẻ cho client)
cat client1_private.key

# Client public key (đặt trên server)
cat client1_public.key

3. Cấu hình Server

Tạo file cấu hình server /etc/wireguard/wg0.conf:

[Interface]
# Server private key
PrivateKey = 

# VPN subnet
Address = 10.0.0.1/24

# Port lắng nghe
ListenPort = 51820

# IP forwarding và NAT
PostUp = iptables -A FORWARD -i wg0 -j ACCEPT
PostUp = iptables -A FORWARD -o wg0 -j ACCEPT
PostUp = iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

PostDown = iptables -D FORWARD -i wg0 -j ACCEPT
PostDown = iptables -D FORWARD -o wg0 -j ACCEPT
PostDown = iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE

# Client configuration
[Peer]
# Client 1 public key
PublicKey = 

# Allowed IPs = IP mà client được phép truy cập
AllowedIPs = 10.0.0.2/32

Lưu ý: Thay eth0 bằng tên interface mạng của bạn. Kiểm tra bằng ip a.

Bật IP Forwarding

# Tạm thời
echo 1 > /proc/sys/net/ipv4/ip_forward

# Vĩnh viễn
echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
sysctl -p

4. Cấu hình Client

Windows

Tải WireGuard client cho Windows.

Tạo file config mới, paste nội dung:

[Interface]
# Client 1 private key
PrivateKey = 

# IP của client trong VPN
Address = 10.0.0.2/24

# DNS server
DNS = 1.1.1.1, 8.8.8.8

[Peer]
# Server public key
PublicKey = 

# Server endpoint
Endpoint = :51820

# Keep connection alive
PersistentKeepalive = 25

# Allowed IPs = traffic nào đi qua VPN
# 0.0.0.0/0 = tất cả traffic (full tunnel)
# 10.0.0.0/24 = chỉ VPN subnet
AllowedIPs = 0.0.0.0/0

Full tunnel vs Split tunnel:

  • AllowedIPs = 0.0.0.0/0 → Tất cả traffic đi qua VPN (bảo mật hơn, bypass censorship).
  • AllowedIPs = 10.0.0.0/24 → Chỉ traffic đến VPN subnet, traffic khác đi trực tiếp.

macOS

Tải WireGuard từ App Store.

Import file config tương tự Windows.

iOS / Android

Tải app:

Quét QR code từ server để import nhanh:

# Tạo QR code cho mobile
apt install qrencode -y

# Tạo config file riêng cho mobile
cat > /etc/wireguard/client1.conf << 'EOF'
[Interface]
PrivateKey = 
Address = 10.0.0.2/24
DNS = 1.1.1.1

[Peer]
PublicKey = 
Endpoint = :51820
AllowedIPs = 0.0.0.0/0
PersistentKeepalive = 25
EOF

# Generate QR code
qrencode -t ansiutf8 < /etc/wireguard/client1.conf

Linux Command Line

# Cài đặt
apt install wireguard -y

# Copy config
cp client1.conf /etc/wireguard/wg0.conf

# Kết nối
wg-quick up wg0

# Ngắt kết nối
wg-quick down wg0

5. Khởi động và Enable Service

# Enable auto-start
systemctl enable wg-quick@wg0

# Start service
systemctl start wg-quick@wg0

# Check status
systemctl status wg-quick@wg0

# Xem interface
wg show

6. Thêm nhiều Clients

Để thêm client mới, tạo keys mới và thêm vào server config:

# Tạo keys cho client 2
cd /etc/wireguard
wg genkey | tee client2_private.key | wg pubkey > client2_public.key

# Thêm peer vào server config
wg set wg0 peer $(cat client2_public.key) allowed-ips 10.0.0.3/32

# Hoặc edit file config trực tiếp
nano /etc/wireguard/wg0.conf
# Thêm vào cuối file wg0.conf
[Peer]
PublicKey = 
AllowedIPs = 10.0.0.3/32

# Restart để áp dụng
systemctl restart wg-quick@wg0

7. Firewall (UFW)

# Mở UDP port cho WireGuard
ufw allow 51820/udp

# Enable forwarding
nano /etc/default/ufw
# Đổi: DEFAULT_FORWARD_POLICY="ACCEPT"

# Restart UFW
ufw reload

# Check rules
ufw status

8. Troubleshooting

Không kết nối được

  • Check server port: ss -ulnp | grep 51820
  • Check firewall: ufw status
  • Check logs: journalctl -u wg-quick@wg0 -f
  • Verify keys đúng: wg show

Kết nối được nhưng không có internet

  • Check IP forwarding: cat /proc/sys/net/ipv4/ip_forward (phải = 1)
  • Check NAT: iptables -t nat -L -n
  • Verify interface name trong PostUp/PostDown đúng.

DNS không hoạt động

  • Kiểm tra DNS set trong client config.
  • Thử DNS khác: 1.1.1.1, 8.8.8.8, hoặc Google DNS.

9. Performance

WireGuard nhanh hơn OpenVPN đáng kể. Benchmark:

ProtocolThroughputLatency
WireGuard~500 Mbps~2ms
OpenVPN~150 Mbps~5ms
IPSec~400 Mbps~3ms

10. Security Best Practices

  • [ ] Server: Private key phải GIỮ BÍ MẬT trên server.
  • [ ] Client: Private key chỉ có trên device của user.
  • [ ] Port: Đổi port không dùng 51820 nếu muốn.
  • [ ] PersistentKeepalive: Set 25 giây nếu behind NAT.
  • [ ] Firewall: Chỉ mở UDP port cần thiết.
  • [ ] Update: WireGuard có security updates, update thường xuyên.

Kết Luận

WireGuard là VPN solution hiện đại, nhanh, bảo mật và dễ setup. So với OpenVPN, nó đơn giản hơn nhiều và performance tốt hơn.

  • Setup trong 5-10 phút.
  • Hỗ trợ Windows, macOS, Linux, iOS, Android.
  • Perfect cho remote work, truy cập server từ xa.

Kết hợp với SSH Hardening để bảo mật server từ xa toàn diện.

Chào các bạn mình là Quốc Hùng , mình sinh ra thuộc cung song tử ,song tử luôn khẳng định chính mình ,luôn luôn phấn đấu vượt lên phía trước ,mình sinh ra và lớn lên tại vùng đất võ cổ truyền ,đam mê của mình là coder ,ngày đi học tối về viết blog ...