banner
Alexeisie

AlexEisie

啊? Email: alexeisie@brs.red
github

Wireguard 異地組網

1. 服務器端環境配置#

開啟伺服器端內核轉發

sysctl net.ipv4.ip_forward=1
sysctl net.ipv6.conf.all.forwarding=1
sysctl -p

安裝 Wireguard 程式

apt install wireguard

2. 伺服器端 Wireguard 介面配置#

生成服務器端密鑰對

wg genkey | tee /dev/stderr | wg pubkey

在 /etc/wireguard 資料夾下新建檔案 ifs0.conf
wireguard 服務監聽 10203/udp
組網使用 192.168.3.0/24 私有網段
伺服器分配 192.168.3.1/32
客戶端分配 192.168.3.2/32
此處 eth0 應替換為實際出口介面名稱
Peer 的 AllowedIPs 用於匹配入站流量的 IP 地址,匹配成功時被 wireguard 接管

[Interface]
PrivateKey = 服務器端私鑰
Address = 192.168.3.1/32
PostUp   = iptables -A FORWARD -i ifs0 -j ACCEPT; iptables -A FORWARD -o ifs0 -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
PostDown = iptables -D FORWARD -i ifs0 -j ACCEPT; iptables -D FORWARD -o ifs0 -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE
ListenPort = 10203

[Peer]
PublicKey =  客戶端公鑰
AllowedIPs = 192.168.3.2/32

啟動 wireguard 的 ifs0 配置,同時執行 PostUp 中的設定

wg-quick up ifs0

關閉 wireguard 的 ifs0 配置,同時執行 PostDown 中的設定

wg-quick down ifs0

3. 客戶端配置#

安裝 Wireguard 程式

apt install wireguard

生成客戶端密鑰對

wg genkey | tee /dev/stderr | wg pubkey

在 /etc/wireguard 資料夾下新建檔案 ifc0.conf
該客戶端分配先前伺服器待處理的 IP 地址 192.168.3.2/32
Peer 終端點填寫伺服器 wireguard 進程 (假設客戶端可以通過 114.51.41.91 訪問該伺服器)
此時 Peer 的 AllowedIPs 用於匹配出站流量的 IP 地址,匹配成功時將會通過 wireguard 使用隧道訪問,實例配置為處理全部 IPv4 流量,當然也可以使用 192.168.3.0/24 來僅處理組網 IPv4 流量

[Interface]
PrivateKey = 客戶端私鑰
Address = 192.168.3.2/32

[Peer]
PublicKey = 服務器端公鑰
AllowedIPs = 0.0.0.0/0
Endpoint = 114.51.41.91:10203

啟動 wireguard 的 ifs0 配置,同時執行 PostUp 中的設定

wg-quick up ifs0

關閉 wireguard 的 ifs0 配置,同時執行 PostDown 中的設定

wg-quick down ifs0

4.1 多客戶端 (伺服器抽象為網關)#

僅需要在伺服器添加多 Peer 匹配即可

[Interface]
###略###

[Peer]
PublicKey =  客戶端192.168.3.2公鑰
AllowedIPs = 192.168.3.2/32

[Peer]
PublicKey =  客戶端192.168.3.3公鑰
AllowedIPs = 192.168.3.3/32

4.2 IPv6 支援 / IPv6 分配#

載入中......
此文章數據所有權由區塊鏈加密技術和智能合約保障僅歸創作者所有。