Windows + Rocky Linux:通过 Tailscale 互传文件

1. 概述

通过 Tailscale 组建虚拟局域网(WireGuard 隧道),实现 Windows 和 Rocky Linux 两台电脑之间的安全文件互传,无需公网 IP、端口转发或复杂防火墙配置。

环境信息:

项目 机器 A 机器 B
操作系统 Windows 10/11 Rocky Linux 9.x
角色 客户端 客户端
Tailscale IP 安装后自动分配(100.x.x.x) 安装后自动分配(100.x.x.x)

2. Tailscale 账号准备

  1. 访问 https://login.tailscale.com 注册账号(支持 Google / Microsoft / GitHub 登录)。
  2. 两台机器使用同一个账号登录,即可自动加入同一 Tailnet。

3. Windows 端安装与配置

3.1 安装 Tailscale

  1. 访问 https://tailscale.com/download/windows 下载安装包。
  2. 运行安装程序,按提示完成安装。
  3. 安装完成后系统托盘会出现 Tailscale 图标,点击 Log in 用浏览器登录账号。

3.2 验证连接

打开 PowerShell 执行:

1
tailscale status

输出中应能看到本机信息及分配的 100.x.x.x 地址。

3.3 开启 Taildrop(文件传输)

Taildrop 是 Tailscale 内置的文件传输功能,默认已启用,无需额外配置。

确认方法:登录 https://login.tailscale.com/admin/acls,确保 ACL 中没有禁用 Taildrop(默认 ACL 策略允许)。


4. Rocky Linux 端安装与配置

4.1 安装 Tailscale

1
2
3
4
5
6
7
8
# 添加 Tailscale 官方仓库
sudo dnf config-manager --add-repo https://pkgs.tailscale.com/stable/rhel/9/tailscale.repo

# 安装
sudo dnf install tailscale -y

# 启动并设为开机自启
sudo systemctl enable --now tailscaled

4.2 登录认证

1
sudo tailscale up

终端会输出一个认证 URL,在浏览器中打开并用与 Windows 相同的账号登录完成授权。

4.3 验证连接

1
tailscale status

应能看到本机和 Windows 机器的信息,记录两台机器的 hostname(后续传文件要用)。

4.4 互 ping 测试

1
2
# 在 Rocky Linux 上 ping Windows(用 Tailscale hostname 或 100.x.x.x)
ping <windows-hostname>

在 Windows 上同样可以 ping Rocky Linux:

1
ping <rocky-hostname>

如果双向 ping 通,说明 Tailscale 隧道已建立成功。


5. 文件互传方式

方式一:Taildrop(Tailscale 内置,推荐)

Taildrop 是最简单的方式,无需额外安装任何软件。

Linux 发送文件到 Windows

1
2
3
4
5
6
7
8
9
# 发送单个文件
tailscale file cp /path/to/file.txt <windows-hostname>:

# 发送多个文件
tailscale file cp /path/to/file1.txt /path/to/file2.txt <windows-hostname>:

# 发送整个目录(需先打包)
tar czf archive.tar.gz /path/to/directory
tailscale file cp archive.tar.gz <windows-hostname>:

Windows 端收到文件后,会在系统托盘弹出通知,文件默认保存到 Downloads 目录。

也可以在 Windows 上通过命令行接收:

1
2
# 查看待接收文件
tailscale file get .

Windows 发送文件到 Linux

方法 A — 命令行:

1
tailscale file cp C:\path\to\file.txt <rocky-hostname>:

方法 B — 右键菜单(GUI):

在文件资源管理器中右键文件 → Send with Tailscale → 选择目标 Rocky Linux 设备。

Linux 端接收文件:

1
2
# 在你想保存文件的目录下执行
tailscale file get .

文件会下载到当前目录。


方式二:Samba 共享(适合频繁大量文件操作)

通过 Tailscale 网络搭建 Samba 共享,Windows 可以像访问本地文件夹一样操作 Linux 文件。

4.1 Rocky Linux 安装配置 Samba

1
2
3
4
5
6
# 安装 Samba
sudo dnf install samba samba-client -y

