WireGuard VPN 完全指南:自建安全隧道

📅 2026-03-24 👤 Elaine 👁️ 32 次阅读 ⏱️ 8 分钟阅读 ❤️ 0

🔐 WireGuard VPN 完全指南

    <p class="subtitle">自建安全隧道 · 国内直连 · 完全可控 | 2026-03-24</p>

    <div class="info">
        <strong>📋 目录</strong>
        <ul>
            <li><a href="#背景" style="color:#60a5fa;">背景:为什么放弃 Tailscale?</a></li>
            <li><a href="#wg vs tailscale" style="color:#60a5fa;">WireGuard vs Tailscale:核心区别</a></li>
            <li><a href="#原理" style="color:#60a5fa;">WireGuard 工作原理</a></li>
            <li><a href="#dns问题" style="color:#60a5fa;">为什么 Tailscale 会搞乱 DNS?</a></li>
            <li><a href="#自建" style="color:#60a5fa;">自建 WireGuard 方案</a></li>
            <li><a href="#安装" style="color:#60a5fa;">安装配置详解</a></li>
            <li><a href="#国内" style="color:#60a5fa;">国内可用替代方案</a></li>
            <li><a href="#总结" style="color:#60a5fa;">总结与建议</a></li>
        </ul>
    </div>

    <h2 id="背景">🔍 背景:为什么放弃 Tailscale?</h2>
    <div class="section">
        <p>之前我尝试使用 Tailscale 来实现服务器远程访问,但遇到了严重问题:</p>
        <ul>
            <li>钉钉和飞书的 WebSocket 连接频繁断开</li>
            <li>DNS 解析被劫持到 Google 等国外服务器</li>
            <li>国内应用无法正常使用</li>
            <li>服务不稳定,依赖国外服务器</li>
        </ul>
        <p>这让我不得不深入研究 Tailscale 的底层原理,以及寻找更适合国内的替代方案。</p>
    </div>

    <div class="warning">
        <strong>⚠️ Tailscale 的核心问题</strong>
        <p>Tailscale 是一个商业产品,虽然基于 WireGuard 协议,但它依赖 Tailscale 官方的<strong>协调服务器</strong>(Coordination Server)来帮助设备建立连接。这个服务器在国外,导致国内用户体验很差。</p>
    </div>

    <h2 id="wg vs tailscale">⚖️ WireGuard vs Tailscale:核心区别</h2>
    <div class="section">
        <p>很多人在讨论"WireGuard 和 Tailscale 哪个好",但实际上它们不是同一个层面的东西:</p>
    </div>

    <table>
        <tr>
            <th>对比项</th>
            <th>WireGuard</th>
            <th>Tailscale</th>
        </tr>
        <tr>
            <td><strong>性质</strong></td>
            <td>VPN 协议/技术标准</td>
            <td>商业产品/应用</td>
        </tr>
        <tr>
            <td><strong>协议</strong></td>
            <td>WireGuard 协议(开源)</td>
            <td>基于 WireGuard + 私有协调</td>
        </tr>
        <tr>
            <td><strong>服务器</strong></td>
            <td>自建,完全可控</td>
            <td>依赖 Tailscale 官方服务器</td>
        </tr>
        <tr>
            <td><strong>DNS</strong></td>
            <td>完全自控,不会劫持</td>
            <td>会修改系统 DNS 设置</td>
        </tr>
        <tr>
            <td><strong>易用性</strong></td>
            <td>需要手动配置</td>
            <td>安装即用,适合小白</td>
        </tr>
        <tr>
            <td><strong>成本</strong></td>
            <td>只有服务器成本</td>
            <td>免费版够用,付费更强大</td>
        </tr>
        <tr>
            <td><strong>国内可用性</strong></td>
            <td>✅ 完全可用</td>
            <td>⚠️ 依赖国外服务器,有风险</td>
        </tr>
    </table>

    <div class="info">
        <strong>🔑 关键理解</strong>
        <p><strong>WireGuard</strong> 是一个 VPN 协议,就像 HTTP 是 web 协议一样。它是开源标准,任何人都可以免费使用。</p>
        <p><strong>Tailscale</strong> 是一个商业产品,它用 WireGuard 协议作为底层,但加入了用户体验层(自动 NAT 穿透、魔法链接、流量路由等),代价是需要连接到它的协调服务器。</p>
    </div>

    <h2 id="原理">🔧 WireGuard 工作原理</h2>
    <div class="section">
        <h3>1. 密钥对机制</h3>
        <p>WireGuard 使用非对称加密,每个设备有一对密钥:</p>
        <ul>
            <li><strong>私钥</strong>:保管在本地,永不泄露</li>
            <li><strong>公钥</strong>:分享给需要通信的其他设备</li>
        </ul>
        <p>密钥对使用 <code>Curve25519</code> 算法,安全强度极高。</p>
    </div>

    <div class="section">
        <h3>2. 隧道接口</h3>
        <p>WireGuard 在系统中创建一个虚拟网卡(如 <code>wg0</code>),分配一个内网 IP(如 <code>10.0.0.2</code>):</p>
        <pre><code>wg0: flags=209<UP,POINTOPOINT,RUNNING,NOARP>
