Tailscale — 基于 WireGuard 的零配置 VPN 网络
安装与配置
# === Linux 一键安装 ===
curl -fsSL https://tailscale.com/install.sh | sh # 官方安装脚本(自动检测发行版)
sudo systemctl enable --now tailscaled # 启动后台守护进程
# === Ubuntu/Debian 手动安装 ===
curl -fsSL https://pkgs.tailscale.com/stable/ubuntu/jammy.noarmor.gpg | \
sudo tee /usr/share/keyrings/tailscale-archive-keyring.gpg # 导入 GPG 密钥
echo "deb [signed-by=/usr/share/keyrings/tailscale-archive-keyring.gpg] \
https://pkgs.tailscale.com/stable/ubuntu jammy main" | \
sudo tee /etc/apt/sources.list.d/tailscale.list # 添加 APT 源
sudo apt update && sudo apt install tailscale # 安装 Tailscale
# === macOS 安装 ===
brew install --cask tailscale # Homebrew 安装 GUI 版
# 或
brew install tailscale # 仅安装 CLI 版
# === 首次连接 ===
sudo tailscale up # 启动并连接(会弹出浏览器认证)
tailscale ip -4 # 查看分配的 Tailscale IPv4 地址
tailscale status # 查看网络中所有设备状态
核心用法
# --- 基本网络操作 ---
tailscale up # 连接到你的 tailnet(虚拟局域网)
tailscale down # 断开连接
tailscale status # 查看所有设备及连接状态
tailscale ping myserver # ping 网络中的其他设备(用设备名或 IP)
# --- SSH 访问(无需配置密钥)---
tailscale set --ssh # 在目标机器上启用 Tailscale SSH
ssh user@device-name # 直接用设备名 SSH(自动认证,无需密钥)
# --- 文件传输(Taildrop)---
tailscale file cp report.pdf myphone: # 发送文件到 "myphone" 设备
tailscale file get ~/Downloads/ # 在接收端获取文件到指定目录
# --- 出口节点(Exit Node,类似传统 VPN)---
tailscale set --advertise-exit-node # 在服务器端声明自己是出口节点
tailscale set --exit-node=myserver # 在客户端指定出口节点(所有流量走该节点)
tailscale set --exit-node= # 取消使用出口节点
# --- 子网路由(访问内网资源)---
tailscale set --advertise-routes=192.168.1.0/24 # 声明可路由的内网子网
# 在管理后台审批子网路由后,其他设备即可访问该内网
# --- 暴露服务到公网 ---
tailscale funnel 8080 # 将本地 8080 端口暴露到公网(自动 HTTPS)
tailscale serve 3000 # 将本地 3000 端口仅暴露给 tailnet 内部
参数详解
| 参数 | 说明 |
|---|
--auth-key | 预认证密钥,用于无浏览器的自动化部署 |
--ssh | 启用 Tailscale SSH(基于身份认证,无需密钥) |
--advertise-exit-node | 声明本机为出口节点 |
--exit-node=<name> | 指定流量出口节点 |
--advertise-routes | 声明子网路由(让其他设备访问内网) |
--accept-routes | 接受其他设备声明的子网路由 |
--hostname | 设置设备在 tailnet 中的名称 |
--operator=$USER | 允许非 root 用户操作 tailscale CLI |
--shields-up | 拒绝所有入站连接(仅允许出站) |
实战案例
# === 场景:远程开发——从家里访问公司内网服务器 ===
# --- 公司服务器端(Ubuntu)---
curl -fsSL https://tailscale.com/install.sh | sh # 安装 Tailscale
sudo tailscale up --auth-key=tskey-xxxxx \
--ssh \
--advertise-routes=10.0.0.0/24 # 启动+SSH+声明内网路由
# --- 家里笔记本端 ---
sudo tailscale up --accept-routes # 启动并接受子网路由
tailscale status # 确认能看到公司服务器
# 现在可以直接访问
ssh ubuntu@company-server # SSH 到公司服务器(无需 VPN 客户端)
curl http://10.0.0.5:8080 # 直接访问公司内网 Web 服务
ping 10.0.0.1 # ping 内网网关
# --- 临时分享本地开发服务 ---
tailscale funnel 3000 # 把本地 3000 端口暴露到公网
# 输出类似: https://your-device.tailnet-name.ts.net/
# 同事可以直接通过这个 URL 访问你本地的开发服务
常见报错与解决
| 报错 | 原因 | 解决方案 |
|---|
tailscaled is not running | 后台服务未启动 | sudo systemctl start tailscaled |
Failed to connect | 网络被防火墙阻断 | Tailscale 会自动走 DERP 中继,检查 443 端口 |
ACL denied | 管理后台的 ACL 规则限制 | 在管理后台调整 Access Controls |
subnet route not approved | 子网路由未在管理后台审批 | 登录 admin console 批准路由 |
key expired | 设备密钥过期 | 重新执行 tailscale up 重新认证 |
速查表
| 操作 | 命令 |
|---|
| 安装 | curl -fsSL https://tailscale.com/install.sh \| sh |
| 连接 | tailscale up |
| 断开 | tailscale down |
| 查看状态 | tailscale status |
| 查看 IP | tailscale ip |
| 启用 SSH | tailscale set --ssh |
| 发送文件 | tailscale file cp <file> <device>: |
| 声明出口节点 | tailscale set --advertise-exit-node |
| 声明子网路由 | tailscale set --advertise-routes=<CIDR> |
| 暴露到公网 | tailscale funnel <port> |
| 更新版本 | tailscale update |
| 查看版本 | tailscale version |