blogbyAndrew

Thiết lập Site-to-Site VPN giữa GCP và Ubuntu với Strongswan

April 4, 2026

Network cables connected to switches in a data center representing VPN connectivity

Giới thiệu

Khi bạn có workload chạy trên Google Cloud Platform (GCP) và cần kết nối an toàn với hạ tầng on-premises hoặc một server ở nơi khác, Site-to-Site VPN là giải pháp phổ biến nhất. Thay vì mở port và expose service ra public internet, VPN tạo một encrypted tunnel giữa hai mạng, cho phép chúng giao tiếp như thể cùng nằm trong một mạng nội bộ.

Bài viết này hướng dẫn cách thiết lập IPSec VPN tunnel giữa GCP Cloud VPN và một server Ubuntu sử dụng Strongswan — một trong những IPSec implementation phổ biến nhất trên Linux.

text
┌────────────────────────────────────────────────────────────┐
│             Site-to-Site VPN Topology                      │
│                                                            │
│  ┌────────────────┐            ┌────────────────┐          │
│  │ GCP VPC        │            │ On-premises    │          │
│  │ (Private Sub)  │            │ (Private Sub)  │          │
│  │                │            │                │          │
│  │ GCP VPN GW     │<--IPSec--->│ Strongswan     │          │
│  │ (Public IP)    │  Tunnel    │ (Public IP)    │          │
│  └────────────────┘            └────────────────┘          │
└────────────────────────────────────────────────────────────┘

Kiến thức nền tảng

IPSec (Internet Protocol Security): Là bộ giao thức bảo mật hoạt động ở Layer 3, cung cấp authentication và encryption cho IP packets. IPSec là nền tảng của hầu hết các giải pháp VPN enterprise.

IKEv2 (Internet Key Exchange version 2): Là giao thức dùng để thiết lập Security Association (SA) giữa hai bên trong IPSec tunnel. IKEv2 nhanh hơn và ổn định hơn so với IKEv1, đặc biệt khi cần reconnect.

Pre-Shared Key (PSK): Là một secret key được chia sẻ trước giữa hai bên (GCP và Strongswan) để xác thực nhau khi thiết lập VPN tunnel. Đơn giản hơn certificate-based authentication nhưng kém linh hoạt hơn.

Strongswan: Là một open-source IPSec implementation cho Linux, hỗ trợ IKEv1, IKEv2, và nhiều authentication methods. Strongswan thay thế cho openswan/libreswan và được sử dụng rộng rãi trong production.

Security Association (SA): Là một kết nối logic một chiều giữa hai bên, định nghĩa các tham số bảo mật (encryption algorithm, key, lifetime). Mỗi IPSec tunnel cần ít nhất hai SA (mỗi chiều một SA).

Yêu cầu trước khi bắt đầu

Thông tinMô tả
GCP Public IPIP của GCP VPN Gateway
GCP Private SubnetCIDR của mạng nội bộ GCP (VPC)
Strongswan Public IPIP public của server Ubuntu
Strongswan Private IPIP private của server Ubuntu
Strongswan Private SubnetCIDR của mạng nội bộ phía Ubuntu

Bước 1: Cấu hình IP Forwarding

Server Ubuntu cần forward IP packets giữa các interfaces để hoạt động như một VPN gateway. Chỉnh sửa /etc/sysctl.conf:

bash
sudo nano /etc/sysctl.conf

Thêm hoặc uncomment các dòng sau:

text
net.ipv4.ip_forward = 1
net.ipv6.conf.all.forwarding = 1
net.ipv4.conf.all.accept_redirects = 0

Apply thay đổi:

bash
sudo sysctl -p

Lưu ý: ip_forward = 1 cho phép kernel forward packets giữa các network interfaces — đây là yêu cầu bắt buộc để server hoạt động như router/VPN gateway. accept_redirects = 0 tắt ICMP redirects để tránh bị thay đổi routing table bởi external sources.

Bước 2: Cài đặt Strongswan

bash
sudo apt update && sudo apt upgrade -y
sudo apt install strongswan strongswan-pki libcharon-extra-plugins \
  libcharon-extauth-plugins libstrongswan-extra-plugins \
  libtss2-tcti-tabrmd0 -y

Bật Strongswan service:

bash
sudo systemctl enable strongswan-starter

Bước 3: Tạo Pre-Shared Key

Tạo một random PSK đủ mạnh:

bash
head -c 24 /dev/urandom | base64

Lưu output lại — bạn sẽ cần dùng key này ở cả hai phía (GCP và Strongswan). Key này cũng cần được cấu hình trên GCP Cloud VPN.

Bước 4: Cấu hình IPSec

4.1 Cấu hình secrets

Thêm PSK vào /etc/ipsec.secrets:

bash
sudo nano /etc/ipsec.secrets
text
STRONGSWAN_PUBLIC_IP GCP_PUBLIC_IP : PSK "YOUR_PSK_VALUE"

