Skip to content

Nuface Blog

隨意隨手記 Casual Notes

Menu
  • Home
  • About
  • Services
  • Blog
  • Contact
  • Privacy Policy
  • Login
Menu

在 OPNsense 上建立與設定 OpenVPN:完整實務指南

Posted on 2025-11-062025-11-20 by Rico

作者: 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 ModeRemote Access (SSL/TLS + User Auth)
ProtocolUDP
InterfaceWAN
Local Port1194
IPv4 Tunnel Network10.8.0.0/24
IPv4 Local Network192.168.1.0/24
TLS Authentication✅ 啟用
EncryptionAES-256-GCM
Auth DigestSHA256
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

  1. 安裝 OpenVPN Connect
  2. 匯入 .ovpn 檔
  3. 點選「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 整合介面,
企業可輕鬆建立即時、安全的遠端存取方案。

結合多網段支援與自動匯出設定功能,
讓遠端員工與海外分公司能安全地連回公司內網,
達到 安全、穩定、跨地協作 的目標。

Recent Posts

  • Postfix + Let’s Encrypt + BIND9 + DANE Fully Automated TLSA Update Guide
  • Postfix + Let’s Encrypt + BIND9 + DANE TLSA 指紋自動更新完整教學
  • Deploying DANE in Postfix
  • 如何在 Postfix 中部署 DANE
  • DANE: DNSSEC-Based TLS Protection

Recent Comments

  1. Building a Complete Enterprise-Grade Mail System (Overview) - Nuface Blog on High Availability Architecture, Failover, GeoDNS, Monitoring, and Email Abuse Automation (SOAR)
  2. Building a Complete Enterprise-Grade Mail System (Overview) - Nuface Blog on MariaDB + PostfixAdmin: The Core of Virtual Domain & Mailbox Management
  3. Building a Complete Enterprise-Grade Mail System (Overview) - Nuface Blog on Daily Operations, Monitoring, and Performance Tuning for an Enterprise Mail System
  4. Building a Complete Enterprise-Grade Mail System (Overview) - Nuface Blog on Final Chapter: Complete Troubleshooting Guide & Frequently Asked Questions (FAQ)
  5. Building a Complete Enterprise-Grade Mail System (Overview) - Nuface Blog on Network Architecture, DNS Configuration, TLS Design, and Postfix/Dovecot SNI Explained

Archives

  • December 2025
  • November 2025
  • October 2025

Categories

  • AI
  • Apache
  • Cybersecurity
  • Database
  • DNS
  • Docker
  • Fail2Ban
  • FileSystem
  • Firewall
  • Linux
  • LLM
  • Mail
  • N8N
  • OpenLdap
  • OPNsense
  • PHP
  • QoS
  • Samba
  • Switch
  • Virtualization
  • VPN
  • WordPress
© 2025 Nuface Blog | Powered by Superbs Personal Blog theme