Files
tools-linux/winshare/README.md
T

96 lines
4.3 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.
# winshare — Windows SMB 共享按需自动挂载
使用 `cifs-utils` + systemd automount 把 Windows 共享按需挂载到 Linux:
- 访问挂载点时自动挂载,空闲 10 分钟自动卸载;
- Windows 关机或离线不会阻塞开机;
- 密码保存在仅 root 可读的凭据文件中,不写入 fstab。
## 安装
在**普通终端窗口**运行(脚本需要隐藏读取密码,必须能访问 TTY):
```bash
sudo bash setup-winshare.sh
```
脚本会依次询问:
1. **Windows 主机名或 IP**(例如 `win-pc` 或 `192.0.2.10`)
2. **共享名**(Windows 上运行 `net share` 或 PowerShell `Get-SmbShare` 可查看)
3. **Linux 挂载点**(默认 `/mnt/windows-<共享名小写>`)
4. **Windows 用户名**
- 本地账号:`alice` 或 `WINDOWS-HOST\alice`
- 微软账号:`MicrosoftAccount\you@example.com`
- 域账号:`DOMAIN\user`
- Windows 的 cmd 中运行 `whoami` 可看到准确格式
5. **域/工作组**:默认是 Windows 计算机名;输入 `-` 表示不指定
6. **密码**:输入两次,终端不回显
也可以用环境变量预填主机、共享名和挂载点:
```bash
sudo env HOST=win-pc SHARE=ShareName MOUNT=/mnt/share bash setup-winshare.sh
```
之后脚本会安装 `cifs-utils`(仅 apt 系;其他发行版请先手动安装),实际测试
挂载,写入 `/etc/fstab`,配置 systemd automount 并验证。需要 systemd、
util-linux(`mountpoint`、`findmnt`)和 root 权限;卸载脚本还需要 python3。
## 访问
```bash
ls -la /mnt/share
```
第一次访问触发挂载,之后空闲 10 分钟自动卸载;再次访问会自动挂载。
## 管理
```bash
findmnt /mnt/share
sudo umount /mnt/share
unit="$(systemd-escape -p --suffix=mount /mnt/share)"
journalctl -u "$unit" -n 30 --no-pager
sudo bash remove-winshare.sh
```
配置和凭据位置:
- `/etc/fstab` 中的注释 + 挂载条目
- `/etc/samba/credentials/winshare-<host>-<share>.cred`(0600 root)
- `/etc/winshare.conf`(记录本次配置,供卸载脚本使用)
## 常见问题
| 现象 | 可能原因 / 处理 |
| --- | --- |
| `mount error(2): No such file or directory` | 共享名不对。在 Windows 运行 `net share` 或 PowerShell `Get-SmbShare` 确认 |
| `mount error(13): Permission denied` | 用户名/密码/域错误,或 Windows 共享/NTFS 权限不足 |
| `mount error(112): Host is down` | Windows 主机离线、IP 变化,或防火墙阻止 445 |
| `NT_STATUS_LOGON_FAILURE` | 账号格式不对。试 `WINDOWS-HOST\用户名`、`WORKGROUP`、`MicrosoftAccount\邮箱` |
| 微软账号密码登录失败 | 该账号可能启用了无密码/Windows Hello。建议在 Windows 上新建一个带密码的本地账号专用于共享 |
| 管理共享 `C$`/`D$` 失败 | 管理共享需要管理员账号,且受 UAC 远程限制;建议改用普通共享 |
| SMB 版本不兼容 | 重试:`VERS=2.1`(旧设备)或 `VERS=3.1.1`:`VERS=2.1 sudo bash setup-winshare.sh` |
| Windows 上无法访问 | 网络配置文件设为“专用”,启用“文件和打印机共享”;必要时在防火墙放行 SMB |
| Windows 主机名解析失败 | 本机的 `/etc/hosts` 可由 `windiscover` 维护;也可改用 IP:`HOST=192.0.2.10 sudo bash setup-winshare.sh` |
## 说明
- 挂载归属为执行 sudo 的 Linux 用户(读取 `SUDO_UID`/`SUDO_GID`),文件权限
`0660`、目录权限 `0770`。
- 挂载点必须位于 `/mnt`、`/media`、`/srv` 或 `/home` 下,并且是空目录或不
存在的目录。脚本会拒绝系统目录、符号链接、非空目录,以及已被其它文件系统
占用的挂载点,避免误覆盖或误卸载。
- 实际运行时只写入 `/etc/fstab`、`/etc/samba/credentials` 和
`/etc/winshare*.conf`;测试可用 `DRY_RUN=1` 指定临时路径。
- 如果 Windows 端要求 SMB 加密,可在 `/etc/fstab` 的挂载选项中追加 `seal`,
然后 `sudo systemctl daemon-reload && sudo umount <挂载点>`。
- 默认使用 `x-systemd.automount` 按需挂载;如果希望开机常驻挂载,删除
`x-systemd.automount` 和 `x-systemd.idle-timeout=...`,保留 `_netdev,nofail`。
- 多套共享:用不同的挂载点,并通过 `CONFIG_FILE` 指定不同的配置文件,例如
`CONFIG_FILE=/etc/winshare-work.conf`;卸载时同样指定。
- 共享名不能包含空格或斜杠(fstab 条目会因此变得复杂)。