VPS VPN WireGuard

如何在 VPS 上架設自己的 VPN:WireGuard 完整安裝教學

手把手教你在 Linux VPS 上用 WireGuard 架設私人 VPN 伺服器,涵蓋金鑰產生、伺服器與客戶端設定、防火牆規則、手機連線,打造屬於自己的加密通道。

用別人的 VPN 服務,你的流量就要經過別人的伺服器。如果你在意隱私,或是需要一個穩定的加密通道來遠端存取內部資源,最踏實的做法就是在自己的 VPS 上架一台 VPN 伺服器。

WireGuard 是目前最值得選擇的 VPN 協定。它從 Linux 5.6 開始就內建在核心裡,程式碼只有大約 4,000 行(OpenVPN 超過 40 萬行),設定簡單、效能優異、安全性高。這篇教學以 Ubuntu 24.04 為例,帶你從零架好一台 WireGuard VPN 伺服器。

WireGuard vs OpenVPN:為什麼選 WireGuard

WireGuard OpenVPN
程式碼量 ~4,000 行 ~400,000 行
協定 UDP TCP / UDP
加密方式 ChaCha20 + Curve25519 OpenSSL(可選多種)
連線速度 快(核心層級運作) 較慢(使用者空間)
設定複雜度
核心整合 Linux 5.6+ 原生支援 需安裝額外軟體

簡單說:WireGuard 更快、更簡單、更安全。除非你有特殊需求(例如需要 TCP 協定來繞過某些網路限制),否則 WireGuard 是 2026 年的首選。

Step 1:安裝 WireGuard

SSH 登入你的 VPS,安裝 WireGuard:

1
2
sudo apt update
sudo apt install -y wireguard

確認安裝成功:

1
wg --version

Ubuntu 22.04 以上的核心已經內建 WireGuard 模組,不需要額外安裝核心套件。

Step 2:產生伺服器金鑰

WireGuard 的認證機制和 SSH Key 類似,也是用公私鑰配對:

1
2
3
4
5
6
# 產生私鑰
wg genkey | sudo tee /etc/wireguard/server_private.key
sudo chmod 600 /etc/wireguard/server_private.key

# 從私鑰推導出公鑰
sudo cat /etc/wireguard/server_private.key | wg pubkey | sudo tee /etc/wireguard/server_public.key

記下公鑰的內容,等一下設定客戶端時會用到。

Step 3:產生客戶端金鑰

在伺服器上幫客戶端也產生一組(也可以在客戶端電腦上自己產生,更安全):

1
2
3
wg genkey | sudo tee /etc/wireguard/client1_private.key
sudo cat /etc/wireguard/client1_private.key | wg pubkey | sudo tee /etc/wireguard/client1_public.key
sudo chmod 600 /etc/wireguard/client1_private.key

Step 4:設定伺服器

建立 WireGuard 的設定檔:

1
sudo nano /etc/wireguard/wg0.conf

寫入以下內容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[Interface]
# 伺服器的私鑰
PrivateKey = 貼上 server_private.key 的內容
# VPN 內部網段,伺服器用 .1
Address = 10.0.0.1/24
# WireGuard 監聽的 UDP Port
ListenPort = 51820
# 啟動時設定 NAT 轉發規則
PostUp = iptables -A FORWARD -i wg0 -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
PostDown = iptables -D FORWARD -i wg0 -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE

[Peer]
# 客戶端的公鑰
PublicKey = 貼上 client1_public.key 的內容
# 允許這個客戶端使用的 VPN 內部 IP
AllowedIPs = 10.0.0.2/32

注意PostUpPostDown 裡的 eth0 要換成你 VPS 實際的網路介面名稱。用以下指令確認:

1
2
3
ip route show default
# 輸出範例:default via 192.168.1.1 dev eth0 proto dhcp
# "dev" 後面的就是你的介面名稱(可能是 eth0、ens3、enp0s3 等)

Step 5:開啟 IP 轉發

讓 VPS 能夠轉發 VPN 客戶端的網路封包:

1
2
echo 'net.ipv4.ip_forward = 1' | sudo tee -a /etc/sysctl.conf
sudo sysctl -p

Step 6:設定防火牆

開放 WireGuard 使用的 UDP Port:

