Chuyển tới nội dung chính

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:

ModeMô tảDùng cho
TransportChỉ mã hoá payload, giữ nguyên IP headerHost-to-host
TunnelMã hoá toàn bộ packet, thêm IP header mớiSite-to-site (phổ biến)

IPSec Protocols:

ProtocolPortChức năng
IKE/ISAKMPUDP 500, 4500Key exchange, negotiation
AHProtocol 51Authentication (không mã hoá)
ESPProtocol 50Encryption + 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

ProtocolSecuritySpeedDùng cho
PPTP❌ Yếu (bị crack)NhanhKhông dùng nữa
L2TP/IPSec✅ TốtChậm hơnWindows built-in, legacy
OpenVPN✅ TốtTốtPhổ biến, cross-platform
WireGuard✅ Xuất sắcNhanh nhấtModern, recommended
IKEv2/IPSec✅ TốtNhanhMobile, iOS native

Split Tunnel vs Full Tunnel

Full TunnelSplit Tunnel
TrafficTất cả qua VPNChỉ traffic nội bộ qua VPN
Bảo mậtCao hơn (kiểm soát tất cả)Thấp hơn
Hiệu năngChậm hơn (tất cả qua server)Nhanh hơn
Bandwidth serverTốn nhiềuTiết kiệm
Dùng choSecurity-sensitiveProductivity, 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