本文转载自:https://www.cnblogs.com/zhaoxxnbsp/p/19799532
概述
Tailscale 是一个基于 WireGuard 的零配置 VPN 服务,可以让不同网络、不同地理位置的设备组成一个虚拟局域网,设备之间像在同一局域网内一样互相访问。
适用场景:
- 远程访问家里的电脑/NAS
- 多地点办公,设备互联
- 远程开发、SSH 连接
- 访问内网服务
1. 准备工作
1.1 注册 Tailscale 账号
访问 https://tailscale.com 点击 "Get Started",可以用以下方式注册:
- Google 账号
- GitHub 账号
- Microsoft 账号
- Apple 账号
⚠️ 免费版支持最多 100 台设备,个人使用足够。
1.2 确认设备信息
假设你的环境:
- Ubuntu 设备:服务器/开发机,IP 假设为
100.x.x.x(Tailscale 分配) - Windows 设备:办公/家用电脑
2. Ubuntu 安装与配置
2.1 安装 Tailscale
# 方法一:官方脚本(推荐)
curl -fsSL https://tailscale.com/install.sh | sh
# 方法二:apt 安装
sudo apt-get update
sudo apt-get install tailscale
2.2 启动并登录
# 启动 Tailscale 并关联账号
sudo tailscale up
执行后会输出一个链接,用浏览器打开并登录你的 Tailscale 账号完成授权。
2.3 验证状态
# 查看连接状态
sudo tailscale status
# 查看分配的 IP
tailscale ip
输出示例:
100.64.0.1 # Tailscale 分配的内网 IP
2.4 设置开机自启
sudo systemctl enable tailscaled
sudo systemctl start tailscaled
2.5 (可选)开启 IP 转发
如果要把 Ubuntu 作为子网网关或 Exit Node:
# 编辑 sysctl 配置
sudo nano /etc/sysctl.d/99-tailscale.conf
添加:
net.ipv4.ip_forward = 1
net.ipv6.conf.all.forwarding = 1
应用:
sudo sysctl -p /etc/sysctl.d/99-tailscale.conf
3. Windows 安装与配置
3.1 下载安装
- 访问 https://tailscale.com/download
- 下载 Windows 客户端
- 运行安装程序,一路 Next
3.2 登录账号
- 安装完成后,系统托盘会出现 Tailscale 图标
- 点击图标,选择 "Log in"
- 浏览器打开,用之前注册的账号登录授权
3.3 验证状态
点击托盘图标,确认显示 "Connected",并能看到分配的 IP(如 100.64.0.2)。
3.4 (可选)命令行查看
打开 PowerShell:
tailscale status
tailscale ip
4. 验证组网成功
4.1 查看所有设备
在任意设备上执行:
tailscale status
输出示例:
100.64.0.1 ubuntu-pc user@ linux -
100.64.0.2 windows-pc user@ windows -
4.2 互 ping 测试
在 Ubuntu 上 ping Windows:
ping 100.64.0.2
在 Windows 上 ping Ubuntu:
ping 100.64.0.1
如果 ping 通,说明组网成功!
4.3 测试服务访问
SSH 连接(从 Windows 连 Ubuntu):
ssh username@100.64.0.1
远程桌面(从 Ubuntu 连 Windows):
使用 Remmina 或其他 RDP 客户端,地址填 100.64.0.2
访问 Web 服务:
如果 Ubuntu 上运行了 Web 服务(端口 8080),在 Windows 浏览器访问:
http://100.64.0.1:8080
5. 高级配置
5.1 子网路由(Subnet Router)
让 Tailscale 网络中的设备访问 Ubuntu 所在局域网的其他设备。
在 Ubuntu 上配置:
假设 Ubuntu 所在局域网是 192.168.1.0/24:
# 启用子网路由
sudo tailscale up --advertise-routes=192.168.1.0/24
在管理后台启用:
- 访问 https://login.tailscale.com/admin/machines
- 找到 Ubuntu 设备
- 点击
...→Edit route settings - 启用子网路由
现在 Windows 可以直接访问 192.168.1.x 的设备了。
5.2 Exit Node(出口节点)
让所有流量通过 Ubuntu 出去(类似全局代理)。
在 Ubuntu 上启用:
sudo tailscale up --advertise-exit-node
在管理后台启用:
- 访问管理后台 → Machines
- 找到 Ubuntu →
Edit route settings - 启用
Use as exit node
在 Windows 上使用:
点击托盘图标 → Exit node → 选择 Ubuntu 设备
5.3 MagicDNS(设备名访问)
启用后可以用设备名代替 IP:
- 访问 https://login.tailscale.com/admin/dns
- 启用
MagicDNS
之后可以这样访问:
ssh username@ubuntu-pc
ping windows-pc
5.4 ACL 访问控制
限制哪些设备可以访问哪些设备/端口。
访问 https://login.tailscale.com/admin/acls 编辑规则。
示例:只允许 Windows 访问 Ubuntu 的 SSH(22 端口):
{
"acls": [
{
"action": "accept",
"src": ["tag:windows"],
"dst": ["tag:ubuntu:22"]
}
]
}
6. 常用命令速查
Ubuntu/Linux
# 启动
sudo tailscale up
# 停止
sudo tailscale down
# 查看状态
tailscale status
# 查看 IP
tailscale ip
# 查看 IP(仅 IPv4)
tailscale ip -4
# Ping 其他设备
tailscale ping <设备名或IP>
# 注销
sudo tailscale logout
# 诊断
tailscale doctor
Windows
# 大部分命令与 Linux 相同
tailscale status
tailscale ip
tailscale ping <设备名或IP>
# 通过 GUI 也可以操作:
# 右键托盘图标 → 选项菜单
7. 常见问题排查
7.1 连接不上
检查项:
# 查看服务状态
sudo systemctl status tailscaled
# 查看日志
sudo journalctl -u tailscaled -f
# 重新登录
sudo tailscale logout
sudo tailscale up
7.2 能 ping 通但访问不了服务
可能原因:
- 目标设备防火墙拦截
- 服务未监听 Tailscale 接口
解决:
# Ubuntu 检查防火墙
sudo ufw status
# 允许 Tailscale 接口
sudo ufw allow in on tailscale0
7.3 子网路由不生效
- 确认管理后台已启用该路由
- 确认 Ubuntu 开启了 IP 转发
- 检查目标子网设备网关设置
7.4 Windows 无法连接
- 检查 Windows 防火墙
- 确认 Tailscale 服务正在运行(任务管理器 → 服务)
- 尝试重启 Tailscale 客户端
7.5 速度慢
- 检查双方网络延迟
- 尝试切换到最近的 DERP 中继(管理后台查看)
- 如果可能,配置直连(开放 UDP 41641 端口)
8. 网络拓扑示例
┌─────────────────┐ ┌─────────────────┐
│ Windows PC │ │ Ubuntu PC │
│ (公司/远程) │ │ (家里/VPS) │
│ │ │ │
│ 100.64.0.2 │◄──── Tailscale ───►│ 100.64.0.1 │
│ │ 虚拟网络 │ │
│ │ │ 192.168.1.100 │
└─────────────────┘ └────────┬────────┘
│
│ 局域网
▼
┌─────────────────┐
│ NAS / 其他 │
│ 192.168.1.200 │
└─────────────────┘
通过子网路由,Windows 可以访问 192.168.1.x 所有设备
9. 安全建议
- 定期检查设备列表 - 移除不用的设备
- 使用 ACL - 限制不必要的访问
- 启用 2FA - 保护 Tailscale 账号
- 关闭不需要的功能 - 如不需要 Exit Node 就别开
- 定期更新客户端 - 保持最新版本