1
2
sudo ufw allow 51820/udp
sudo ufw status

Step 7:啟動 WireGuard

1
2
3
4
5
6
7
8
# 啟動
sudo systemctl start wg-quick@wg0

# 設定開機自動啟動
sudo systemctl enable wg-quick@wg0

# 確認狀態
sudo wg show

看到 interface: wg0 和相關資訊就表示伺服器端設定完成。

Step 8:設定客戶端

電腦端(Windows / macOS / Linux)

WireGuard 官網 下載對應平台的客戶端。

建立一個設定檔 client1.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[Interface]
# 客戶端的私鑰
PrivateKey = 貼上 client1_private.key 的內容
# 客戶端的 VPN 內部 IP
Address = 10.0.0.2/24
# VPN 啟用時使用的 DNS
DNS = 8.8.8.8

[Peer]
# 伺服器的公鑰
PublicKey = 貼上 server_public.key 的內容
# 所有流量都走 VPN
AllowedIPs = 0.0.0.0/0
# 伺服器的公網 IP 和 Port
Endpoint = 你的VPS_IP:51820
# 保持連線(NAT 穿透用)
PersistentKeepalive = 25

把這個設定檔匯入 WireGuard 客戶端,按下連線就可以了。

AllowedIPs 的選擇0.0.0.0/0 表示所有流量都走 VPN(全域模式)。如果你只想讓特定流量走 VPN,可以改成目標網段,例如 10.0.0.0/24, 192.168.1.0/24

手機端(iOS / Android)

到 App Store 或 Google Play 搜尋「WireGuard」下載官方 App。

最方便的連線方式是用 QR Code。在伺服器上安裝 qrencode

1
2
sudo apt install -y qrencode
qrencode -t ansiutf8 < client1.conf

終端機會顯示一個 QR Code,用手機的 WireGuard App 掃描即可匯入設定。

新增更多客戶端

每個裝置都需要獨立的金鑰對。流程是:

  1. 產生新的金鑰對(wg genkey | tee client2_private.key | wg pubkey > client2_public.key
  2. 在伺服器的 wg0.conf 加入新的 [Peer] 區塊,指定新的 AllowedIPs(例如 10.0.0.3/32
  3. 重新載入設定:sudo wg syncconf wg0 <(wg-quick strip wg0)
  4. 建立客戶端的設定檔,匯入客戶端軟體

驗證 VPN 連線

連上 VPN 後,打開瀏覽器訪問 https://ifconfig.me,看看顯示的 IP 是不是你 VPS 的 IP。如果是,表示所有流量都成功透過 VPN 轉發了。

也可以用指令確認:

1
curl ifconfig.me

常見問題排除

Q:客戶端連不上?

依序檢查:防火牆有沒有開 51820/udp、IP 轉發有沒有開、伺服器的 wg0.conf 裡客戶端的公鑰有沒有貼對、PostUp 裡的網路介面名稱有沒有寫對。

Q:連上了但沒辦法上網?

通常是 NAT 轉發規則沒生效。確認 iptables -t nat -L 裡有 MASQUERADE 規則,以及 sysctl net.ipv4.ip_forward 的值是 1。

Q:速度不如預期?

WireGuard 走 UDP 協定,某些 ISP 可能會對 UDP 做 QoS 限速。另外,VPS 的頻寬上限也會影響速度。如果你的 VPS 是 100Mbps 的方案,VPN 速度自然不會超過 100Mbps。

結語

用 WireGuard 在 VPS 上架 VPN,從安裝到連線大約 20 分鐘就能搞定。跟商業 VPN 服務比起來,自架的好處是完全掌控——你的流量不經過第三方、Log 要不要留由你決定、想加多少裝置都不用加價。

如果你需要一台穩定的台灣 VPS 來跑 WireGuard,NCSE Network 提供搭載 NVMe SSD 的 VPS 方案,獨立 IP、1Gbps 高速網路,適合作為你的私人 VPN 節點。台灣的使用者透過本地節點連線,延遲極低,體驗幾乎和沒開 VPN 一樣順暢。

需要穩定的雲端主機?

NCSE Network 提供企業級 VPS,7 天免費試用,臺灣是方電訊機房,99% SLA 保證。

查看 VPS 方案 →