Skip to main content

Site-to-Site VPN với WireGuard

Ở bài viết này, mình sẽ hướng dẫn cấu hình Wireguard để tạo tunnel, cho phép các server/thiết bị trong mạng có thể truy cập, trao đổi dữ liệu giữa các site khác nhau. Nội dung sẽ kèm mô hình lab tượng trưng cho khởi tạo kết nối giữa AWS <-> On-premise và là giải pháp bổ sung thay thế cho VPN của AWS.

Đối với môi trường 2 Sites/VPCs thuần trên AWS, bạn nên sử dụng VPC Peering. Hoặc, nếu nhiều hơn 2 Sites/VPCs - tham khảo Transit Gateway.

1. Mô hình lab

Picture here

a. Bảng cấu hình

Region us-east-1 ap-southeast-1
CIDR Block 10.10.0.0/16 10.20.0.0/16
WireGuard 10.0.1.1 10.0.1.2
Server/NAT 10.10.100.10 10.20.100.10
Private server 10.10.0.10 10.20.0.10

b. Lab

Mình có chuẩn bị sẵn hạ tầng (VPC, route table, EC2, Security group) trên AWS giữa các regions với Terraform. Nếu muốn thực nghiệm, bạn có thể clone source về sử dụng tại: https://github.com/Kocoji/wg-lab-infra.

Yêu cầu trước khi sử dụng (Linux/macOS):

  • Chuẩn bị sẵn SSH key để sử dụng truy cập vào cấu hình trên Wireguard Server. Nếu chưa có, bạn có thể tạo với lệnh: ssh-keygen
    • Giữ lại thông tin *.pub để nhập vào Terraform
    • Dùng Private key để SSH và truy cập vào Server
  • Tài khoản AWS:
    • Sử dụng env var: AWS_ACCESS_KEY_ID="key"AWS_SECRET_ACCESS_KEY="secret"
  • Terraform… dĩ nhiên rồi 😗

Lưu ý 01: Nếu tạo thủ công, đối với hạ tầng AWS Cloud, bạn cần:

  • Tắt Source Destination Check trên ENI
  • Cấu hình route table để các dịch vụ trong private subnet biết & kết nối tới Site còn lại

Lưu ý 02: Nếu server trong mạng nội bộ không cấu hình route qua Linux NAT

  • Cấu hình thêm route đến CIDR site còn lại cho những server này đến Linux NAT

Sau khi đủ xong thì … tới bước cấu hình thôi!

2. Cấu hình

Dưới dây là các bước cài đặt Wireguard trên cả 2 servers mỗi site. Nếu sử dụng lab link mình đính kèm, có thể bỏ qua bước này!

sudo su
apt update
apt install -y wireguard
cd /etc/wireguard 
# tạo file với full quyền chỉ cho user root
umask 077; wg genkey | tee privatekey | wg pubkey > publickey
# cho phép forward gói tin ipv4 (chỉ cần trên wireguard server và server NAT tại site B)
echo "net.ipv4.ip_forward=1" >> /etc/sysctl.conf
sysctl -p

Kiểm lại nội dung của các files: /etc/wireguard/publickey/etc/wireguard/privatekey sau đó tiến hành các bước tiếp theo.

a. Wireguard Server

Trên Server, truy cập SSH và cập nhật lại thông tin cấu hình theo đường dẫn: /etc/wireguard, tạo file cấu hình wg0.conf có nội dung như sau… Mình có ghi chú thêm tại file cấu hình ở những Key cấu hình.

[Interface]
# VPN server private IP address
Address = 10.0.1.1/24
# Run the iptables when the wg service is up, and otherwise. IF use as NAT mode only.
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
ListenPort = 51820
# VPN server's private 
PrivateKey = gKeHLrqIhaEctuyjksXJwh0g62Sh4EGHccoEXbdJZVg=

[Peer]
# Client VPN public key
PublicKey = /p+ejOznLn8EbQ4LByxl+me5pzL1+mQflJ8GjAjHuyU=
AllowedIPs = 10.0.1.2/32, 10.20.0.0/16 # cidr block of site ap-southeast-1

Lưu ý: cập nhập lại thông tin PrivateKey của Wireguard Server, và PublicKey của Server Peer

Sau khi cấu hình xong, bắt đầu start và enable dịch vụ:

systemctl enable wg-quick@wg0
systemctl start wg-quick@wg0
systemctl status wg-quick@wg0
root@ip-10-10-100-10:/etc/wireguard# systemctl status wg-quick@wg0
● wg-quick@wg0.service - WireGuard via wg-quick(8) for wg0
     Loaded: loaded (/lib/systemd/system/wg-quick@.service; enabled; vendor preset: enabled)
     Active: active (exited) since Thu 2022-08-18 14:37:11 UTC; 2s ago
       Docs: man:wg-quick(8)
             man:wg(8)
             https://www.wireguard.com/
             https://www.wireguard.com/quickstart/
             https://git.zx2c4.com/wireguard-tools/about/src/man/wg-quick.8
             https://git.zx2c4.com/wireguard-tools/about/src/man/wg.8
    Process: 1963 ExecStart=/usr/bin/wg-quick up wg0 (code=exited, status=0/SUCCESS)
   Main PID: 1963 (code=exited, status=0/SUCCESS)