# 创建共享目录
mkdir -p ~/shared
chmod 755 ~/shared

编辑 Samba 配置文件:

1
sudo vim /etc/samba/smb.conf

在文件末尾添加:

1
2
3
4
5
6
7
[shared]
path = /home/<你的用户名>/shared
browseable = yes
writable = yes
valid users = <你的用户名>
create mask = 0644
directory mask = 0755

设置 Samba 密码并启动服务:

1
2
3
4
5
# 设置 Samba 用户密码(可以和系统密码不同)
sudo smbpasswd -a <你的用户名>

# 启动 Samba 服务
sudo systemctl enable --now smb nmb

4.2 防火墙放行(仅允许 Tailscale 接口)

1
2
3
4
5
6
# 获取 Tailscale 接口名(通常是 tailscale0)
ip addr show tailscale0

# 将 Tailscale 接口加入 trusted zone(仅信任 Tailscale 流量)
sudo firewall-cmd --zone=trusted --add-interface=tailscale0 --permanent
sudo firewall-cmd --reload

这样只有通过 Tailscale 隧道的流量才能访问 Samba,不会暴露在公网。

4.3 Windows 访问 Samba 共享

  1. 打开文件资源管理器,地址栏输入:
1
\\<rocky-tailscale-ip>\shared

或使用 Tailscale hostname:

1
\\<rocky-hostname>\shared
  1. 输入之前设置的 Samba 用户名和密码。

  2. 可右键 映射网络驱动器 将其挂载为盘符(如 Z:),方便日常使用。


方式三:SFTP/SCP(利用 SSH,无需额外服务)

Rocky Linux 通常默认已安装 OpenSSH Server。

确认 SSH 服务运行

1
2
3
sudo systemctl status sshd
# 如果未运行
sudo systemctl enable --now sshd

Windows 通过 SCP 传输文件

Windows 10/11 自带 OpenSSH 客户端:

1
2
3
4
5
# Windows → Linux
scp C:\path\to\file.txt <用户名>@<rocky-hostname>:/home/<用户名>/

# Linux → Windows(在 Windows 上拉取)
scp <用户名>@<rocky-hostname>:/path/to/file.txt C:\Users\<用户名>\Downloads\

使用 WinSCP 图形界面(可选)

  1. 下载安装 WinSCP:https://winscp.net
  2. 新建连接:
  • 协议:SFTP
  • 主机名:Rocky Linux 的 Tailscale IP 或 hostname
  • 用户名/密码:Linux 系统账号
  1. 连接后可拖拽传输文件。

6. 三种方式对比

特性 Taildrop Samba SFTP/SCP
安装复杂度 无需额外安装 需安装配置 Samba 通常已有 SSH
适用场景 偶尔传几个文件 频繁读写、大量文件 偶尔传输、脚本自动化
传输速度 一般 快(持续连接)
双向传输 支持 支持(Windows 可读写) 支持
图形界面 Windows 右键菜单 Windows 资源管理器 WinSCP
额外端口 445(仅 Tailscale 内) 22(仅 Tailscale 内)

7. 开机自启动配置

7.1 Windows 端

Tailscale Windows 客户端安装后默认开机自启,无需额外配置。

确认方法:

  1. Ctrl + Shift + Esc 打开任务管理器 → 启动 标签页 → 确认 Tailscale 状态为「已启用」。
  2. 或通过服务管理器确认:
1
2
# PowerShell 查看 Tailscale 服务状态
Get-Service -Name "Tailscale" | Select-Object Name, Status, StartType

输出的 StartType 应为 Automatic

如果未自启动,手动设置:

1
2
3
4
5
# 设置 Tailscale 服务为自动启动
Set-Service -Name "Tailscale" -StartupType Automatic

# 立即启动服务
Start-Service -Name "Tailscale"

开机自动登录(免手动点 Log in):

Tailscale 认证成功一次后,密钥会缓存在本地,重启后自动重连,无需重复登录。如果密钥过期(默认 180 天),需要重新认证。可在 Admin Console 中为设备禁用密钥过期

  1. 访问 https://login.tailscale.com/admin/machines
  2. 点击目标设备 → Disable key expiry

