✨ Tailscale 零配置组网:从入门到出口节点的硬核笔记

Tailscale 零配置组网

忘了 SSH 裸奔公网、忘了翻墙配置一大堆。Tailscale 用一套 WireGuard 魔法,让世界上任何两台设备像在同一个路由器下那样直接通信。

Tailscale 笔记

Tailscale 是基于 WireGuard 的零配置 Mesh 组网工具。免费计划:个人设备无限台、最多 6 个用户、50 个 tagged 资源。

flowchart LR
    subgraph 你家网络
        NAS[NAS/无Tailscale老设备]
        R[子网路由器<br/>广告 192.168.1.0/24]
    end
    L1[你的笔记本] <-->|WireGuard 隧道| L2[远程服务器]
    L1 <--> R
    L1 -.- NAS
    EX[出口节点 Exit Node] <-->|全局流量转发| L1

1. 安装

1.1 Linux(Debian / Ubuntu)

curl -fsSL https://tailscale.com/install.sh | sh

1.2 其他发行版(RHEL 系)

# CentOS / RHEL / AlmaLinux / Rocky
curl -fsSL https://tailscale.com/install.sh | sh
sudo dnf install tailscale -y

1.3 Docker

docker run -d \
  --name tailscale \
  --hostname $(hostname) \
  --network host \
  --privileged \
  --volume /var/lib/tailscale:/var/lib/tailscale \
  --volume /dev/net/tun:/dev/net/tun \
  --env TS_AUTHKEY=YOUR_AUTH_KEY \
  --env TS_EXTRA_ARGS=--advertise-exit-node \
  --restart unless-stopped \
  tailscale/tailscale:latest
  • 无特权环境下需用用户态网络:tailscaled --tun=userspace-networking(功能受限,无法做 subnet router/exit node)。

1.4 群晖 NAS / Windows / macOS / iOS / Android

  • 群晖:套件中心搜索 Tailscale 直接安装。
  • 其他平台:官网下载客户端,登录账号即可。

1.5 OpenWrt / 路由器

  • 通过 opkg 安装 tailscale,在 LuCI 或命令行配置。

2. 基础使用

2.1 登录并认证

sudo tailscale up
  • 会输出一个 https://login.tailscale.com/... 链接,浏览器登录(Google/GitHub/微软)后设备加入 tailnet。
  • 所有设备用同一账号登录即处于同一虚拟内网(tailnet)。

2.2 查看状态

tailscale status          # 设备列表,每台有 100.x.y.z 地址
tailscale ip              # 本机 Tailscale IP
tailscale version         # 版本号
tailscale netcheck        # 网络连通性诊断

2.3 连接测试

ping 100.x.y.z            # 单台设备
ssh user@100.x.y.z        # SSH 到其他设备

2.4 断开 / 重连 / 更新

sudo tailscale down       # 断开
sudo tailscale up         # 重新连接(应用最新配置)
tailscale update          # 更新(Linux)

2.5 共享文件(Taildrop)

tailscale file cp <file> <target-name>:
tailscale file get <file>

3. 进阶设置

3.1 Tailscale SSH(免管理公钥)

在目标主机上:

sudo tailscale set --ssh
# 或:sudo tailscale up --ssh
  • 授权由 Admin Console 的 ACL 决定,与服务器 authorized_keys 无关。
  • 仅支持连接到 Linux 与 macOS 开源版;连接端任意平台。
  • 仅对 Tailscale 来源的 22 端口生效,不会改 sshd_config
ssh user@<hostname>       # 直接连接,hostname 为 MagicDNS 名称
tailscale ssh user@host   # 走 local daemon 的连接方式

3.2 MagicDNS(主机名代替 IP)

  • 默认开启。登录后直接 ping <hostname> 即可,无需记 IP。
  • 关闭:Admin Console → DNS → MagicDNS(一般不建议关)。

3.3 自定义 hostname

sudo tailscale set --hostname=my-server

3.4 关闭密钥过期(服务器防止莫名失联)

tailscale login --authkey=<key> --… # authkey 本身可设不过期
# 或:Admin Console → Machines → 目标设备 → 关闭 Key expiry

3.5 ACL(访问控制策略)

默认策略允许全部互访。在 Admin Console → Access Controls 编辑,示例:

{
  "acls": [
    // 允许所有人互访
    {"action": "accept", "src": ["*"], "dst": ["*:*"]},
    // 仅允许 home 组访问 dev 服务器的 22 端口
    {"action": "accept", "src": ["group:home"], "dst": ["tag:dev:22"]}
  ],
  "ssh": [
    {
      "action": "accept",
      "src": ["group:home"],
      "dst": ["tag:dev"],
      "users": ["root", "autogroup:nonroot"]
    }
  ]
}
  • autogroup:internet 用于放行出口节点流量:
    {"action": "accept", "src": ["*"], "dst": ["autogroup:internet:*"]}
    
  • 常用关键字:tag:(标签)、group:(用户组)、autogroup:(内置集合)。