Aug 7 14:37:11 ip-10-10-100-10 systemd[1]: Starting WireGuard via wg-quick(8) for wg0...
Aug 7 14:37:11 ip-10-10-100-10 wg-quick[1963]: [#] ip link add wg0 type wireguard
Aug 7 14:37:11 ip-10-10-100-10 wg-quick[1963]: [#] wg setconf wg0 /dev/fd/63
Aug 7 14:37:11 ip-10-10-100-10 wg-quick[1963]: [#] ip -4 address add 10.0.1.1/24 dev wg0
Aug 7 14:37:11 ip-10-10-100-10 wg-quick[1963]: [#] ip link set mtu 8921 up dev wg0
Aug 7 14:37:11 ip-10-10-100-10 wg-quick[1963]: [#] ip -4 route add 10.20.0.0/16 dev wg0
Aug 7 14:37:11 ip-10-10-100-10 wg-quick[1963]: [#] iptables -A FORWARD -i wg0 -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
Aug 7 14:37:11 ip-10-10-100-10 systemd[1]: Finished WireGuard via wg-quick(8) for wg0.

b. Peers

Tại Peer, chỉnh lại file cấu hình thông tin cấu hình theo đường dẫn: /etc/wireguard, tạo file cấu hình wg0.conf có nội dung như sau.

[Interface]
# client's private key
PrivateKey = cKg4GpbaKnQmW1FRzyKCJit2U0HrFITJlzRDWpFEbnE=
# Client ip address
Address = 10.0.1.2/24
# Run the iptables when the wg service is up, and otherwise. IF use as NAT mode only.
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]
# Wireguard server public key
PublicKey = 82/1ykJGF6IpkdIhMPgJVlj7bB+j1Tr3iDIWAx4rs3Y=
AllowedIPs = 10.0.1.0/24, 10.10.0.0/16 # Site us-east-1
#public IP of the Server, and the listen port.
Endpoint = 107.23.222.221:51820
 
# PersistentKeepalive in the case the client is behind the NAT
PersistentKeepalive = 30

Lưu ý: cập nhập lại thông tin PrivateKey của Peer. EndpointPublicKey của Wireguard Server

Tương tự từ phía Server:

systemctl enable wg-quick@wg0
systemctl start wg-quick@wg0
systemctl status wg-quick@wg0

3. Kết quả

Thử đứng từ Peer ở ap-southeast-1 kết nối đến server trên private subnet ở us-east-1

ubuntu@ip-10-20-100-10:~$ ssh 10.10.0.10 -i kocoji 
Enter passphrase for key 'kocoji': 
Welcome to Ubuntu 20.04.4 LTS (GNU/Linux 5.15.0-1017-aws x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

  System information as of Thu Aug 7 15:09:05 UTC 2022

  System load:  0.0               Processes:             98
  Usage of /:   19.8% of 7.57GB   Users logged in:       0
  Memory usage: 22%               IPv4 address for eth0: 10.10.0.10
  Swap usage:   0%

1 update can be applied immediately.
To see these additional updates run: apt list --upgradable


The list of available updates is more than a week old.
To check for new updates run: sudo apt update


The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.

To run a command as administrator (user "root"), use "sudo <command>".
See "man sudo_root" for details.

ubuntu@ip-10-10-0-10:~$

Kiểm tra thêm, từ server trên private subnet ở us-east-1 đến private server tại ap-southeast-1

ubuntu@ip-10-10-0-10:~$ ping -c 4 10.20.0.10
PING 10.20.0.10 (10.20.0.10) 56(84) bytes of data.
64 bytes from 10.20.0.10: icmp_seq=1 ttl=62 time=213 ms
64 bytes from 10.20.0.10: icmp_seq=2 ttl=62 time=214 ms
64 bytes from 10.20.0.10: icmp_seq=3 ttl=62 time=214 ms
64 bytes from 10.20.0.10: icmp_seq=4 ttl=62 time=214 ms

--- 10.20.0.10 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3005ms
rtt min/avg/max/mdev = 213.218/213.534/213.698/0.186 ms

4. Mục bổ sung

Nếu gặp sự cố mà không biết rõ nguyên nhân từ đâu… bạn có thể sử dụng lệnh dưới đây để bật debug mode.

modprobe wireguard && echo module wireguard +p > /sys/kernel/debug/dynamic_debug/control

Và… dùng dmesg để tìm ra nguyên nhân thôi!

Mình sẽ bổ sung, cập nhật lại những thông tin bị thiếu hoặc sai sót nếu có. Cảm ơn đã đọc bài viết này, hi vọng những thông tin trên sẽ có ích cho bạn!