macOS 挂载 Linux 远程目录详解
在 Mac 上直接读写 Linux 服务器上的代码、日志或数据目录,不必每次 scp 来回拷。SSHFS、NFS、SMB 三种方式覆盖远程/VPN、局域网高速、Windows 混用等场景;开发者常用 SSHFS 把远端项目挂到本地,用 VS Code 编辑而工具链留在 Linux。
速览
- 远程 / 只开 22 端口 :SSHFS(走 SSH,Linux 端零额外服务)。
- 同局域网大文件 :NFS(最快,需 Linux 配 exports + 注意 UID)。
- Win + Mac 共用 :SMB/Samba (Finder
Cmd+K连接)。- Mac 装 sshfs :
sshfs-mac依赖 macFUSE ;不想用内核扩展可换 FUSE-T + fuse-t-sshfs。
核心结论(30 秒版)
- 能 SSH 登录 → 优先 SSHFS,一行挂载,加密传输。
- 局域网传大文件 → NFS ;混合办公 → SMB。
- Homebrew 装
gromgit/fuse/sshfs-mac报错 → 先装 macFUSE 并在「隐私与安全性」放行,或改 FUSE-T 全家桶。 - 挂载后权限不对 → 查 Mac/Linux UID ;Finder 卡 → 加
-o local,volname=...。 - Ventura 及以后装 macFUSE 被拦 → 恢复模式 降低安全策略并允许内核扩展。
目录
- [1. 方案选型](#1. 方案选型)
- [2. SSHFS(推荐)](#2. SSHFS(推荐))
- [3. macFUSE 与 FUSE-T 安装](#3. macFUSE 与 FUSE-T 安装)
- [4. sshfs 用法与参数](#4. sshfs 用法与参数)
- [5. NFS(局域网)](#5. NFS(局域网))
- [6. Samba / SMB](#6. Samba / SMB)
- [7. 常见问题](#7. 常见问题)
- [8. 延伸阅读](#8. 延伸阅读)
1. 方案选型
#mermaid-svg-0iNxP5SZMCWdphGv{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-0iNxP5SZMCWdphGv .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-0iNxP5SZMCWdphGv .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-0iNxP5SZMCWdphGv .error-icon{fill:#552222;}#mermaid-svg-0iNxP5SZMCWdphGv .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-0iNxP5SZMCWdphGv .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-0iNxP5SZMCWdphGv .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-0iNxP5SZMCWdphGv .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-0iNxP5SZMCWdphGv .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-0iNxP5SZMCWdphGv .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-0iNxP5SZMCWdphGv .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-0iNxP5SZMCWdphGv .marker{fill:#333333;stroke:#333333;}#mermaid-svg-0iNxP5SZMCWdphGv .marker.cross{stroke:#333333;}#mermaid-svg-0iNxP5SZMCWdphGv svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-0iNxP5SZMCWdphGv p{margin:0;}#mermaid-svg-0iNxP5SZMCWdphGv .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-0iNxP5SZMCWdphGv .cluster-label text{fill:#333;}#mermaid-svg-0iNxP5SZMCWdphGv .cluster-label span{color:#333;}#mermaid-svg-0iNxP5SZMCWdphGv .cluster-label span p{background-color:transparent;}#mermaid-svg-0iNxP5SZMCWdphGv .label text,#mermaid-svg-0iNxP5SZMCWdphGv span{fill:#333;color:#333;}#mermaid-svg-0iNxP5SZMCWdphGv .node rect,#mermaid-svg-0iNxP5SZMCWdphGv .node circle,#mermaid-svg-0iNxP5SZMCWdphGv .node ellipse,#mermaid-svg-0iNxP5SZMCWdphGv .node polygon,#mermaid-svg-0iNxP5SZMCWdphGv .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-0iNxP5SZMCWdphGv .rough-node .label text,#mermaid-svg-0iNxP5SZMCWdphGv .node .label text,#mermaid-svg-0iNxP5SZMCWdphGv .image-shape .label,#mermaid-svg-0iNxP5SZMCWdphGv .icon-shape .label{text-anchor:middle;}#mermaid-svg-0iNxP5SZMCWdphGv .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-0iNxP5SZMCWdphGv .rough-node .label,#mermaid-svg-0iNxP5SZMCWdphGv .node .label,#mermaid-svg-0iNxP5SZMCWdphGv .image-shape .label,#mermaid-svg-0iNxP5SZMCWdphGv .icon-shape .label{text-align:center;}#mermaid-svg-0iNxP5SZMCWdphGv .node.clickable{cursor:pointer;}#mermaid-svg-0iNxP5SZMCWdphGv .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-0iNxP5SZMCWdphGv .arrowheadPath{fill:#333333;}#mermaid-svg-0iNxP5SZMCWdphGv .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-0iNxP5SZMCWdphGv .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-0iNxP5SZMCWdphGv .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-0iNxP5SZMCWdphGv .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-0iNxP5SZMCWdphGv .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-0iNxP5SZMCWdphGv .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-0iNxP5SZMCWdphGv .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-0iNxP5SZMCWdphGv .cluster text{fill:#333;}#mermaid-svg-0iNxP5SZMCWdphGv .cluster span{color:#333;}#mermaid-svg-0iNxP5SZMCWdphGv div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-0iNxP5SZMCWdphGv .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-0iNxP5SZMCWdphGv rect.text{fill:none;stroke-width:0;}#mermaid-svg-0iNxP5SZMCWdphGv .icon-shape,#mermaid-svg-0iNxP5SZMCWdphGv .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-0iNxP5SZMCWdphGv .icon-shape p,#mermaid-svg-0iNxP5SZMCWdphGv .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-0iNxP5SZMCWdphGv .icon-shape .label rect,#mermaid-svg-0iNxP5SZMCWdphGv .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-0iNxP5SZMCWdphGv .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-0iNxP5SZMCWdphGv .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-0iNxP5SZMCWdphGv :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 远程 / VPN / 仅 22 端口
同一局域网
是
Win 也要访问
Mac 访问 Linux 目录
网络环境?
SSHFS
要极速?
NFS
SMB/Samba
开发: VS Code 编远端代码
| 场景 | 推荐 | 理由 |
|---|---|---|
| 远程 / VPN 办公 | SSHFS | SSH 加密;Linux 无需额外服务;防火墙常只需 22 |
| 局域网大文件 | NFS | 原生网络文件系统,开销小、速度快 |
| Windows + Mac 混用 | SMB | 兼容性最好,各端即插即用 |
| 维度 | SSHFS | NFS | SMB |
|---|---|---|---|
| Linux 端配置 | 无(已有 sshd) | nfs-kernel-server + exports |
Samba + smb.conf |
| 加密 | ✅ SSH | ❌ 明文(局域网) | 可选 |
| 典型端口 | 22 | 2049 | 445 |
| Mac 依赖 | macFUSE 或 FUSE-T | 系统内置 NFS 客户端 | Finder 内置 |
2. SSHFS(推荐)
SSHFS 通过 FUSE 把 SSH 连接当作传输管道,远端目录呈现为 Mac 本地挂载点。
2.1 安装概览
Mac 不原生支持 FUSE,需先装 macFUSE 或 FUSE-T ,再装 sshfs 客户端。详见 §3。
macFUSE 路线(装完 macFUSE 后):
bash
brew trust gromgit/fuse # 若提示 untrusted
brew install gromgit/fuse/sshfs-mac
# 冲突时:brew link --overwrite sshfs-mac
FUSE-T 路线:
bash
brew tap macos-fuse-t/homebrew-cask
brew install --cask fuse-t
brew install fuse-t-sshfs
# 命令为 fuse-t-sshfs,非 sshfs
2.2 挂载与卸载
bash
mkdir -p ~/mnt/linux_home
# macFUSE + sshfs
sshfs user@192.168.1.100:/home/user ~/mnt/linux_home \
-o volname=LinuxServer,defer_permissions
# FUSE-T(Sequoia 等新系统建议加 nonamedattr)
fuse-t-sshfs user@192.168.1.100:/home/user ~/mnt/linux_home \
-o volname=LinuxServer,nonamedattr
挂载成功后,Finder 侧边栏会出现该卷;~/mnt/linux_home 内即为 Linux 远端文件。
bash
umount ~/mnt/linux_home
# 卡死时
diskutil unmount force ~/mnt/linux_home
2.3 开发场景
用 SSHFS 挂载代码目录 (尽量避开 node_modules 等海量小文件目录),在 Mac 上用 VS Code 编辑,编译/运行仍在 Linux------本地 IDE 体验 + 远端环境一致。
3. macFUSE 与 FUSE-T 安装
3.1 路子一:macFUSE(成熟稳定,推荐多数用户)
gromgit/fuse/sshfs-mac 硬依赖 macFUSE,与 FUSE-T 不互通。
bash
brew install --cask macfuse
- 打开 系统设置 → 隐私与安全性 ,允许开发者 Benjamin Fleischer (弹窗也可能显示 OSXFUSE / FUSE for macOS)的系统软件。
- 重启(内核扩展需重启加载)。
- 再装
sshfs-mac(见 §2.1)。
3.2 路子二:FUSE-T(无传统 KEXT,追新)
bash
brew uninstall gromgit/fuse/sshfs-mac 2>/dev/null
brew untap gromgit/fuse 2>/dev/null
brew tap macos-fuse-t/homebrew-cask
brew install --cask fuse-t
brew install fuse-t-sshfs
| 对比 | macFUSE | FUSE-T |
|---|---|---|
| 内核扩展 | 有(需授权/重启) | 用户态为主 |
| sshfs 命令 | sshfs |
fuse-t-sshfs |
| 边缘兼容 | 最成熟 | 新系统需 nonamedattr 等参数 |
3.3 Ventura 及以后:系统扩展被阻止
若提示 「当前你的安全性设置阻止安装系统扩展」:
Apple Silicon(M 系列)
- 关机,长按电源至「启动选项」→ 选项 → 输入密码。
- 实用工具 → 启动安全性实用工具 → 选主磁盘 → 安全策略。
- 选 降低安全性 ,勾选 允许用户管理来自被认可开发者的内核扩展。
- 重启后重装 macFUSE,在 隐私与安全性 中点 允许。
Intel Mac :开机按住 Command + R 进入恢复模式,其余步骤类似。
4. sshfs 用法与参数
4.1 基本语法
bash
sshfs [用户]@[主机]:[远端目录] [本地挂载点]
示例:Linux /home/app/logs → Mac ~/mnt/server_logs:
bash
mkdir -p ~/mnt/server_logs
sshfs root@192.168.1.50:/home/app/logs ~/mnt/server_logs
首次连接输入 SSH 密码;配置 SSH 密钥后可免密挂载。
配置免密(Mac 端执行一次):
bash
# 若尚未有密钥
ssh-keygen -t ed25519 -C "mac@local"
ssh-copy-id user@host
# 之后 sshfs 挂载不再提示密码
4.2 实用参数
| 需求 | 命令示例 |
|---|---|
| 非 22 端口 | sshfs -p 2222 user@host:/path ~/mnt |
| 免密 | 见上文 ssh-keygen + ssh-copy-id |
| 减轻 Finder 卡顿 | sshfs user@host:/path ~/mnt -o local,volname=MyServer |
| 断线重连 | sshfs ... -o reconnect,ServerAliveInterval=15 |
| 权限映射 | sshfs ... -o uid=0,gid=0(慎用,见 §7) |
| GBK 中文 | sshfs ... -o modules=iconv,from_code=GBK,to_code=UTF-8 |
4.3 操作清单
| 操作 | 命令 |
|---|---|
| 挂载 | sshfs user@host:/remote ~/mnt |
| 卸载 | umount ~/mnt |
| 强制卸载 | diskutil unmount force ~/mnt |
| 查看状态 | `mount |
5. NFS(局域网)
适合同一网段、追求读写速度;Linux 端需安装并配置 NFS 服务。
5.1 Linux 端
bash
sudo apt install nfs-kernel-server
# /etc/exports --- insecure 供 macOS 客户端(高端口)使用
# 防重复追加:grep -q '/data/share' /etc/exports || echo '...' | sudo tee -a /etc/exports
echo "/data/share 192.168.1.0/24(rw,sync,insecure,no_subtree_check,no_root_squash)" \
| sudo tee -a /etc/exports
sudo exportfs -rav
sudo systemctl restart nfs-kernel-server
⚠️
no_root_squash允许远端 root 在 NFS 上保留完整 root 权限,开发机调试 方便,生产环境慎用 。生产建议root_squash,或改用all_squash,anonuid=...,anongid=...(见 §5.3)做 UID 映射。
5.2 Mac 端
bash
mkdir -p ~/mnt/nfs_share
sudo mount -t nfs -o resvport,rw 192.168.1.100:/data/share ~/mnt/nfs_share
卸载通常也需要 sudo(与挂载对称):
bash
sudo umount ~/mnt/nfs_share
# 失败时:diskutil unmount force ~/mnt/nfs_share
5.3 UID 权限坑
macOS 默认 UID 常从 501 起,Linux 用户常从 1000 起,直接挂载可能出现 Permission denied 。可在 Linux /etc/exports 增加映射(示例,按实际 Mac UID/GID 调整):
/data/share 192.168.1.0/24(rw,sync,insecure,all_squash,anonuid=501,anongid=20)
Mac 上查看 UID:id -u;主组 GID:id -g。
6. Samba / SMB
需 Windows 与 Mac 同时访问时最省事。
6.1 Linux 端
bash
sudo apt install samba
sudo smbpasswd -a your_username # 须为系统已有用户
# /etc/samba/smb.conf 末尾追加:
# [shared]
# path = /data/share
# browseable = yes
# writable = yes
# valid users = your_username
sudo systemctl restart smbd
6.2 Mac 端
Finder → 前往 → 连接服务器 (Cmd+K)→ 输入 smb://192.168.1.100/shared → 账号密码登录。
7. 常见问题
| 问题 | 原因 / 处理 |
|---|---|
sshfs-mac requires macFUSE |
先 brew install --cask macfuse 并授权重启,或改 FUSE-T 路线 |
brew trust gromgit/fuse |
未信任 tap 时执行 brew trust gromgit/fuse |
| Permission denied(sshfs) | Mac/Linux UID 不一致;-o uid=...,gid=... 或改 Linux 目录权限 |
| Permission denied(NFS) | exports 加 anonuid/anongid 或 all_squash |
| Finder 彩虹圈 | 加 -o local,volname=...;避免挂载海量小文件目录 |
| 中文乱码 | 老系统 GBK:modules=iconv,from_code=GBK,to_code=UTF-8 |
| Device busy 卸载失败 | diskutil unmount force ~/mnt |
| 系统扩展被阻止 | §3.3 恢复模式降低安全策略 |
8. 延伸阅读
| 资源 | 说明 |
|---|---|
| macFUSE | macOS FUSE 实现 |
| FUSE-T | 用户态 FUSE 替代方案 |
| SSHFS 手册 | 上游项目与参数 |
| Apple 启动安全性 | 系统扩展与恢复模式 |
落地时注意:SSHFS 走公网务必配 密钥登录 并限制 SSH;NFS 勿暴露公网;生产数据优先 VPN + SSHFS 而非明文 NFS。挂载前确认远端路径与磁盘配额,卸载后再断网,避免 FUSE 层 Device busy 导致本地目录假死。
收束 :Mac 挂载 Linux 目录没有单一「最优解」------能 SSH 就用 SSHFS ,局域网要快用 NFS ,要 Windows 同框用 SMB 。安装层分清 macFUSE + sshfs 与 FUSE-T + fuse-t-sshfs 两套栈;权限与系统扩展是 Mac 上最常卡关的两处,按 §3、§7 对照即可。
验证环境:macOS Sequoia 15.x(Apple Silicon)· Ubuntu 22.04 LTS · macFUSE 4.x / FUSE-T 1.x(具体小版本随 Homebrew 更新)