Files
nexus/knowledgebase/WSL2 Ubuntu 中挂载 NAS 网盘完全指南.md

265 lines
4.4 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#wsl #ubuntu #nas #mount
## 背景
- **环境**: Windows 11 + WSL2 (Ubuntu 24)
- **硬件**: Lenovo ThinkBook 16, Synology DS718 NAS
- **目标**: 在 Ubuntu 中访问 NAS 上的 `/volume1/music` 共享文件夹
- **Windows 路径**: `\\192.168.3.17\music`
## 踩过的坑
### 1. CIFS 方案失败 ❌
#### 尝试方法
```bash
sudo apt install cifs-utils
sudo mkdir -p /mnt/nas-music
sudo mount -t cifs //192.168.3.17/music /mnt/nas-music -o username=shenwei,password=your_password
```
#### 错误信息
```
mount error(95): Operation not supported
```
#### 原因分析
- 虽然内核支持 CIFS (`grep -i cifs /proc/filesystems` 有输出)
- 但 WSL2 内核被配置为禁止旧 SMB 版本
- dmesg 显示: `vers=2.0 mount not permitted when legacy dialects disabled`
- Synology NAS 只支持 SMB 2.0 及更低版本,与 WSL2 不兼容
#### 相关错误日志
```
CIFS: VFS: \\192.168.3.17 Dialect not supported by server
CIFS: VFS: vers=2.0 mount not permitted when legacy dialects disabled
```
### 2. SSHFS 方案问题 ⚠️
#### 尝试方法
```bash
sudo apt install sshfs
sudo sshfs shenwei@192.168.3.17:/volume1/music /mnt/nas-music -o allow_other
```
#### 错误信息
```
read: Connection reset by peer
```
#### 原因
可能是 Synology SSH 配置的限制或 sshfs 参数兼容性问题
#### 建议
如果 SSH 能连接但 sshfs 有问题,可尝试:
- 用密钥认证而不是密码
- 调整参数: `-o reconnect,ServerAliveInterval=15`
- 但不如直接用 drvfs 稳定
---
## ✅ 最终成功方案:在 Windows 挂载后,WSL2 中访问
### 第 1 步:在 Windows 中挂载 NAS(已完成)
PowerShell (管理员):
```powershell
net use Z: \\192.168.3.17\music /user:shenwei your_password
```
验证:
```powershell
dir Z:\
```
### 第 2 步:在 WSL2 Ubuntu 中挂载 Windows 驱动器
创建挂载点:
```bash
sudo mkdir -p /mnt/z
```
挂载 Z: 驱动器:
```bash
sudo mount -t drvfs Z: /mnt/z
```
验证:
```bash
ls /mnt/z/test
```
输出示例:
```
20260815
@eaDir
albums
new
test
另外处理
张国荣音乐合集1978-2007 140CD FLAC
英文经典
```
### 第 3 步:永久自动挂载(可选)
编辑 `/etc/fstab`:
```bash
sudo nano /etc/fstab
```
在末尾添加:
```
Z: /mnt/z drvfs defaults 0 0
```
保存 (Ctrl+O → Enter → Ctrl+X)
测试:
```bash
sudo mount -a
```
以后每次启动 WSL2,Z: 驱动器会自动挂载到 `/mnt/z`
---
## 使用示例
### 在 Python 中访问 NAS 文件
```python
import os
from pathlib import Path
# NAS 路径
nas_music_path = Path("/mnt/z")
# 列出所有目录
for item in nas_music_path.iterdir():
print(item.name)
# 处理音乐文件
for flac_file in nas_music_path.glob("**/*.flac"):
print(f"处理: {flac_file}")
```
### 在 Bash 中访问 NAS 文件
```bash
# 列出文件
ls /mnt/z/albums
# 复制文件
cp /mnt/z/albums/artist/album.flac /home/user/music/
# 批量处理
for file in /mnt/z/**/*.flac; do
echo "处理: $file"
done
```
### 在 Docker 容器中使用
在 `docker-compose.yml` 中挂载:
```yaml
volumes:
- /mnt/z:/nas-music:ro
```
容器内访问:
```bash
ls /nas-music/albums
```
---
## 性能和稳定性对比
|方案|稳定性|性能|易用性|备注|
|---|---|---|---|---|
|CIFS|❌|-|-|WSL2 不支持低版本 SMB|
|SSHFS|⚠️|中|中|可能有兼容性问题|
|**drvfs**|✅|快|✅|**推荐方案**|
---
## 故障排查
### 问题 1: 挂载点不存在
```bash
# 解决
sudo mkdir -p /mnt/z
```
### 问题 2: 权限不足
```bash
# 加 sudo
sudo mount -t drvfs Z: /mnt/z
```
### 问题 3: Z: 驱动器在 Windows 中不可用
```powershell
# Windows PowerShell (管理员) 中重新挂载
net use Z: \\192.168.3.17\music /user:shenwei your_password
```
### 问题 4: WSL2 中 Z: 驱动器挂载失败
```bash
# 查看 dmesg 日志
sudo dmesg | tail -20
# 尝试卸载后重新挂载
sudo umount /mnt/z
sudo mount -t drvfs Z: /mnt/z
```
---
## 总结
**最简单和最稳定的方案**:
1. **Windows 中**: `net use Z: \\192.168.3.17\music`
2. **WSL2 中**: `sudo mount -t drvfs Z: /mnt/z`
3. **Ubuntu 中访问**: `/mnt/z/test`
不需要配置复杂的 CIFS 或 SSHFS,直接利用 Windows 已有的网络共享,通过 WSL2 的 drvfs 驱动访问,**稳定、快速、可靠**。
---
## 相关文件路径
- Windows 网络路径: `\\192.168.3.17\music`
- Windows 驱动器: `Z:\`
- WSL2 挂载点: `/mnt/z`
- 音乐文件实际位置: `/mnt/z/albums/...`
---
**更新时间**: 2026-08-16
**验证环境**: WSL2 Ubuntu 24, Synology DS718