7.2 Rocky Linux 端

安装时已执行 systemctl enable tailscaled,服务会开机自启。

验证自启状态:

1
2
3
4
5
6
# 查看服务是否 enabled
systemctl is-enabled tailscaled
# 输出应为: enabled

# 查看详细状态
systemctl status tailscaled

确保登录状态持久化:

tailscale up 登录一次后,认证状态会持久保存在 /var/lib/tailscale/ 中,重启后自动重连。

如需开机后自动以特定参数启动(如指定 exit node、advertise routes),创建 override 配置:

1
2
3
4
5
# 查看当前启动参数
tailscale status --json | grep -i "backend"

# 如果需要自定义启动参数,编辑 service override
sudo systemctl edit tailscaled

在编辑器中添加:

1
2
[Service]
Environment="TS_EXTRA_ARGS=--accept-routes"

保存后重载:

1
2
sudo systemctl daemon-reload
sudo systemctl restart tailscaled

7.3 Samba 服务自启动(如果使用 Samba 方式)

1
2
3
4
5
6
# 确认 smb 和 nmb 均为开机自启
systemctl is-enabled smb nmb
# 两个都应输出: enabled

# 如果不是,手动启用
sudo systemctl enable smb nmb

7.4 SSH 服务自启动(如果使用 SFTP/SCP 方式)

1
2
3
4
5
6
# 确认 sshd 为开机自启
systemctl is-enabled sshd
# 输出应为: enabled

# 如果不是,手动启用
sudo systemctl enable sshd

7.5 开机自启验证清单

重启两台机器后,依次检查:

检查项 Windows 命令 Rocky Linux 命令
Tailscale 服务运行 Get-Service Tailscale systemctl status tailscaled
Tailscale 已连接 tailscale status tailscale status
互相可达 ping <rocky-hostname> ping <windows-hostname>
Samba 运行(可选) systemctl status smb
SSH 运行(可选) systemctl status sshd

8. 详细排障指南

8.1 Tailscale 连接问题

Q1: tailscale status 看不到对方设备

排查步骤:

1
2
3
4
5
6
7
8
9
10
11
12
13
# 1. 确认本机 tailscaled 服务运行
# Linux:
systemctl status tailscaled
# Windows (PowerShell):
# Get-Service Tailscale

# 2. 确认本机已登录
tailscale status
# 如果显示 "Logged out" 或 "NeedsLogin",重新登录:
sudo tailscale up

# 3. 确认设备密钥未过期
tailscale status --json | grep -i "expired"
  • 确认两台机器登录的是同一个 Tailscale 账号
  • 登录 https://login.tailscale.com/admin/machines 查看设备是否在线。
  • 如果设备显示 “key expired”,点击设备 → Reauthenticate,或在终端重新执行 tailscale up

Q2: ping 不通对方

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 1. 确认 Tailscale 接口存在且有 IP
ip addr show tailscale0
# 应该看到 100.x.x.x 地址

# 2. 确认路由正常
ip route | grep tailscale

# 3. 检查 Tailscale 连接状态
tailscale status
# 对方设备行末应显示 "active" 或直连 IP

# 4. Rocky Linux 防火墙检查
sudo firewall-cmd --get-active-zones
# 确认 tailscale0 在 trusted zone 中
sudo firewall-cmd --zone=trusted --query-interface=tailscale0
# 如果返回 no,添加它:
sudo firewall-cmd --zone=trusted --add-interface=tailscale0 --permanent
sudo firewall-cmd --reload

# 5. Windows 防火墙检查(PowerShell 管理员)
# 确认 Tailscale 网络配置文件为"专用"
# Get-NetConnectionProfile | Where-Object {$_.InterfaceAlias -like "*Tailscale*"}
# 如果是"公用",改为"专用"以允许 ping:
# Set-NetConnectionProfile -InterfaceAlias "Tailscale" -NetworkCategory Private

Q3: Tailscale 节点认证过期