Thay STRONGSWAN_PUBLIC_IP, GCP_PUBLIC_IP, và YOUR_PSK_VALUE bằng giá trị thực tế.

4.2 Cấu hình tunnel

Tạo file /etc/ipsec.conf:

bash
sudo nano /etc/ipsec.conf
text
config setup
        charondebug="all"

conn site-b
        type=tunnel
        auto=start
        keyexchange=ikev2
        authby=secret
        right=GCP_PUBLIC_IP
        rightsubnet=GCP_PRIVATE_SUBNET
        left=STRONGSWAN_PRIVATE_IP
        leftid=STRONGSWAN_PUBLIC_IP
        leftsubnet=STRONGSWAN_PRIVATE_SUBNET
        ike=aes256-sha1-modp1024!
        esp=aes256-sha1!
        aggressive=no
        keyingtries=%forever
        ikelifetime=86400s
        lifetime=43200s
        lifebytes=576000000
        dpddelay=30s
        dpdtimeout=120s
        dpdaction=restart

Giải thích các tham số quan trọng:

Tham sốGiá trịMô tả
keyexchangeikev2Sử dụng IKEv2 protocol
authbysecretXác thực bằng Pre-Shared Key
rightGCP Public IPPeer (phía GCP)
rightsubnetGCP CIDRMạng nội bộ phía GCP
leftStrongswan Private IPLocal identity
leftidStrongswan Public IPID gửi cho peer
autostartTự động kết nối khi service start
dpdactionrestartTự động reconnect khi Dead Peer Detection phát hiện tunnel down
keyingtries%foreverRetry không giới hạn nếu kết nối thất bại
text
┌──────────────────────────────────────────────────────────┐
│            IPSec Tunnel Parameters                       │
│                                                          │
│  Phase 1 (IKE SA):                                       │
│    Encryption: AES-256                                   │
│    Hash:       SHA-1                                     │
│    DH Group:   modp1024 (Group 2)                        │
│    Lifetime:   86400s (24 hours)                         │
│                                                          │
│  Phase 2 (Child SA / ESP):                               │
│    Encryption: AES-256                                   │
│    Hash:       SHA-1                                     │
│    Lifetime:   43200s (12 hours)                         │
│    Bytes:      576MB                                     │
│                                                          │
│  Dead Peer Detection:                                    │
│    Delay:   30s                                          │
│    Timeout: 120s                                         │
│    Action:  restart                                      │
└──────────────────────────────────────────────────────────┘

Bước 5: Khởi động VPN tunnel

bash
sudo ipsec restart

Kiểm tra trạng thái kết nối:

bash
sudo ipsec statusall

Kết quả mong đợi — bạn sẽ thấy trạng thái ESTABLISHED cho IKE SA và INSTALLED cho Child SA:

text
Connections:
  site-b: STRONGSWAN_PRIVATE_IP...GCP_PUBLIC_IP  IKEv2
Security Associations:
  site-b[1]: ESTABLISHED ... IKEv2 SPIs: ...
  site-b{1}: INSTALLED, TUNNEL, ESP SPIs: ...

Bước 6: Cấu hình routing trên GCP

Để traffic từ GCP VPC đi qua VPN tunnel đến mạng phía Strongswan, bạn cần tạo route trên GCP:

  1. Trong GCP Console, vào VPC network > Routes
  2. Tạo route mới:
    • Destination: CIDR của mạng phía Strongswan
    • Next hop: VPN tunnel đã tạo

Hoặc sử dụng gcloud:

bash
gcloud compute routes create vpn-route-to-onprem \
  --network=YOUR_VPC \
  --destination-range=STRONGSWAN_PRIVATE_SUBNET \
  --next-hop-vpn-tunnel=YOUR_VPN_TUNNEL \
  --next-hop-vpn-tunnel-region=YOUR_REGION

Troubleshooting

Một số lỗi thường gặp:

Debug bằng tcpdump:

bash
sudo tcpdump -i any -n esp or udp port 500 or udp port 4500

Xem Strongswan logs:

bash
sudo journalctl -u strongswan-starter -f

Tổng kết

BướcMục đích
IP ForwardingCho phép server forward packets qua VPN
Install StrongswanIPSec implementation trên Ubuntu
Pre-Shared KeyAuthentication giữa hai bên
ipsec.conf + ipsec.secretsCấu hình tunnel parameters
ipsec restartKhởi động và kết nối tunnel
GCP RoutesRouting traffic qua VPN tunnel

Site-to-Site VPN với Strongswan là giải pháp đáng tin cậy để kết nối hạ tầng on-premises với GCP. Với dpdaction=restartkeyingtries=%forever, tunnel sẽ tự động reconnect khi gặp sự cố, đảm bảo kết nối liên tục.

Lưu ý bảo mật: SHA-1 và modp1024 (DH Group 2) trong cấu hình trên đã được coi là yếu theo tiêu chuẩn hiện tại. Nếu GCP hỗ trợ, hãy nâng cấp lên sha256 hoặc sha384, và modp2048 (Group 14) hoặc cao hơn cho production.

References