---
title: "Trên Rocky Linux 9: Hướng dẫn cài đặt WireGuard VPN làm gateway nội bộ"
description: "Hướng dẫn cài đặt WireGuard VPN trên Rocky Linux 9 làm gateway kết nối mạng nội bộ an toàn giữa các VPS, bật IP forwarding và cấu hình firewalld."
canonical: "https://thuecloud.vn/tren-rocky-linux-9-huong-dan-cai-dat-wireguard-vpn-lam-gateway-noi-bo"
markdown_url: "https://thuecloud.vn/tren-rocky-linux-9-huong-dan-cai-dat-wireguard-vpn-lam-gateway-noi-bo.md"
locale: "vi"
kind: "article"
published_at: "2026-11-22T16:18:29.982Z"
updated_at: "2026-11-22T16:18:29.248Z"
site: "ThueCloud.Vn"
estimated_tokens: 1661
---
# Trên Rocky Linux 9: Hướng dẫn cài đặt WireGuard VPN làm gateway nội bộ

> Hướng dẫn cài đặt WireGuard VPN trên Rocky Linux 9 làm gateway kết nối mạng nội bộ an toàn giữa các VPS, bật IP forwarding và cấu hình firewalld.

- Canonical HTML: https://thuecloud.vn/tren-rocky-linux-9-huong-dan-cai-dat-wireguard-vpn-lam-gateway-noi-bo
- Markdown URL: https://thuecloud.vn/tren-rocky-linux-9-huong-dan-cai-dat-wireguard-vpn-lam-gateway-noi-bo.md
- Organization: ThueCloud.Vn
- Support Email: support@thuecloud.vn
- Hotline: 03333 499 48
- Published: 2026-11-22
- Updated: 2026-11-22

## Mục lục

- Cài đặt WireGuard và tạo cặp khóa trên Rocky Linux 9
  - Bảng quy hoạch dải IP nội bộ WireGuard mẫu
- Cấu hình /etc/wireguard/wg0.conf và Firewalld
- Câu hỏi thường gặp (FAQ)
  - WireGuard có làm chậm tốc độ truyền tải giữa Web Server và Database không?
  - Tại sao các máy Client (App Node/DB Node) kết nối tới Hub nhưng không ping thấy nhau?
  - Khi VPS khởi động lại, WireGuard có tự động kết nối lại không?

