本页目录

Tailscale 操作手册

Tailscale 是基于 WireGuard 的 mesh VPN。核心概念: tailnet (你的私有网络), node (每个设备), DERP (中继服务器, 无法直连时用), MagicDNS (自动 .ts.net 域名), ACL (访问控制)。

状态查看

tailscale status                          # 所有节点: IP/主机名/在线状态/出口节点
tailscale status --json | jq '.'          # 机器可读
tailscale status --self                   # 只看本机

tailscale ip -4                           # 本机 Tailscale IPv4 (100.x.y.z)
tailscale ip -6                           # 本机 Tailscale IPv6

tailscale netcheck                        # 网络连通性: NAT 类型/延迟/DERP 可达性
tailscale ping <hostname>                 # 端到端连通测试 (通过 tailnet)
tailscale ping --c 5 <hostname>           # 5 次

tailscale version                         # 客户端版本
tailscale debug derp-map                  # 当前使用的 DERP 地图
tailscale debug local-creatures           # 本机注册的 MagicDNS 名字
tailscale debug prefs                     # 当前配置 (路由/出口/accept-routes...)

基本操作

# 启停
tailscale up                              # 启动 (用上次的参数)
tailscale down                            # 断开 (不删节点)
tailscale logout                          # 登出并删除此节点

# 常用 up 参数
tailscale up --accept-routes              # 接受其他节点通告的子网路由
tailscale up --advertise-routes=192.168.1.0/24  # 通告子网 (本机做子网网关)
tailscale up --exit-node=<hostname>       # 指定出口节点 (全局流量走它)
tailscale up --exit-node-allow-lan-access # 出口模式仍允许访问本地 LAN
tailscale up --reset                      # 重置所有配置重新 up
tailscale up --operator=lizzbit           # 允许非 root 用户操作

# 切换账号
tailscale login                           # 登录 (新设备或重登录)
tailscale switch <account>                # 切换登录账号

# 重新认证 (key 过期后)
tailscale up --force-reauth

子网路由 (Subnet Router)

# 在作为网关的节点上:
tailscale up --advertise-routes=192.168.31.0/24,10.0.0.0/24

# 在 Admin Console 中批准路由 (一次性):
# Machines → 找到该节点 → Edit route settings → 勾选子网并 Approve

# 在客户端启用:
tailscale up --accept-routes              # 接受所有已批准路由
# 查看当前接受的路由
tailscale debug prefs | grep Route

出口节点 (Exit Node)

# 在出口节点 (通常是 VPS 或有公网出口的主机) 上:
tailscale up --advertise-exit-node

# 在 Admin Console 批准后, 客户端:
tailscale up --exit-node=<hostname>       # 全局流量走出口节点
tailscale up --exit-node=                 # 恢复直连 (空值)

# 临时切换出口节点
tailscale set --exit-node=<hostname>
tailscale set --exit-node=                # 取消

DERP 服务器

# 查看当前 DERP 使用情况
tailscale netcheck                        # 显示各 DERP 延迟 + 当前选中的
tailscale debug derp-map                  # DERP 地图 URL (内置+自定义)

# 自建 DERP (derper)
# 启动: derper -hostname derp.liz6.com -certmode manual -certdir /etc/ssl/
# 端口: 8443 (TLS, 公网暴露), 3478 (STUN, UDP)
# Tailscale Admin Console → Access Controls 添加自定义 DERP 地图:
#   "derpMap": { "Regions": { "901": { ... } } }

# DERP 调试
tailscale debug --derp                     # 实时 DERP 连接状态
# 如果流量走了 DERP (非直连), 延迟会明显变高

ACL 管理 (Admin Console)

# ACL 在 admin console 的 Access Controls 页面管理 (JSON)
# 核心概念:
# - acls: 谁可以访问谁/什么端口
# - groups: 用户分组
# - hosts: 限制节点接受哪些来源
# - derpMap: 自定义 DERP 服务器

# 示例 ACL:
# "acls": [
#   {"action": "accept", "src": ["group:admin"], "dst": ["*:*"]},
#   {"action": "accept", "src": ["group:dev"], "dst": ["tag:server:22,443"]},
# ]

# 测试 ACL 未生效时:
tailscale ping <hostname>                 # 端到端验证

排障

# 1. 检查服务状态
systemctl status tailscaled
journalctl -u tailscaled -f               # 实时日志

# 2. 检查连通性
tailscale ping <hostname>                 # 直连成功 = 低延迟 pong
# 如果显示 "via DERP(...)" 则走了中继, 延迟高

# 3. WireGuard 层面
wg show                                   # Tailscale 的 WireGuard 接口 (通常 tailscale0)
ip addr show tailscale0                   # 100.x.y.z

# 4. 防火墙
# Tailscale 自己打洞会用到 STUN (UDP 3478) 和 DERP (TCP 443/8443)
# 如果直连不通, 检查 UDP 是否被 block

# 5. NAT 问题
tailscale netcheck                        # 看 NAT 类型: easy/random/hard
# "hard NAT" 最难直连, 可能需要 DERP 或手动打洞

# 6. 重置
tailscale down
systemctl restart tailscaled
tailscale up

# 7. MagicDNS 不生效
# 检查是否启用: tailscale debug prefs | grep -i dns
# MagicDNS 基础域名: <hostname>.<tailnet>.ts.net
nslookup <hostname> 100.100.100.100       # 用 Tailscale DNS 解析