自建子网:用 Headscale 替代商业 VPN,从 SOCKS 到 WireGuard
你的 VPS 是 SOCKS 代理?没错,我也是这么过来的。
SSH -D 1080 hk-vps,浏览器设代理,够用了。但你知道这个模式的问题——它是一个单点穿透,不是网络。你的笔记本和 VPS 之间只有一条隧道,你的手机连不上,你的其他服务器连不上,你想让两台机器直接互访?不行。
WireGuard 可以解决这个问题。但 WireGuard 是点到点的,你要组 mesh 全网状,得自己维护配置、密钥、IP 分配。每个节点加进来,你得改所有其他节点的配置。
这就是为什么 Tailscale 存在——它在 WireGuard 上加了一层控制平面,自动协调密钥交换、IP 分配、NAT 穿透。
但 Tailscale 的官方控制服务器是闭源的。你用别人的服务器管理你的网络,等于把你的网络拓扑交给第三方。
Headscale 就是 Tailscale 控制服务器的开源替代。
你自建一个控制服务器(在你已有的 HK VPS 上),所有设备安装 Tailscale 客户端指向你的服务器,然后它们自动组成一个私有子网。你在郑州用笔记本直连 HK VPS,在手机上 SSH 回家里的机器,在任何地方访问你 VPS 上的服务——都是直连 WireGuard 加密隧道,不经过任何第三方。
下面一步步来。
SOCKS vs WireGuard vs Tailscale vs Headscale
先厘清概念。
| 方案 | 架构 | 控制权 | 加密 | 穿透能力 | 客户端复杂度 |
|---|---|---|---|---|---|
| SSH SOCKS (-D) | 单点代理 | 全在你这 | SSH隧道 | 弱(只代理 TCP) | 零(一条命令) |
| WireGuard (手动) | 点对点 | 全在你这 | WireGuard | 强(UDP+Tunnel) | 高(每台配密钥) |
| Tailscale (官方) | Mesh | 交给 Tailscale | WireGuard | 强(DERP/TURN) | 低(客户端一键) |
| Headscale | Mesh | 自建控制服务器 | WireGuard | 强(DERP/TURN) | 低(客户端指向你的服务器) |
SOCKS 最轻,但功能最弱。Tailscale 交互最好但控制权不在你。Headscale 是中间态——你拥有控制平面,客户端体验和 Tailscale 一样流畅。
适合谁
你应该用 Headscale 如果:
- 你有 2 台以上需要互联的机器(VPS / 笔记本 / 手机 / 树莓派)
- 你不信任第三方控制服务器管理你的网络拓扑
- 你想享受 Tailscale 的流畅体验但自己掌握数据
- 你有至少一台公网可达的 VPS(你的 HK 机器就行)
你不应该用 Headscale 如果:
- 你只有一台机器 + 一台笔记本,SOCKS 够用
- 你需要 Tailscale 的 Funnel/Funnel ACL 等商业特性(Headscale 只实现核心控制平面)
搭建 Headscale
环境
- 服务器:HK VPS, Ubuntu 24.04, 公网 IP(假设你 SSH 配置了 hk107)
- 域名(可选):
headscale.yourdomain.com指向 VPS(方便证书,非必须) - 客户端:Windows 11(你的笔记本)
安装 Headscale 服务端
Headscale v0.29.1(2026-06-18 发布)支持.deb 包直接安装。
# SSH 到 VPS
ssh hk107
# 下载最新 deb 包
wget https://github.com/juanfont/headscale/releases/download/v0.29.1/headscale_0.29.1_linux_amd64.deb
# 安装
sudo dpkg -i headscale_0.29.1_linux_amd64.deb
# 验证
headscale version
# 输出: v0.29.1
配置
配置文件在 /etc/headscale/config.yaml。
关键修改项:
# 服务器 URL:客户端连接你的服务器地址
# 如果有域名:https://headscale.yourdomain.com:8080
# 如果只有 IP:https://<你的VPS公网IP>:8080
server_url: https://<你的VPS公网IP>:8080
# 侦听地址
listen_addr: 0.0.0.0:8080
# 私有子网网段(默认是 100.64.0.0/10,Tailscale 的 CGNAT 范围)
ip_prefixes:
- 100.64.0.0/10
# DERP 服务器(中继服务器,当两个节点无法直连时使用)
# 默认配置已经包含了 Tailscale 的公共 DERP 服务器
derp:
server:
enabled: false # 不自行搭建 DERP 中继,使用公共的
urls:
- https://controlplane.tailscale.com/derpmap/default
然后启动服务:
sudo systemctl enable headscale
sudo systemctl start headscale
sudo systemctl status headscale
防火墙放行
sudo ufw allow 8080/tcp comment 'headscale'
# WireGuard 需要 UDP 端口,由客户端动态分配,不用手动开放
注册第一个用户(namespace)
Headscale 用 namespace(命名空间)隔离网络。个人使用一个就够了:
sudo headscale users create learsu
注册节点
每个节点需要一条预认证密钥来加入网络。生成密钥:
# 生成一个可重复使用的密钥
sudo headscale preauthkeys create -u learsu -e 72h --reusable=false
# 输出: abcd1234...
保存好这个密钥。每个新节点加入时都需要输入。
Windows 客户端配置
你的笔记本(Windows 11)需要安装 Tailscale 客户端并指向你的 Headscale 服务器。
- 下载 Tailscale Windows 客户端:https://tailscale.com/download/windows
- 安装后不要直接登录——先设置自定义控制服务器
- 在系统托盘右键 Tailscale → 选择 Custom Login Server
- 输入你的 Headscale 服务器 URL:
https://<你的VPS公网IP>:8080 - 然后点 Login,浏览器会打开一个页面,显示
/register?key=开头的 URL
在 Headscale 服务器上注册这个节点:
# 在 VPS 上执行
# 等待节点出现(它会在 preauth 模式下等待)
sudo headscale nodes register --user learsu --key <上面生成的preauthkey>
或者更简单的方法——用 preauthkey 直接登录:
# 在 Windows 上,管理员权限运行 PowerShell
tailscale up --login-server https://<你的VPS公网IP>:8080 --authkey <preauthkey>
注册成功后,你的笔记本和 VPS 就在同一个子网里了。
在 VPS 上也安装 Tailscale 客户端
注意:Headscale 只是控制服务器。你的 VPS 本身也需要运行 Tailscale 客户端才能进入子网。
# 在 VPS 上安装 Tailscale 客户端
curl -fsSL https://tailscale.com/install.sh | sh
# 指向你的 Headscale 并加入
sudo tailscale up --login-server https://localhost:8080 --authkey <preauthkey>
(用 localhost 是因为 Headscale 和 Tailscale 在同一台机器上)
验证
现在,你在笔记本上应该可以 ping 通 VPS 的 Tailscale 子网 IP:
# 在 Windows PowerShell 上
# 先查 VPS 的 Tailscale IP
ssh hk107 "tailscale ip -4"
# 输出类似: 100.64.0.1
# 然后 ping 它
ping 100.64.0.1
# 应该 Pong
查看所有已注册的节点:
sudo headscale nodes list
# ID | Hostname | IPs | ...
实际能干嘛
组完子网后,以前的 SSH -D SOCKS 可以退休了。你现在可以:
1. 直接 SSH 不用暴露端口
你的 VPS 可以关闭 SSH 公网端口(47504),只允许 Tailscale 子网内的连接。没有公网扫描器能碰到你的 SSH,攻击面大幅减少。
# 从笔记本 SSH 到 VPS 的 Tailscale IP
ssh user@100.64.0.1
2. 本地访问 VPS 上的服务
VPS 上跑的任何 web 服务(博客、API、数据库),你可以在浏览器里直接访问 http://100.64.0.1:PORT。不需要公网端口映射,不需要 Nginx 反向代理。
3. 手机也能接入
手机安装 Tailscale 客户端(iOS/Android App Store),指向你的 Headscale 服务器,自动进入子网。你在外面用手机连回家里的机器、连 VPS 数据库,和在家一样。
4. 多台 VPS 组成私有网络
如果你以后再加一台机器(比如阿里云国内轻量),在上面也安装 Tailscale 客户端指向同一个 Headscale,三台机器自动组网,相互直连。
关于 DERP 中继
不是所有节点都能直连。比如你的笔记本在公司防火墙后面,VPS 在公网,手机在 4G 网络——它们之间的 NAT 类型可能不允许 P2P 穿透。
这时候需要 DERP 中继服务器。Tailscale 官方提供了公共 DERP 服务器,Headscale 默认配置引用了它们。你的节点无法直连时,数据流量会通过公共 DERP 中继。
代价:流量经过第三方服务器(但仍是 WireGuard 加密的,Tailscale 看不到你的内容)。
如果你不信任公共 DERP,可以自建 DERP 服务器在你的 VPS 上。但大多数个人场景下,直连就够用了——你在家连 VPS 通常是 NAT 穿透友好的场景。
SOCKS 模式还留着吗
留。SOCKS 仍然有它的位置:
- 一次性使用:临时在别人电脑上上网,装 Tailscale 客户端不现实,一条 SSH -D 解决
- 浏览器代理:如果你的浏览器配置了 SOCKS v5 代理(SwitchyOmega),指向本地 Tailscale IP 的 SOCKS 端口
- 备用通道:Tailscale 挂了?你的 SSH 隧道还在
所以不是替代,是升维——在 SOCKS 单点穿透之上,加一层 mesh 子网。
Headscale v0.29.1 是目前最新稳定版。Tailscale 客户端版本需与 Headscale 兼容,建议同时更新。配置你的服务器 URL 时注意 HTTPS 证书——自签证书需要额外配置,第一次建议用 HTTP 调试通后再上证书。
Build Your Own Mesh Network: Replace Commercial VPNs with Headscale
Your VPS runs on an SSH SOCKS proxy (-D 1080 hk-vps). It works — but it's a single tunnel, not a network. Your phone can't reach your VPS. Your other servers can't reach each other. You want point-to-point connectivity between any two machines? Not with SOCKS.
WireGuard solves the encryption part, but WireGuard is point-to-point by design. Every new node means updating config on every existing node. No automatic key exchange. No IP allocation. No NAT traversal.
That's why Tailscale exists — it wraps a control plane around WireGuard: automatic key exchange, IP assignment, NAT punching. But Tailscale's official control server is closed-source. You're handing your network topology to a third party.
Headscale is the open-source alternative to Tailscale's control server.
You run the control server on your own VPS. All your devices install the Tailscale client pointed at your server. They automatically form a private mesh subnet. Your laptop in Zhengzhou connects to your HK VPS directly over WireGuard. Your phone SSHs into your machines at home. All encrypted, all peer-to-peer, no third party involved.
Here's the step-by-step.
SOCKS vs WireGuard vs Tailscale vs Headscale
| Solution | Architecture | Control | Encryption | Penetration | Complexity |
|---|---|---|---|---|---|
| SSH SOCKS (-D) | Single proxy | You | SSH tunnel | Weak (TCP only) | Zero (one command) |
| WireGuard (manual) | Point-to-point | You | WireGuard | Strong (UDP+Tunnel) | High (per-machine config) |
| Tailscale (official) | Mesh | Tailscale Inc. | WireGuard | Strong (DERP/TURN) | Low (one-click client) |
| Headscale | Mesh | Self-hosted | WireGuard | Strong (DERP/TURN) | Low (client→your server) |
SOCKS is the lightest but weakest. Tailscale is the most polished but you don't control the control plane. Headscale is the sweet spot — you own the server, you get the Tailscale client experience.
Installation
Server (HK VPS, Ubuntu 24.04)
# Download the .deb
wget https://github.com/juanfont/headscale/releases/download/v0.29.1/headscale_0.29.1_linux_amd64.deb
sudo dpkg -i headscale_0.29.1_linux_amd64.deb
# Verify
headscale version
Configuration
Edit /etc/headscale/config.yaml:
server_url: https://<YOUR_VPS_PUBLIC_IP>:8080
listen_addr: 0.0.0.0:8080
ip_prefixes:
- 100.64.0.0/10
Start the service:
sudo systemctl enable headscale
sudo systemctl start headscale
Create a user namespace and a pre-auth key:
sudo headscale users create learsu
sudo headscale preauthkeys create -u learsu -e 72h --reusable=false
Windows Client
- Install Tailscale for Windows
- Don't log in yet — right-click the tray icon, select Custom Login Server
- Enter
https://<YOUR_VPS_IP>:8080 - In PowerShell (admin):
tailscale up --login-server https://<YOUR_VPS_IP>:8080 --authkey <KEY>
Also install client on the VPS itself
curl -fsSL https://tailscale.com/install.sh | sh
sudo tailscale up --login-server https://localhost:8080 --authkey <KEY>
Verification
# On the VPS
tailscale ip -4
# Output: 100.64.0.1
# On Windows, ping that IP
ping 100.64.0.1
# Should respond
What You Can Now Do
1. SSH without exposing ports. Close port 47504 on the VPS. Allow only Tailscale subnet connections. No more port scanners finding your SSH.
2. Access VPS services locally. Any web service on your VPS is accessible at http://100.64.0.1:PORT — no Nginx reverse proxy, no port forwarding.
3. Phone access. Install the Tailscale mobile app, point it at your Headscale, and you're on the subnet from anywhere.
4. Multi-VPS mesh. Add a second VPS (say Alibaba Cloud in mainland China) with the same Headscale server. Three machines auto-mesh.
DERP Relays
Not every pair of nodes can punch through NAT. When they can't, traffic goes through DERP relays. Headscale defaults to Tailscale's public DERP servers — traffic is still WireGuard encrypted, but the relay endpoint is a third party. For most home-to-VPS setups, direct connections work fine.
Keep SOCKS Too
SOCKS still has its uses: throwaway machines, browser proxy chains (SwitchyOmega → SOCKS over Tailscale IP), emergency fallback. The upgrade isn't replacement — it's adding a mesh layer on top of your existing tunnel.
Headscale v0.29.1 (Jun 2026) is the latest stable. Keep client versions aligned with the server. For first-time setup, test with HTTP before adding TLS certificates.