Khi vận hành nhiều node [VPS Việt Nam](https://thuecloud.vn/vps-viet-nam) (ví dụ tách riêng App Server, Database Server và Monitoring), việc mở cổng Database (5432/3306) hoặc Redis (6379) ra IP công cộng tiềm ẩn nhiều rủi ro. Cài đặt **WireGuard VPN** trên **Rocky Linux 9** làm gateway nội bộ giúp các máy chủ giao tiếp qua đường hầm mã hóa siêu nhẹ với độ trễ gần như bằng 0.

> **Tóm tắt nhanh (TL;DR):**
> - Rocky Linux 9 tích hợp sẵn kernel module WireGuard; chỉ cần cài gói `wireguard-tools`.
> - Bật `net.ipv4.ip_forward = 1` và cấu hình `firewalld` masquerade nếu cần định tuyến qua gateway.
> - Chỉ cho phép Database/Redis lắng nghe trên IP ảo của giao diện `wg0` (ví dụ `10.88.0.1`).
> - *Cập nhật: 11/2026*

## 1. Cài đặt WireGuard và tạo cặp khóa trên Rocky Linux 9

```bash
sudo dnf install -y epel-release
sudo dnf install -y wireguard-tools

# Sinh cặp khóa Private/Public với quyền bảo mật chặt chẽ
umask 077
wg genkey | sudo tee /etc/wireguard/private.key | wg pubkey | sudo tee /etc/wireguard/public.key
```

### Bảng quy hoạch dải IP nội bộ WireGuard mẫu

| Node | IP Công cộng | IP Tunnel `wg0` | Vai trò |
| :--- | :--- | :--- | :--- |
| **Gateway Hub** | `203.x.x.10` | `10.88.0.1/24` | Định tuyến trung tâm & Admin truy cập |
| **App Node** | `203.x.x.11` | `10.88.0.2/32` | Chạy Web/API, gọi DB qua `10.88.0.3` |
| **DB Node** | `203.x.x.12` | `10.88.0.3/32` | PostgreSQL/MariaDB chỉ bind `10.88.0.3` |

## 2. Cấu hình `/etc/wireguard/wg0.conf` và Firewalld

Trên máy chủ Gateway Hub, tạo file `/etc/wireguard/wg0.conf`:

```ini
[Interface]
Address = 10.88.0.1/24
ListenPort = 51820
PrivateKey = <NOI_DUNG_PRIVATE_KEY_CUA_HUB>

[Peer]
# App Node
PublicKey = <PUBLIC_KEY_APP_NODE>
AllowedIPs = 10.88.0.2/32

[Peer]
# DB Node
PublicKey = <PUBLIC_KEY_DB_NODE>
AllowedIPs = 10.88.0.3/32
```

Bật IP forwarding, mở cổng UDP `51820` trên `firewalld` và khởi chạy `wg0`:

```bash
echo "net.ipv4.ip_forward = 1" | sudo tee /etc/sysctl.d/99-wireguard.conf
sudo sysctl --system

sudo firewall-cmd --permanent --add-port=51820/udp
sudo firewall-cmd --permanent --zone=trusted --add-interface=wg0
sudo firewall-cmd --permanent --add-masquerade
sudo firewall-cmd --reload

sudo systemctl enable --now wg-quick@wg0
sudo wg show
```

Trong mô hình [giải pháp mạng riêng ảo](https://thuecloud.vn/giai-phap), kết hợp WireGuard Gateway với [HTTP Early Hints 103](https://thuecloud.vn/http-early-hints-103-cai-dat-chi-tiet-tu-dau-va-kiem-tra-hoat-dong-thuc-te) (và [xử lý lỗi Early Hints 103](https://thuecloud.vn/http-early-hints-103-phan-tich-loi-thuong-gap-va-quy-trinh-xu-ly-su-co-nhanh)) cũng như [bảo mật Cosign](https://thuecloud.vn/cosign-container-signing-tang-cuong-bao-mat-phan-quyen-va-thiet-lap-giam-sat-canh-bao) sẽ tạo nên một cụm Production khép kín và an toàn.

> **Xây dựng mạng nội bộ liên kết nhiều VPS tại ThueCloud.Vn:** Khởi tạo các node từ **VPS VN 1** (`45.000đ/tháng` năm) hoặc **VPS VN 2** (`78.000đ/tháng` năm) chạy **KVM Enterprise NVMe** cùng cụm `vn-hn` để đạt độ trễ nội bộ dưới 1ms. Miễn phí **VAT 0%**, **SLA 99,9%**, hoàn tiền **07 ngày**. Xem tại [Bảng giá ThueCloud](https://thuecloud.vn/pricing).

## Câu hỏi thường gặp (FAQ)

### WireGuard có làm chậm tốc độ truyền tải giữa Web Server và Database không?
Rất ít. Vì WireGuard chạy trực tiếp trong nhân Linux (kernel-space) với thuật toán mã hóa ChaCha20 hiện đại, băng thông qua tunnel trên VPS NVMe thường đạt gần sát tốc độ card mạng vật lý.

### Tại sao các máy Client (App Node/DB Node) kết nối tới Hub nhưng không ping thấy nhau?
Bạn cần đảm bảo đã bật `net.ipv4.ip_forward = 1` trên Hub, thêm `--zone=trusted --add-interface=wg0` trong `firewalld`, và cấu hình `AllowedIPs = 10.88.0.0/24` ở phía các Peer.

### Khi VPS khởi động lại, WireGuard có tự động kết nối lại không?
Có, nếu bạn đã kích hoạt service bằng lệnh `sudo systemctl enable wg-quick@wg0`.

## References / Sources

- https://www.wireguard.com/install/
- https://docs.rockylinux.org/
- https://firewalld.org/documentation/howto/enable-ip-forwarding.html

---

## Navigation & Machine-Readable Endpoints

- [Site Summary (llms.txt)](https://thuecloud.vn/llms.txt)
- [Full Technical Reference (llms-full.txt)](https://thuecloud.vn/llms-full.txt)
- [Pricing Catalog (Markdown)](https://thuecloud.vn/pricing.md)
- [Blog & Technical Guides (Markdown)](https://thuecloud.vn/blog.md)
- [XML Sitemap](https://thuecloud.vn/sitemap.xml)
