Home » Cài đặt WireGuard sử dụng wg-easy
Posted in

Cài đặt WireGuard sử dụng wg-easy

Dưới đây là hướng dẫn chi tiết để bạn cài đặt và cấu hình WireGuard sử dụng wg-easy trên VPS Ubuntu 22.04 và kết nối với 1 client Windows.

Bước 1: Chuẩn bị VPS Ubuntu 22.04

  • Bạn đã có VPS chạy Ubuntu 22.04, có quyền sudo
  • VPS có kết nối internet
  • Cổng UDP (mặc định 51820 hoặc bạn chọn) cần được mở trên firewall & VPS

Bước 2: Cài đặt Docker & Docker Compose

wg-easy hoạt động dựa trên Docker, nên bạn cần cài Docker trước.

Mở terminal trên VPS, chạy lần lượt:

sudo apt update
sudo apt install -y docker.io docker-compose
sudo systemctl enable --now docker

Kiểm tra Docker đã chạy:

sudo docker run hello-world

Nếu thấy thông báo chào mừng thì Docker đã cài thành công.

Bước 3: Cài đặt và chạy wg-easy

Tạo thư mục lưu cấu hình wg-easy:

mkdir ~/wg-easy
cd ~/wg-easy

Tạo file docker-compose.yml với nội dung sau:

version: '3.8'
services:
  wg-easy:
    image: weejewel/wg-easy
    container_name: wg-easy
    restart: always
    ports:
      - "51820:51820/udp"   # Cổng UDP WireGuard
      - "51821:51821/tcp"   # Cổng web UI
    environment:
      - WG_HOST=YOUR_VPS_IP_OR_DOMAIN
      - PASSWORD=yourpassword
    volumes:
      - ./config:/etc/wireguard
    cap_add:
      - NET_ADMIN
      - SYS_MODULE

Lưu ý:

Thay YOUR_VPS_IP_OR_DOMAIN bằng IP public hoặc domain của VPS bạn.

PASSWORD=yourpassword là mật khẩu đăng nhập giao diện web quản lý wg-easy (bạn tự đặt).

Khởi chạy container:

sudo docker-compose up -d

Kiểm tra container đang chạy:

sudo docker ps

Nếu thấy container wg-easy chạy, bạn có thể truy cập giao diện quản lý bằng trình duyệt:

http://YOUR_VPS_IP:51821

Đăng nhập với mật khẩu bạn đã đặt.

Bước 4: Tạo user/client WireGuard qua web UI

Đăng nhập web UI wg-easy

Click “Add Client” để tạo client mới

Bạn sẽ có file cấu hình .conf cho client

Bước 5: Cài đặt WireGuard trên Windows Client

Tải WireGuard cho Windows:
https://www.wireguard.com/install/

Cài đặt phần mềm và mở WireGuard trên Windows

Tại giao diện WireGuard, click “Import tunnel(s) from file…”

Chọn file .conf bạn tải từ wg-easy web UI (hoặc copy-paste cấu hình)

Kích hoạt tunnel WireGuard trên Windows

Mở cổng (UDP) cho WireGuard trên VPS

sudo ufw allow 51820/udp
sudo ufw enable

Bật IP forwarding trên VPS:

echo "net.ipv4.ip_forward=1" | sudo tee -a /etc/sysctl.conf
echo "net.ipv6.conf.all.forwarding=1" | sudo tee -a /etc/sysctl.conf
sudo sysctl -p

Trên client WireGuard: route toàn bộ Internet qua VPN

[Interface]
PrivateKey = <private_key_client>
Address = 10.8.0.2/24
DNS = 1.1.1.1

[Peer]
PublicKey = <public_key_server>
Endpoint = <VPS_IP>:51820
AllowedIPs = 0.0.0.0/0, ::/0
PersistentKeepalive = 25

AllowedIPs = 0.0.0.0/0 sẽ đẩy toàn bộ lưu lượng Internet qua WireGuard tunnel

Leave a Reply

Your email address will not be published. Required fields are marked *