作者: Rico Wu
環境: OPNsense 25.x
應用場景: 遠端辦公、跨地分公司連線、行動員工 VPN
一、為什麼選擇 OPNsense + OpenVPN?
OpenVPN 是一款開源、安全且穩定的 VPN 解決方案,
而 OPNsense 內建完整的 OpenVPN 管理介面,可快速建立:
- ✅ 遠端使用者 VPN(Remote Access)
- ✅ 站點對站點 VPN(Site-to-Site)
- ✅ 多使用者憑證控管
- ✅ 自動匯出
.ovpn客戶端設定檔
與傳統 VPN 相比,OpenVPN 使用 TLS/SSL 驗證機制,
可確保連線安全並支援跨平台(Windows / macOS / Linux / iOS / Android)。
二、建立 OpenVPN 伺服器(Server)
🔹 步驟 1:建立憑證授權中心(CA)
1️⃣ 前往 System → Trust → Authorities → +Add
2️⃣ 設定如下:
Descriptive name: VPN_CA
Method: Create an internal Certificate Authority
Key length: 4096
Digest Algorithm: SHA256
Lifetime: 3650
→ 儲存 ✅
🔹 步驟 2:建立伺服器憑證
1️⃣ 前往 System → Trust → Certificates → +Add
2️⃣ 設定:
Descriptive name: VPN_Server
Method: Create an internal Certificate
Certificate Authority: VPN_CA
Type: Server Certificate
Key length: 4096
Lifetime: 1095
🔹 步驟 3:建立 OpenVPN Server
前往 VPN → OpenVPN → Servers → +Add
建議設定如下:
| 項目 | 設定值 |
|---|---|
| Server Mode | Remote Access (SSL/TLS + User Auth) |
| Protocol | UDP |
| Interface | WAN |
| Local Port | 1194 |
| IPv4 Tunnel Network | 10.8.0.0/24 |
| IPv4 Local Network | 192.168.1.0/24 |
| TLS Authentication | ✅ 啟用 |
| Encryption | AES-256-GCM |
| Auth Digest | SHA256 |
| Redirect Gateway | ✅ (讓流量走 VPN) |
→ 儲存並套用設定。
三、建立使用者帳號與憑證
1️⃣ 前往 System → Access → Users → +Add
2️⃣ 填寫:
Username: rico
Password: (自訂)
3️⃣ 勾選「Click to create a certificate」
Certificate Authority: VPN_CA
Type: User Certificate
→ 儲存 ✅
四、匯出使用者設定檔
前往 VPN → OpenVPN → Client Export
1️⃣ 選擇剛建立的 Server(例如 HQ_VPN_Server)
2️⃣ 找到使用者帳號(例如 rico)
3️⃣ 點選:
- File Only → 匯出
.ovpn檔 - 或 Windows Installer → 產生完整安裝包
五、客戶端設定方式
💻 Windows
- 安裝 OpenVPN Connect
- 匯入
.ovpn檔 - 點選「Connect」即可
🐧 Linux
sudo apt install openvpn
sudo openvpn --config barry.ovpn
📱 iOS / Android
安裝 OpenVPN Connect App → 匯入 .ovpn → 連線。
六、允許 VPN 使用者存取多個內部網段
若公司內部有多個網段(例如 NAS、伺服器網段):
🔸 例:
LAN1: 192.168.1.0/24
LAN2: 192.168.2.0/24
🔸 設定方式:
VPN → OpenVPN → Servers → Routing → Local Network
192.168.1.0/24,192.168.2.0/24
再到:
Firewall → Rules → OpenVPN
Action: Pass
Source: 10.8.0.0/24
Destination: any
即可讓 VPN 使用者同時存取多個內部網段。
七、防火牆與 NAT 設定檢查
✅ 防火牆規則(WAN)
Action: Pass
Protocol: UDP
Destination Port: 1194
✅ Outbound NAT(Hybrid 模式)
Source: 10.8.0.0/24
Translation: Interface Address (WAN)
八、常見問題與排除
| 問題 | 解法 |
|---|---|
| 無法連線 | 檢查防火牆是否開放 UDP 1194 |
| 已連線但無法上網 | 勾選「Redirect Gateway」或調整 NAT |
| 憑證錯誤 | 確認 CA 與憑證對應正確 |
| 內部多網段無法互通 | 新增路由或修改 Local Network |
| Gateway 顯示 Offline | 檢查上游閘道或 DNS 設定 |
九、架構示意圖
[ Internet ]
|
(WAN)
[ OPNsense VPN Server ]
| \
| [192.168.1.0/24]
| [192.168.2.0/24]
|
[ Remote User 10.8.0.x ]
🔑 結語
透過 OPNsense 的 OpenVPN 整合介面,
企業可輕鬆建立即時、安全的遠端存取方案。
結合多網段支援與自動匯出設定功能,
讓遠端員工與海外分公司能安全地連回公司內網,
達到 安全、穩定、跨地協作 的目標。