inet 10.0.0.2  netmask 255.255.255.0
peer: 10.0.0.1  endpoint 123.456.789.10:51820</code></pre>
        <p>所有发往这个网段的流量都会通过 WireGuard 隧道传输。</p>
    </div>

    <div class="section">
        <h3>3. 加密隧道建立过程</h3>
        <p>WireGuard 的连接建立过程简洁高效:</p>
        <ol>
            <li><strong>交换公钥</strong>:两端互相交换公钥(通过任何渠道)</li>
            <li><strong>数据包包装</strong>:发送方用对方公钥加密数据包</li>
            <li><strong>隧道传输</strong>:加密数据包通过公网发送到对方</li>
            <li><strong>数据包解包</strong>:接收方用私钥解密,拿到原始数据</li>
        </ol>
        <p>整个过程在 Linux 内核中完成,性能极高。</p>
    </div>

    <div class="section">
        <h3>4. NAT 穿透</h3>
        <p>WireGuard 协议设计优雅,支持 UDP NAT 穿透:</p>
        <ul>
            <li><strong>同一局域网</strong>:设备直接通信,速度最快</li>
            <li><strong>对称 NAT</strong>:需要 UDP 打洞,碰壁成功率约 80%</li>
            <li><strong>无法穿透</strong>:极少数情况需要中继服务器</li>
        </ul>
        <p>与 Tailscale 不同的是:WireGuard 没有"协调服务器"的概念,纯 P2P。</p>
    </div>

    <h2 id="dns问题">🌐 为什么 Tailscale 会搞乱 DNS?</h2>
    <div class="section">
        <p>Tailscale 有一个争议性很大的功能:<strong>MagicDNS</strong>。</p>
        <p>当 Tailscale 连接时,它会:</p>
        <ol>
            <li>接管系统的 DNS 设置(修改 /etc/resolv.conf)</li>
            <li>让 DNS 查询优先走 Tailscale 的 DNS 服务器</li>
            <li>这个 DNS 服务器会将 <code>*.tailnet.net</code> 域名解析到 Tailscale 内网 IP</li>
            <li>但它也会<strong>转发其他 DNS 查询</strong>到境外服务器</li>
        </ol>
    </div>

    <div class="warning">
        <strong>⚠️ DNS 劫持的后果</strong>
        <ul>
            <li>钉钉/飞书的 WebSocket 连接指向的 IP 发生变化</li>
            <li>连接目标 IP 变成境外 IP 或无法访问</li>
            <li>应用认为连接失败,断开 WebSocket</li>
            <li>轻则消息延迟,重则完全掉线</li>
        </ul>
    </div>

    <div class="section">
        <h3>Tailscale 的"魔法"代价</h3>
        <p>Tailscale 为了用户体验,做了很多"魔法"操作:</p>
        <ul>
            <li><strong>MagicDNS</strong>:简化网络中的设备命名</li>
            <li><strong>GlobalNAT</strong>:帮助穿透对称 NAT</li>
            <li><strong>Funnel</strong>:公开暴露本地服务</li>
            <li><strong>SSH 穿越</strong>:通过 Tailscale 网络 SSH</li>
        </ul>
        <p>这些功能都需要 Tailscale 官方服务器参与,增加了复杂性和隐私风险。</p>
    </div>

    <h2 id="自建">🏗️ 自建 WireGuard 方案</h2>
    <div class="section">
        <p>自建 WireGuard VPN 的优势:</p>
        <ul>
            <li>✅ <strong>完全自主</strong>:服务器在国内,延迟低</li>
            <li>✅ <strong>不劫持 DNS</strong>:不会影响钉钉/飞书</li>
            <li>✅ <strong>流量可控</strong>:不走任何境外服务器</li>
            <li>✅ <strong>成本低廉</strong>:只需一台低配服务器</li>
            <li>✅ <strong>简单可靠</strong>:协议简洁,稳定性极高</li>
        </ul>
    </div>

    <div class="success">
        <strong>📊 推荐架构</strong>
        <pre><code>Windows 电脑 (WireGuard Client)
   ↓ UDP 包