1
2
3
4
5
6
7
8
9
10
# 检查密钥过期时间
tailscale status --json | python3 -c "
import sys, json
data = json.load(sys.stdin)
for peer_id, peer in data.get('Peer', {}).items():
print(f\"{peer['HostName']}: KeyExpiry={peer.get('KeyExpiry', 'N/A')}\")
"

# 重新认证
sudo tailscale up

永久解决:在 Admin Console 中为设备禁用密钥过期(见 7.1 节)。

8.2 Taildrop 文件传输问题

Q4: 发送文件报错 “file transfer not enabled”

Q5: 发送成功但对方收不到

Windows 端收不到:

1
2
3
4
5
6
# 手动接收(在目标目录下执行)
cd $env:USERPROFILE\Downloads
tailscale file get .

# 查看待接收文件列表
tailscale file get --list
  • 检查系统托盘 Tailscale 图标是否有通知气泡。
  • 确认 Windows Defender 没有拦截文件。

Linux 端收不到:

1
2
3
4
5
6
7
8
9
10
# 手动接收
tailscale file get .

# 如果报权限错误,确认当前目录可写
ls -la .
# 或指定有写权限的目录
tailscale file get /tmp/

# 查看 tailscaled 日志排查
sudo journalctl -u tailscaled --since "5 minutes ago" | grep -i file

Q6: 大文件传输中断

  • Taildrop 不支持断点续传,中断后需重新发送。
  • 大文件(> 1GB)建议使用 Samba 或 SCP 方式,更稳定。
  • 传输前可先压缩,减少传输时间:
1
2
3
4
5
# Linux 端压缩
tar czf data.tar.gz /path/to/large-directory

# Windows 端压缩(PowerShell)
# Compress-Archive -Path C:\path\to\folder -DestinationPath C:\path\to\data.zip

8.3 Samba 共享问题

Q7: Windows 无法连接 Samba 共享

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 1. 确认 Samba 服务运行
sudo systemctl status smb
# 如果未运行
sudo systemctl start smb

# 2. 验证 Samba 配置文件语法
testparm

# 3. 确认用户已添加到 Samba
sudo pdbedit -L
# 如果没有用户,添加:
sudo smbpasswd -a <用户名>

# 4. 检查 SELinux(Rocky Linux 最常见问题)
# 查看 SELinux 状态
getenforce
# 如果是 Enforcing,需要正确设置策略:
sudo setsebool -P samba_enable_home_dirs on

# 如果共享目录不在 home 下
sudo semanage fcontext -a -t samba_share_t "/path/to/shared(/.*)?"
sudo restorecon -Rv /path/to/shared

# 5. 查看 SELinux 拒绝日志
sudo ausearch -m avc --start recent | grep smb

# 6. 检查防火墙
sudo firewall-cmd --zone=trusted --query-interface=tailscale0

Windows 端排查:

1
2
3
4
5
6
7
8
9
# 测试 SMB 端口连通性
Test-NetConnection -ComputerName <rocky-hostname> -Port 445

# 清除缓存的凭据(如果密码改了连不上)
# 控制面板 → 凭据管理器 → Windows 凭据 → 删除对应条目
# 或命令行:
net use \\<rocky-hostname>\shared /delete
# 重新连接
net use \\<rocky-hostname>\shared /user:<用户名> <密码>

Q8: Samba 能连但无法写入文件

1
2
3
4
5
6
7
8
9
10
# 1. 检查 Linux 文件系统权限
ls -la /home/<用户名>/shared/
# 确认目录对当前用户可写

# 2. 检查 smb.conf 中 writable 设置
grep -A5 "\[shared\]" /etc/samba/smb.conf
# 确认 writable = yes

# 3. 检查 SELinux 是否阻止写入
sudo ausearch -m avc --start recent | grep smb

8.4 SFTP/SCP 问题

Q9: SSH 连接被拒绝

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 1. 确认 sshd 运行
sudo systemctl status sshd

# 2. 确认 SSH 监听端口
sudo ss -tlnp | grep:22

# 3. 检查防火墙(tailscale0 应在 trusted zone)
sudo firewall-cmd --zone=trusted --query-interface=tailscale0

# 4. 检查 SSH 配置是否禁止了密码登录
sudo grep -E "^(PasswordAuthentication|PermitRootLogin)" /etc/ssh/sshd_config

# 5. 查看 SSH 拒绝日志
sudo journalctl -u sshd --since "10 minutes ago" | grep -i "refused\|denied\|failed"

Q10: SCP 传输速度异常慢

1
2
3
4
5
6
7
8
9
# 1. 确认 Tailscale 是直连还是中继
tailscale ping <对方hostname>
# "via DERP" = 中继(慢), "pong from ... via <ip>" = 直连(快)

# 2. 尝试使用更快的加密算法
scp -c aes128-gcm@openssh.com /path/to/file <用户名>@<rocky-hostname>:/path/

# 3. 大文件可用 rsync 替代(支持断点续传)
rsync -avP --progress /path/to/file <用户名>@<rocky-hostname>:/path/

8.5 Tailscale 网络性能问题

Q11: 传输速度慢(走 DERP 中继)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 1. 确认连接方式
tailscale ping <对方hostname>
# 多 ping 几次,Tailscale 会尝试建立直连

# 2. 查看网络路径详情
tailscale netcheck
# 输出会显示:
# - 是否支持 UDP
# - 与各 DERP 服务器的延迟
# - 是否可以 NAT 穿透

# 3. 如果两台机器在同一局域网
# 确保路由器支持 NAT hairpinning
# 或者直接走局域网 IP(Tailscale 会自动优化)

# 4. 重启 Tailscale 触发重新握手
sudo systemctl restart tailscaled
# 等待几秒后再测试
tailscale ping <对方hostname>

常见原因和解决方案:

原因 解决方案
严格 NAT / 对称 NAT 换用支持 Full Cone NAT 的路由器
防火墙阻止 UDP 放行 UDP 41641 端口
ISP 限制 UDP 联系 ISP 或使用其他网络
设备刚加入网络 等待 1-2 分钟让直连握手完成

Q12: Tailscale 突然断开

1
2
3
4
5
6
7
8
9
10
11
12
13
# 1. 查看 tailscaled 日志
sudo journalctl -u tailscaled --since "30 minutes ago" -f

# 2. 常见断开原因及解决
# a) 网络切换(WiFi → 有线):Tailscale 会自动重连,等待几秒
# b) 休眠/睡眠唤醒后:
sudo systemctl restart tailscaled

# c) 系统时间不同步(会导致密钥验证失败):
timedatectl status
# 如果时间不对:
sudo timedatectl set-ntp true
sudo systemctl restart chronyd

8.6 日志收集(提交工单或社区求助时使用)

1
2
3
4
5
6
7
8
9
10
# Rocky Linux 完整日志收集
tailscale bugreport
# 会输出一个 bug report ID,可提交给 Tailscale 支持

# 查看详细运行日志
sudo journalctl -u tailscaled --no-pager > /tmp/tailscale-log.txt

# 网络诊断信息
tailscale netcheck > /tmp/tailscale-netcheck.txt 2>&1
tailscale status --json > /tmp/tailscale-status.json

Windows 端:

1
2
3
4
5
# 收集日志
tailscale bugreport

# 日志文件位置
# %LocalAppData%\Tailscale\Logs\

9. 安全建议

  1. ACL 最小权限:如果 Tailnet 中有多台设备,在 Admin Console 中配置 ACL 限制设备间访问权限。
  2. Samba 仅绑定 Tailscale 接口:在 smb.conf[global] 段添加:
1
2
interfaces = tailscale0
bind interfaces only = yes
  1. SSH 密钥认证:建议用密钥替代密码登录,提高安全性:
1
2
3
4
# Windows 生成密钥
ssh-keygen -t ed25519
# 复制公钥到 Linux
type $env:USERPROFILE\.ssh\id_ed25519.pub | ssh <用户名>@<rocky-hostname> "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"
  1. 定期更新:保持两端 Tailscale 版本为最新。