3.6 标签(Tags)与别名

  • 服务器类设备建议加标签(如 tag:homebox),与个人账号解耦。
  • tag 无 90 天密钥过期限制,适合无人值守机器。

3.7 自定义 DNS / 搜索域

sudo tailscale set --accept-dns=false          # 不用 MagicDNS
# Admin Console → DNS 可配置内部 DNS 与搜索域

3.8 自建控制面(Headscale)

不想用官方云端,可自架 Headscale(兼容 Tailscale 客户端):

# 服务端(一台有公网 IP 的 VPS 上)
HEADSCALE_PUBLIC_URL=https://ts.example.com  docker run ... headscale
# 客户端指定控制面
sudo tailscale up --login-server=https://ts.example.com

4. 路由

4.1 子网路由器(Subnet Router)

让 tailnet 内设备能访问局域网里没装 Tailscale 的老设备(NAS、打印机、IoT)。

# 1) 在局域网内一台 Linux 主机上开启 IP 转发
echo 'net.ipv4.ip_forward = 1' | sudo tee -a /etc/sysctl.conf
sudo sysctl -p

# 2) 广告整段子网
sudo tailscale up --advertise-routes=192.168.1.0/24
# 多段:--advertise-routes=192.168.1.0/24,10.0.0.0/8

必须在 Admin Console 批准路由:Machines → 该设备 → … → Edit route settings → 勾选并 Approve。

客户端访问(可选):

sudo tailscale up --accept-routes
# 之后即可通过局域网 IP 直接访问子网内设备 ssh user@192.168.1.100

要点:

  • SNAT 默认开启(子网内设备看到的是路由器地址)。关闭:--snat-subnet-routes=false(需手动配回程路由,一般不关)。
  • tailscale serve / funnel 只代理运行 Tailscale 的机器本身,不代理子网。
  • 子网路由器与出口节点若要共存且关 SNAT 易被丢弃,建议拆两台机器。

4.2 出口节点(Exit Node,全局科学上网/回家)

把所有流量经 tailnet 内某台机器转发,隐藏本机 IP。

# 1) 出口节点上(例如家中服务器 / VPS)
sudo tailscale set --advertise-exit-node
sudo tailscale up      # 注意:set 之后需再 up

# 2) Admin Console 审批:Edit route settings → 勾选 Exit node
# 3) ACL 需放行 autogroup:internet(见 3.5)

# 4) 客户端启用
sudo tailscale set --exit-node=<exit-node-ip>            # 或名称
sudo tailscale up --exit-node=100.x.y.z
# 允许访问本地局域网
sudo tailscale set --exit-node=<ip> --exit-node-allow-lan-access=true

# 5) 验证出口 IP
curl ifconfig.me
# 关闭
sudo tailscale set --exit-node=
  • 自动跟随建议节点:--exit-node=auto:any
  • 常踩坑:把出口节点本身当作连接目标,只允许 SSH 却上不了网 → 需要放行 autogroup:internet

4.3 App Connector(按域名路由)

只把特定域名的流量走出口节点(分流转发),比全局 exit node 更精细,适合企业场景。


5. 常用命令速查

命令 作用
tailscale up 登录并连接
tailscale set --ssh 开启 Tailscale SSH
tailscale status / ip 状态 / 本机 IP
tailscale down 断开
tailscale up --advertise-routes=192.168.1.0/24 广告子网
tailscale up --advertise-exit-node 广告出口节点
tailscale up --exit-node=100.x.y.z 使用出口节点
tailscale file cp/get Taildrop 传文件
tailscale netcheck 连通性诊断
tailscale ping <host> 打洞/连接测试
tailscale ip -4 取 IPv4

6. 常见问题排查

现象 处理
子网路由连不通 到 Admin Console 批准路由(最高频错误)
出口节点上了但无外网 ACL 未放行 autogroup:internet
服务器突然失联 密钥过期 → 关闭 Key expiry
速度慢 打洞失败走 DERP 中继,tailscale netcheck 查看是否直连
配了 --ssh 仍连不上 确认 ACL 有 22 端口 / ssh 规则
与 Caddy/Nginx 冲突? 不冲突,Caddy 管公网域名,tailscale serve 管内网 HTTPS

7. 补充

  • 免费 vs 付费:MagicDNS、Subnet Router、Exit Node、Taildrop、Serve/Funnel、基础 Tailscale SSH 均在免费计划内。
  • 注册提醒:用 @gmail.com 等公开邮箱注册自动进 Personal 计划;用自定义域名邮箱会进 Enterprise 14 天试用,不想被收费需改回。
  • 与 ZeroTier / ngrok / Cloudflare Tunnel / WireGuard 的关系:Tailscale 适合长期稳定的私有内网互联;ngrok/Cloudflare Tunnel 面向对外开放服务。
评论 0 打赏 分享

相关推荐

评论交流