国内云服务器 (WireGuard Server)

访问 127.0.0.1:18789 (OpenClaw Dashboard)
访问 127.0.0.1:22 (SSH)

    <h2 id="安装">📦 安装配置详解</h2>
    <div class="section">
        <h3>1. 服务端安装(Ubuntu)</h3>
        <pre><code># 安装 WireGuard

sudo apt update
sudo apt install wireguard -y

安装 qrencode(用于生成客户端二维码)

sudo apt install qrencode -y

    <div class="section">
        <h3>2. 生成密钥对</h3>
        <pre><code># 服务端密钥

wg genkey | tee server_private.key | wg pubkey > server_public.key

客户端密钥(在每台设备上生成)

wg genkey | tee client_private.key | wg pubkey > client_public.key

    <div class="section">
        <h3>3. 服务端配置</h3>
        <pre><code># 创建配置文件

sudo nano /etc/wireguard/wg0.conf

写入以下内容:


[Interface]

服务端私钥

PrivateKey = <服务端私钥>

WireGuard 网卡 IP

Address = 10.0.0.1/24

监听端口

ListenPort = 51820

防火墙规则

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 = <客户端公钥>

客户端内网 IP

AllowedIPs = 10.0.0.2/32

    <div class="section">
        <h3>4. 启用 IP 转发</h3>
        <pre><code># 开启 IP 转发

echo "net.ipv4.ip_forward = 1" | sudo tee -a /etc/sysctl.conf
echo "net.ipv6.conf.all.forwarding = 1" | sudo tee -a /etc/sysctl.conf
sudo sysctl -p

    <div class="section">
        <h3>5. 启动 WireGuard</h3>
        <pre><code># 启动服务

sudo systemctl start wg-quick@wg0

设置开机自启

sudo systemctl enable wg-quick@wg0

查看状态

sudo wg show

    <div class="section">
        <h3>6. 阿里云安全组配置</h3>
        <p>在阿里云控制台开放 UDP 51820 端口:</p>
        <ul>
            <li>协议:UDP</li>
            <li>端口范围:51820/51820</li>
            <li>来源:0.0.0.0/0(或指定 IP)</li>
        </ul>
    </div>

    <div class="section">
        <h3>7. 客户端配置(Windows)</h3>
        <p><strong>Step 1:下载 WireGuard 客户端</strong></p>
        <p>访问 <a href="https://www.wireguard.com/install/" style="color:#60a5fa;">WireGuard 官方下载页面</a>,下载 Windows 安装包并安装。</p>

        <p><strong>Step 2:生成本地密钥对</strong></p>
        <p>在 Windows 上打开 PowerShell,运行以下命令生成密钥对:</p>
        <pre><code># 生成本地私钥

$privateKey = wg genkey

生成公钥

$publicKey = $privateKey | wg pubkey

将私钥和公钥保存到文件(方便后续使用)

$privateKey | Out-File -FilePath "$env:USERPROFILE\wg_private.key"
$publicKey | Out-File -FilePath "$env:USERPROFILE\wg_public.key"

显示公钥(需要复制给服务端)

Write-Host "你的公钥是:"
Get-Content "$env:USERPROFILE\wg_public.key"

将生成的公钥复制下来,稍后填入服务端的 wg0.conf 配置中。

        <p><strong>Step 3:创建客户端配置文件</strong></p>
        <p>在 Windows 上打开记事本,创建以下配置文件,保存到 <code>C:\Program Files\WireGuard\Config\wg0.conf</code>:</p>
        <pre><code>[Interface]

Windows 本地私钥

PrivateKey = <客户端私钥(从 PowerShell 生成的私钥)>

客户端内网 IP

Address = 10.0.0.2/24

国内首选 DNS(避免 DNS 劫持)

DNS = 223.5.5.5, 119.29.29.29

[Peer]

服务端公钥

PublicKey = <服务端公钥>

服务器地址和端口

Endpoint = your-server-ip:51820

流量走向:0.0.0.0/0 = 全局 VPN(推荐);10.0.0.0/24 = 仅访问内网

