🚀WireGuard 企业级部署(正式用)
🎯 目标
- 企业 A ↔ 企业 C 稳定互通
- 高性能(500M~1Gbps)
- 不依赖 P2P
🧠 生产拓扑
A(企业) ──→ B(WireGuard Server) ←── C(企业)
🔧 服务器 B 部署(核心)
1️⃣ 安装 WireGuard
Rocky / CentOS / openEuler:
dnf install -y epel-release
dnf install -y wireguard-tools
dnf install -y wireguard-tools
2️⃣ 生成密钥
wg genkey | tee server.key | wg pubkey > server.pub
👉 Hub-and-Spoke(中心转发)
🔧 B(服务器)配置
/etc/wireguard/wg0.conf
[Interface]
Address = 10.0.0.1/24
ListenPort = 51820
PrivateKey = <B私钥>
MTU = 1420PostUp = sysctl -w net.ipv4.ip_forward=1
PostUp = iptables -A FORWARD -i wg0 -j ACCEPT
PostUp = iptables -A FORWARD -o wg0 -j ACCEPT
PostDown = iptables -D FORWARD -i wg0 -j ACCEPT
PostDown = iptables -D FORWARD -o wg0 -j ACCEPT# A
Address = 10.0.0.1/24
ListenPort = 51820
PrivateKey = <B私钥>
MTU = 1420PostUp = sysctl -w net.ipv4.ip_forward=1
PostUp = iptables -A FORWARD -i wg0 -j ACCEPT
PostUp = iptables -A FORWARD -o wg0 -j ACCEPT
PostDown = iptables -D FORWARD -i wg0 -j ACCEPT
PostDown = iptables -D FORWARD -o wg0 -j ACCEPT# A
# A
[Peer]
PublicKey = <A公钥>
AllowedIPs = 10.0.0.2/32, 192.168.10.0/24# C
[Peer]
PublicKey = <C公钥>
AllowedIPs = 10.0.0.3/32, 192.168.20.0/24
PublicKey = <A公钥>
AllowedIPs = 10.0.0.2/32, 192.168.10.0/24# C
[Peer]
PublicKey = <C公钥>
AllowedIPs = 10.0.0.3/32, 192.168.20.0/24
1️⃣ 生成密钥
wg genkey | tee a.key | wg pubkey > a.pub
🔧 A(企业A)
[Interface]
Address = 10.0.0.2/24
PrivateKey = <A私钥>
MTU = 1420[Peer]
PublicKey = <B公钥>
Endpoint = B_IP:51820
AllowedIPs = 192.168.20.0/24, 10.0.0.0/24
PersistentKeepalive = 25
Address = 10.0.0.2/24
PrivateKey = <A私钥>
MTU = 1420[Peer]
PublicKey = <B公钥>
Endpoint = B_IP:51820
AllowedIPs = 192.168.20.0/24, 10.0.0.0/24
PersistentKeepalive = 25
1️⃣ 生成密钥
wg genkey | tee c.key | wg pubkey > c.pub
🔧 C(企业C)
[Interface]
Address = 10.0.0.3/24
PrivateKey = <C私钥>
MTU = 1420[Peer]
PublicKey = <B公钥>
Endpoint = B_IP:51820
AllowedIPs = 192.168.10.0/24, 10.0.0.0/24
PersistentKeepalive = 25
Address = 10.0.0.3/24
PrivateKey = <C私钥>
MTU = 1420[Peer]
PublicKey = <B公钥>
Endpoint = B_IP:51820
AllowedIPs = 192.168.10.0/24, 10.0.0.0/24
PersistentKeepalive = 25
🔥 企业级关键点(别忽略)
✔ 1️⃣ 内网路由打通
在 A / C 内网网关上:
ip route add 对端网段 via WG网关
👉 否则只有“单机通”,不是“整网通”
✔ 2️⃣ UDP 必须放行
企业出口至少允许:
UDP 出站(51820)
✔ 3️⃣ 性能调优
sysctl -w net.core.rmem_max=2500000
sysctl -w net.core.wmem_max=2500000
sysctl -w net.core.wmem_max=2500000
✔ 4️⃣ 多核利用(关键优势)
WireGuard 自动多核,比 n2n 强太多
📊 预期效果
| 架构 | 带宽 |
|---|---|
| n2n(你现在) | ~100M |
| WireGuard | 500M~1Gbps |
yxbinghe
✅ WireGuard + 多网段(像真实企业网络)
✅ 动态路由(FRR/BGP)
✅ 零信任访问控制(ACL)
yxbinghe
https://hub.docker.com/r/linuxserver/wireguard/
docker run -d
–name=wireguard
–cap-add=NET_ADMIN
–cap-add=SYS_MODULE `#optional`
-e PUID=1000
-e PGID=1000
-e TZ=Etc/UTC
-e SERVERURL=wireguard.domain.com `#optional`
-e SERVERPORT=51820 `#optional`
-e PEERS=1 `#optional`
-e PEERDNS=auto `#optional`
-e INTERNAL_SUBNET=10.13.13.0 `#optional`
-e ALLOWEDIPS=0.0.0.0/0 `#optional`
-e PERSISTENTKEEPALIVE_PEERS= `#optional`
-e LOG_CONFS=true `#optional`
-p 51820:51820/udp
-v /path/to/wireguard/config:/config
-v /lib/modules:/lib/modules `#optional`
–sysctl=”net.ipv4.conf.all.src_valid_mark=1″
–restart unless-stopped
lscr.io/linuxserver/wireguard:latest
yxbinghe
supernode与edge:啟動參數中必須包含 -E(允許接收多播),這是 FRR 運行 OSPF 的前提。
yxbinghe
第一步:準備 community.list 文件在 Supernode 的配置目錄(通常是 /etc/n2n/ 或你掛載的 Docker 目錄)中創建或修改 community.list:
# 格式:[Community名稱] [網段/掩碼]
group1 10.100.10.0/25
group2 10.100.10.128/25
注:這會強制要求名為 group1 的節點從前半段取地址,group2 從後半段取地址。
第二步:啟動 Supernode 並加載配置
啟動 Supernode 時,需要使用 -c 參數指定該列表文件:bashsupernode -l [端口] -c /path/to/community.list
請謹慎使用程式碼。
第三步:Edge 端獲取地址在 Edge 端啟動時,不要指定 -a 參數(即不手動設固定 IP),只需指定對應的 Community 名稱:Edge 1: edge -c group1 -l [Supernode_IP:Port] … -> 會自動獲得 10.100.10.x (1-126 之間)。Edge 2: edge -c group2 -l [Supernode_IP:Port] … -> 會自動獲得 10.100.10.y (129-254 之間)。3. 注意事項路由互通:雖然分配在不同的 /25 段,但由於它們屬於不同的 Community,n2n 默認情況下不同 Community 是不互通的。跨組訪問:如果你希望這兩個組能互相訪問,你仍然需要依靠你之前配置的 FRR。在群暉上運行一個同時加入 group1 和 group2 的 Edge,並在 FRR 中將這兩個網段宣告出去。掩碼一致性:建議在 community.list 中明確定義掩碼,否則 n2n 可能會默認分配 /24 導致地址段重疊。
yxbinghe@yxbinghe
使用 n2n v3 內置的本地 IP 管理 (推薦)如果你使用的是 n2n v3 版本,雖然它不是標準的 DHCP 協議,但可以透過 Supernode 配合 Edge 參數實現類似效果。
Supernode 端:啟動時指定一個 IP 池網段。
supernode -p 7777 -M -c /path/to/community.list
在 community.list 文件中定義群組對應的網段,例如:mygroup 10.100.10.0/24Edge 端:啟動時不手動指定 -a [IP],而是使用 -a dhcp:0.0.0.0。
edge -c mygroup -k mypass -l x.x.x.x:7777 -a dhcp:0.0.0.0
請謹慎使用程式碼。這樣 Edge 會從 Supernode 定義的池中自動領取一個唯一的 10.100.10.x 地址。