## 什么是 Tailscale
Tailscale 是一种基于 WireGuard 协议的端到端加密网络软件,使得在任何设备之间创建安全的网络连接变得简单。它利用现代加密技术为用户提供了一种快速、安全且易于管理的虚拟私人网络(VPN)解决方案。Tailscale 的设计允许设备直接连接,而不需要传统 VPN 需要的复杂配置和中央服务器,从而提高了效率和性能。
## 安装
```Shell
$ curl -fsSL https://tailscale.com/install.sh | sh
```
## 启动
```Shell
$ tailscale up
$ tailscale up --exit-node=xxx
```
## 网络配置
Tailscale 配置网络前需要修改如下配置,如果你的Linux系统,并且有 `/etc/sysctl.d` 目录,就执行:
```Shell
$ echo 'net.ipv4.ip_forward = 1' | tee -a /etc/sysctl.d/99-tailscale.conf
$ echo 'net.ipv6.conf.all.forwarding = 1' | tee -a /etc/sysctl.d/99-tailscale.conf
$ sysctl -p /etc/sysctl.d/99-tailscale.conf
$ # 如果使用防火墙,需要执行:
$ firewall-cmd --permanent --add-masquerade
```
### 设置节点局域网段
```Shell
# 如果你的局域网是 192.168.xxx.0 就执行
$ tailscale up --advertise-routes=192.168.xx.0/24
# 关闭
tailscale up --advertise-routes=false
```
### 设置出口节点流量
```Shell
# 开启
$ tailscale up --advertise-exit-node
# 关闭
$ tailscale up --advertise-exit-node=false
```
## 常见问题
### LXC containers 问题
PVE 创建 LXC 虚拟机无法启动 Tailscale 的解决办法,编辑 `/etc/pve/lxc/{ID}.conf` 文件,添加如下配置
```
lxc.cgroup2.devices.allow: c 10:200 rwm
lxc.mount.entry: /dev/net/tun dev/net/tun none bind,create=file
```
### 阿里云安装 Tailscale,无法使用阿里云内网DNS
由于阿里云内网与 Tailscale DNS 100.100.100.100 网段重复,可以使用如下两种方法解决
#### 手动添加阿里云 DNS
1. 编辑 `/etc/systemd/resolved.conf` 文件,保存下面内容
```
DNS=223.5.5.5
```
2. 重启 resolved
```Shell
$ systemctl restart systemd-resolved
$ systemctl enable systemd-resolved
```
#### 自定义 Tailscale DNS
Tailscale 的 DNS 会覆盖系统的 DNS,导致无法使用内网 DNS,可以使用 `systemd-resolved` 的 `resolvectl` 设置DNS解决
1. 关闭 Tailscale 的 MagicDNS
```shell
tailscale up --accept-dns=false
```
2. 使用 `resolvectl` 设置 DNS
```shell
sudo resolvectl dns tailscale0 100.100.100.100
sudo resolvectl domain tailscale0 xxx-xx.ts.net
sudo resolvectl status tailscale0
```
3. 测试
```shell
sudo resolvectl query xxx.com
```
### tailscale 和 surge 一起使用
同时使用 Tailscale 和 Surge,需要在菜单栏的设置中取消勾选 "Use Tailscale DNS settings",如使用命令请确保去掉 `--accept-dns` 参数
## 参考
- https://tailscale.com/kb/1019/subnets?tab=linux#connect-to-tailscale-as-a-subnet-router
- https://tailscale.com/kb/1103/exit-nodes?tab=linux
- https://tailscale.com/kb/1130/lxc-unprivileged