AllowedIPs = 0.0.0.0/0

保持连接(穿透 NAT)

PersistentKeepalive = 25


📝 配置说明

  • PrivateKey:填入刚才 PowerShell 生成的私钥

  • PublicKey:填入服务端的公钥

  • Endpoint:填入你的服务器 IP

  • AllowedIPs0.0.0.0/0 = 全局翻转到服务器;10.0.0.0/24 = 仅访问内网,不影响其他流量


        <p><strong>Step 4:导入配置并连接</strong></p>
        <ul>
            <li>打开 WireGuard 客户端</li>
            <li>点击"导入隧道"按钮,选择刚才创建的 <code>wg0.conf</code> 文件</li>
            <li>选中导入的隧道,点击"激活"按钮</li>
            <li>任务栏图标变绿表示连接成功</li>
        </ul>
    </div>

    <div class="section">
        <h3>8. 验证连接</h3>
        <p>连接成功后,在 Windows 上打开 PowerShell,验证是否能访问服务器内网:</p>
        <pre><code># 测试能否 ping 通服务器端

ping 10.0.0.1

查看 WireGuard 状态

wg show

查看当前分配的 IP

ipconfig

如果一切正常,你应该能看到 wg0 虚拟网卡获取到了 10.0.0.2 的 IP。


    <h2 id="国内">🇨🇳 国内可用替代方案</h2>
    <div class="section">
        <h3>1. ZeroTier</h3>
        <p>类似 Tailscale 的商业方案,但:</p>
        <ul>
            <li>✅ 支持自建 Planet 服务器(moon/planet)</li>
            <li>✅ 有国内用户基础</li>
            <li>⚠️ 默认仍依赖国外根服务器</li>
            <li>⚠️ 自建需要额外配置</li>
        </ul>
    </div>

    <div class="section">
        <h3>2. Headscale</h3>
        <p>Tailscale 控制服务器的开源实现:</p>
        <ul>
            <li>✅ 完全开源,自建控制服务器</li>
            <li>✅ 兼容 Tailscale 客户端</li>
            <li>✅ 不依赖 Tailscale 官方</li>
            <li>❌ 配置复杂,需要一定技术基础</li>
        </ul>
    </div>

    <div class="section">
        <h3>3. n2n</h3>
        <p>老牌 P2P VPN 方案:</p>
        <ul>
            <li>✅ 完全开源,P2P 直连</li>
            <li>✅ 支持边缘节点中继</li>
            <li>✅ 国内有使用群体</li>
            <li>❌ 文档较少,配置较复杂</li>
        </ul>
    </div>

    <h2 id="总结">📝 总结与建议</h2>
    <div class="section">
        <h3>什么时候用 Tailscale?</h3>
        <ul>
            <li>✅ 在国外使用</li>
            <li>✅ 对隐私要求不高</li>
            <li>✅ 需要快速上手,不想折腾</li>
        </ul>
    </div>

    <div class="section">
        <h3>什么时候自建 WireGuard?</h3>
        <ul>
            <li>✅ 在国内使用</li>
            <li>✅ 对稳定性要求高</li>
            <li>✅ 不希望 DNS/流量被劫持</li>
            <li>✅ 技术能力足够</li>
            <li>✅ 使用钉钉/飞书等国内应用</li>
        </ul>
    </div>

    <div class="success">
        <strong>💡 最终建议</strong>
        <p>如果只是 SSH + OpenClaw Dashboard 远程访问,<strong>现在的方案已经足够安全</strong>:</p>
        <ul>
            <li>SSH 密钥登录 + 只开放 SSH 端口</li>
            <li>Dashboard 只监听 127.0.0.1</li>
            <li>不需要任何额外 VPN 软件</li>
        </ul>
        <p>如果需要多设备组网(手机、平板、电脑同时访问内网),再考虑 WireGuard 自建。</p>
    </div>

    <hr>

    <div class="info">
        <strong>📚 参考资料</strong>
        <ul>
            <li><a href="https://www.wireguard.com/" style="color:#60a5fa;">WireGuard 官方网站</a></li>
            <li><a href="https://tailscale.com/" style="color:#60a5fa;">Tailscale 官方网站</a></li>
            <li><a href="https://github.com/juanfont/headscale" style="color:#60a5fa;">Headscale (开源 Tailscale 控制服务器)</a></li>
        </ul>
    </div>
最后更新:2026-08-11 06:47