🌍 Networking

WireGuard

WireGuard

A modern VPN protocol. Simple and fast.

📖 Detailed Explanation

WireGuard is a next-generation VPN protocol developed by Jason A. Donenfeld in 2016. Unlike existing VPN solutions such as OpenVPN (~600,000 lines) and IPsec (~400,000 lines), WireGuard consists of fewer than 4,000 lines of code, which makes security audits easier and makes the likelihood of bugs significantly lower.

WireGuard's core philosophy is "simplicity is security." Without negotiating complex encryption options, it uses only proven, modern cryptographic algorithms such as ChaCha20 (symmetric encryption), Poly1305 (authentication), Curve25519 (key exchange), BLAKE2s (hashing), and HKDF (key derivation). As a result, configuration is simple and the attack surface is minimized.

In terms of performance, WireGuard operates at the kernel level and provides 3-4 times faster throughput than OpenVPN. It was officially integrated into Linux kernel 5.6 in 2020, so it can be used without installing a separate driver. The handshake completes in 1-RTT, so connection establishment time is measured in milliseconds.

Currently, WireGuard supports all major platforms: Linux, Windows, macOS, iOS, and Android. Many commercial VPN services such as Cloudflare WARP, Tailscale, and Mullvad VPN are built on WireGuard, and it is also used in Site-to-Site VPN on AWS, Azure, and GCP.

💻 Code Example

# WireGuard 설치 (Ubuntu/Debian)
sudo apt update && sudo apt install wireguard

# 키 쌍 생성 (서버 & 클라이언트 각각)
wg genkey | tee privatekey | wg pubkey > publickey
chmod 600 privatekey

# WireGuard 인터페이스 생성 및 시작
sudo wg-quick up wg0

# 연결 상태 확인
sudo wg show
# 출력 예시:
# interface: wg0
#   public key: abc123...
#   listening port: 51820
#   peer: xyz789...
#     endpoint: 203.0.113.1:51820
#     latest handshake: 5 seconds ago
#     transfer: 1.5 GiB received, 500 MiB sent

# 실시간 트래픽 모니터링
watch -n 1 sudo wg show

# WireGuard 중지
sudo wg-quick down wg0

# 부팅 시 자동 시작
sudo systemctl enable wg-quick@wg0
# /etc/wireguard/wg0.conf (서버)
[Interface]
Address = 10.0.0.1/24
ListenPort = 51820
PrivateKey = SERVER_PRIVATE_KEY
# NAT 설정 (인터넷 공유 시)
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

[Peer]
# 클라이언트 1
PublicKey = CLIENT1_PUBLIC_KEY
AllowedIPs = 10.0.0.2/32

[Peer]
# 클라이언트 2
PublicKey = CLIENT2_PUBLIC_KEY
AllowedIPs = 10.0.0.3/32

# ------------------------------------------
# /etc/wireguard/wg0.conf (클라이언트)
[Interface]
Address = 10.0.0.2/24
PrivateKey = CLIENT_PRIVATE_KEY
DNS = 1.1.1.1, 8.8.8.8

[Peer]
PublicKey = SERVER_PUBLIC_KEY
Endpoint = vpn.example.com:51820
AllowedIPs = 0.0.0.0/0  # 모든 트래픽 VPN 경유
PersistentKeepalive = 25  # NAT 뒤에서 연결 유지
# docker-compose.yml - WireGuard 서버
version: "3.8"
services:
  wireguard:
    image: linuxserver/wireguard
    container_name: wireguard
    cap_add:
      - NET_ADMIN
      - SYS_MODULE
    environment:
      - PUID=1000
      - PGID=1000
      - TZ=Asia/Seoul
      - SERVERURL=vpn.example.com
      - SERVERPORT=51820
      - PEERS=3  # 클라이언트 수
      - PEERDNS=auto
      - INTERNAL_SUBNET=10.13.13.0
    volumes:
      - ./config:/config
      - /lib/modules:/lib/modules
    ports:
      - 51820:51820/udp
    sysctls:
      - net.ipv4.conf.all.src_valid_mark=1
    restart: unless-stopped

# 실행
# docker-compose up -d
# 클라이언트 설정 확인: docker exec wireguard cat /config/peer1/peer1.conf
# QR 코드 생성: docker exec wireguard /app/show-peer 1

🗣️ Say It Like This in Practice

💬 In a VPN Solution Selection Meeting
"Compared to OpenVPN, WireGuard has fewer than 4,000 lines of code, so security audits are easier, and its kernel-level operation makes throughput more than 3 times faster. It has been included by default since Linux 5.6, so no separate module installation is needed. Its roaming feature, which keeps sessions alive even when the network switches in mobile environments, is also a major advantage."
💬 In an Infrastructure Team Meeting
"With WireGuard, you only need to open UDP port 51820. The configuration file is simple, at around 20 lines, so it is easy to automate with Ansible or Terraform. If we adopt a WireGuard-based mesh VPN like Tailscale, we can have P2P connections without a complicated hub-and-spoke setup."
💬 In a Security Review Meeting
"WireGuard uses only ChaCha20-Poly1305 and Curve25519, so there is no cipher suite negotiation. An attacker cannot downgrade to weak encryption, which is actually advantageous for security. However, it supports PFS (Perfect Forward Secrecy) by default, so past traffic stays safe even if a key is exposed."

⚠️ Common Mistakes & Cautions

❌
Committing the Private Key directly in the configuration file

If you commit the wg0.conf file to Git as is, the Private Key is exposed. Use environment variables or Vault, and add *.conf to .gitignore.

❌
AllowedIPs configuration errors

AllowedIPs specifies both "IPs that can come from this peer" and "IPs to route to this peer" at the same time. 0.0.0.0/0 sends all traffic through the VPN, and specifying only particular subnets results in Split Tunneling.

❌
Not setting PersistentKeepalive behind NAT

If a client is behind NAT, the server cannot send packets first. Set PersistentKeepalive = 25 to send a keepalive packet every 25 seconds and maintain the NAT mapping.

✅
Correct WireGuard Operating Practices

Generate a unique key for each device and rotate it regularly. Automate firewall rules with PostUp/PostDown scripts, and monitor the handshake time with wg show to check the connection status.

🔗 Related Terms

📚 Learn More