VPN — Virtual Private Network
VPN tạo ra một đường hầm mã hoá (encrypted tunnel) qua mạng công cộng, cho phép kết nối an toàn như thể đang trong cùng mạng nội bộ.
Tại sao cần VPN?
Các loại VPN
Site-to-Site VPN — Kết nối hai văn phòng
IPSec — Giao thức Site-to-Site phổ biến nhất
IPSec hoạt động ở Layer 3 (Network), mã hoá packet IP:
IPSec Modes:
| Mode | Mô tả | Dùng cho |
|---|---|---|
| Transport | Chỉ mã hoá payload, giữ nguyên IP header | Host-to-host |
| Tunnel | Mã hoá toàn bộ packet, thêm IP header mới | Site-to-site (phổ biến) |
IPSec Protocols:
| Protocol | Port | Chức năng |
|---|---|---|
| IKE/ISAKMP | UDP 500, 4500 | Key exchange, negotiation |
| AH | Protocol 51 | Authentication (không mã hoá) |
| ESP | Protocol 50 | Encryption + Authentication |
# Xem IPSec SA trên Linux (strongSwan)
sudo ipsec status
sudo ipsec statusall
# Restart tunnel
sudo ipsec restart
sudo ipsec down tunnel-name
sudo ipsec up tunnel-name
Client-to-Site VPN — Remote Access
WireGuard — Thế hệ mới (Khuyến nghị)
WireGuard là VPN protocol hiện đại nhất — nhanh, nhẹ, bảo mật, cấu hình đơn giản.
Cài đặt WireGuard Server (Ubuntu):
# Cài đặt
sudo apt install wireguard
# Tạo key pair cho server
wg genkey | sudo tee /etc/wireguard/private.key
sudo cat /etc/wireguard/private.key | wg pubkey | sudo tee /etc/wireguard/public.key
# Cấu hình server
sudo nano /etc/wireguard/wg0.conf
# /etc/wireguard/wg0.conf — Server
[Interface]
Address = 10.8.0.1/24
ListenPort = 51820
PrivateKey = <SERVER_PRIVATE_KEY>
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
# Mỗi client là một [Peer]
[Peer]
PublicKey = <CLIENT1_PUBLIC_KEY>
AllowedIPs = 10.8.0.2/32 # IP trong tunnel cho client này
[Peer]
PublicKey = <CLIENT2_PUBLIC_KEY>
AllowedIPs = 10.8.0.3/32
# Client config — lưu vào ~/.config/wireguard/wg0.conf hoặc app mobile
[Interface]
Address = 10.8.0.2/24
PrivateKey = <CLIENT_PRIVATE_KEY>
DNS = 8.8.8.8
[Peer]
PublicKey = <SERVER_PUBLIC_KEY>
Endpoint = server_public_ip:51820
AllowedIPs = 0.0.0.0/0 # full tunnel — tất cả traffic qua VPN
# AllowedIPs = 192.168.0.0/16 # split tunnel — chỉ traffic nội bộ qua VPN
PersistentKeepalive = 25
# Bật WireGuard
sudo systemctl enable --now wg-quick@wg0
sudo wg show # xem status và connected peers
# Client
sudo wg-quick up wg0
sudo wg-quick down wg0
OpenVPN — Phổ biến nhất hiện tại
# Cài OpenVPN + EasyRSA (Ubuntu)
sudo apt install openvpn easy-rsa
# Khởi tạo PKI
cd /etc/openvpn/easy-rsa
./easyrsa init-pki
./easyrsa build-ca # tạo CA certificate
./easyrsa gen-req server nopass # tạo server key
./easyrsa sign-req server server
./easyrsa gen-dh # Diffie-Hellman params
openvpn --genkey secret /etc/openvpn/ta.key # TLS-auth key
# Tạo client certificate
./easyrsa gen-req client1 nopass
./easyrsa sign-req client client1
# Start server
sudo systemctl enable --now openvpn@server
L2TP/IPSec và PPTP
| Protocol | Security | Speed | Dùng cho |
|---|---|---|---|
| PPTP | ❌ Yếu (bị crack) | Nhanh | Không dùng nữa |
| L2TP/IPSec | ✅ Tốt | Chậm hơn | Windows built-in, legacy |
| OpenVPN | ✅ Tốt | Tốt | Phổ biến, cross-platform |
| WireGuard | ✅ Xuất sắc | Nhanh nhất | Modern, recommended |
| IKEv2/IPSec | ✅ Tốt | Nhanh | Mobile, iOS native |
Split Tunnel vs Full Tunnel
| Full Tunnel | Split Tunnel | |
|---|---|---|
| Traffic | Tất cả qua VPN | Chỉ traffic nội bộ qua VPN |
| Bảo mật | Cao hơn (kiểm soát tất cả) | Thấp hơn |
| Hiệu năng | Chậm hơn (tất cả qua server) | Nhanh hơn |
| Bandwidth server | Tốn nhiều | Tiết kiệm |
| Dùng cho | Security-sensitive | Productivity, WFH thông thường |
VPN trên Cloud (AWS/Azure/GCP)
AWS Site-to-Site VPN
# AWS CLI — tạo VPN connection
aws ec2 create-vpn-connection \
--type ipsec.1 \
--customer-gateway-id cgw-xxxxx \
--vpn-gateway-id vgw-xxxxx
AWS Client VPN
Cho phép users remote access vào VPC bằng OpenVPN client:
# Download client config từ AWS Console
# Cài AWS VPN Client hoặc OpenVPN client
# Import .ovpn config file → Connect