# macOS 命令

> 每个 barn mac 子命令的语法、选项与示例。

---

LLMS 索引： [llms.txt](/v1.5/zh/llms.txt)

---

本页列出每个 `barn mac` 子命令的语法、选项与示例。这些命令共用的虚拟机模型、网络、JSON 结果和失败原因定义在 [Mac 运行时参考](../../mac/)；第一次上手请看 [macOS 虚拟机](../../../start/macos/)。

## 通用规则 {#common-rules}

- **宿主**：需要运行 macOS 27 或更高版本的 Apple 芯片 Mac，安装版本匹配的 `Barn.app`，并以当前登录桌面的用户运行（见[运行要求](../../../start/macos/#requirements)）。每台 Mac 同时最多运行两台 macOS 虚拟机，启动第三台会以 `mac_vm_limit` 失败。
- **虚拟机名称**：名称可省略的命令会选择唯一已有的虚拟机，或选择 `mac1`；有多台虚拟机且没有 `mac1` 时，需要显式指定。`configure`、`recreate`、`destroy` 始终需要名称。`start` 与 `stop` 可传多个名称或 `--all`，两者不能同时使用。
- **容量单位**：`--memory` 与 `--disk` 即使写作 `GB` 也按二进制计算：`16G`、`16GiB`、`16GB` 都表示 16 GiB，裸数字表示字节。Linux 主机清单中的 `GB` 则是十进制。
- **确认**：`recreate` 与 `destroy` 会先列出将删除的内容，并要求输入命令名确认。`--yes` 或 `--force` 跳过确认，没有终端时必须传入；这两条命令都没有 `-y` 简写。需要从 Apple 下载 macOS 的命令（`up`、`setup`、`image update`）会先询问；`-y`/`--yes` 跳过询问，没有终端时必须传入。
- **状态**：虚拟机数据位于 `$BARN_HOME/mac`，与 Linux 主机清单分开。这些命令忽略 Linux 的 `BARN_IMAGE/BARN_ARCH/BARN_CPU/BARN_MEM/BARN_SCALE/BARN_DISK` 覆盖。

下列所有选项自 1.1.0 起提供。

## `barn mac ls` {#barn-mac-ls}

想知道现在有哪些虚拟机时先运行它：列出每台虚拟机的状态、地址、SSH 就绪情况、macOS 版本、资源与共享目录。

```text
barn mac ls [flags]
```

它读取本地记录并查询运行中的虚拟机，不会创建、下载或启动任何东西。不带子命令运行 `barn mac` 即执行 `ls`。JSON 结构见 [`ls` 输出](../../mac/#ls)。

别名：`list`、`status`、`st`。

除 `--help` 外没有局部选项，仍可使用全局输出选项。

```bash
barn mac ls
barn --json mac ls
```

## `barn mac up` {#barn-mac-up}

用 `up` 让虚拟机跑起来：第一次运行时创建虚拟机，之后直接启动，SSH 与免密 sudo 可用后返回。

```text
barn mac up [name] [flags]
```

默认名称为 `mac1`。Mac 上还没有准备好 macOS 基础镜像时，`up` 会在你确认后先从 Apple 下载恢复镜像并安装一次，之后的虚拟机从该基础镜像克隆，几秒即可完成。`--disk` 与已准备基础镜像的容量不同时，会先安装一个匹配容量的基础镜像。

创建参数只对新虚拟机生效。对已有虚拟机，`up` 只负责启动；参数与已记录的设置不同时以 `mac_configuration_conflict` 失败，`next:` 行会打印应改用的命令。`--share` 的名称默认取目录路径的最后一段，`~/` 会展开为你的主目录。

`--no-wait` 在虚拟机运行后立即返回。首次启动时客机可能仍在初始化；在使用 `ssh` 或 `exec` 之前，再运行一次 `barn mac up NAME`，等待 SSH 并完成初始化。

| 选项 | 含义 / 默认值 | 可用版本 |
|---|---|---|
| `--clipboard on\|off` | `on/off`、`true/false` 或 `yes/no`；默认开启，在桌面窗口聚焦时生效 | 1.1.0 |
| `--cpu int` | vCPU 数，默认 4；最少 2，最多为宿主逻辑 CPU 数 | 1.1.0 |
| `--disk size` | 基础镜像容量，最小 32 GiB；默认沿用已准备的基础镜像，首次为 100 GiB | 1.1.0 |
| `--ipsw string` | 用这个恢复镜像安装 macOS，不从 Apple 下载 | 1.1.0 |
| `--memory size` | 内存，默认 8 GiB；须为整 MiB，范围为 4 GiB 至宿主内存 | 1.1.0 |
| `-n, --no-wait` | 虚拟机运行后即返回；首次启动可能仍在初始化 | 1.1.0 |
| `--open` | 虚拟机运行后立即显示桌面 | 1.1.0 |
| `--share [name=]path[:ro]` | 可重复：`[name=]path[:ro\|:rw]`；默认可写，挂载在 `/Volumes/My Shared Files` 下 | 1.1.0 |
| `--subnet string` | 规范的私有 `/24`，或 `auto`；默认选择第一个空闲网段 | 1.1.0 |
| `--user string` | 客机管理员；默认取宿主用户名，该名称无效时用 `barn` | 1.1.0 |
| `-y, --yes` | 不经询问直接从 Apple 下载 macOS（无终端时必需） | 1.1.0 |
{.wide .expand}

```bash
barn mac up
barn mac up dev --cpu 8 --memory 16G --share ~/src --open
barn mac up build --yes --no-wait
```

## `barn mac start` {#barn-mac-start}

用 `start` 启动已经完成首次 `up` 的虚拟机。它会等待 SSH，不会创建虚拟机，也不会下载任何东西。

```text
barn mac start [name...] [flags]
```

传名称或 `--all`，两者不能同时使用。尚未完成首次启动的虚拟机会以 `mac_not_initialized` 失败：如果它正在运行，用 `barn mac open NAME` 查看首次启动过程；如果已停止，用 `barn mac up NAME --open` 继续。`--recovery` 启动进入 macOS Recovery 并显示桌面，不等待 SSH。

| 选项 | 含义 / 默认值 | 可用版本 |
|---|---|---|
| `--all` | 启动全部虚拟机 | 1.1.0 |
| `-n, --no-wait` | 虚拟机运行后即返回，不等待 SSH | 1.1.0 |
| `--open` | 虚拟机运行后立即显示桌面 | 1.1.0 |
| `--recovery` | 启动进入 macOS Recovery 并显示桌面 | 1.1.0 |
{.wide .expand}

```bash
barn mac start mac1
barn mac start --all
barn mac start mac1 --recovery
```

## `barn mac stop` {#barn-mac-stop}

用 `stop` 通过 macOS 正常关闭虚拟机，磁盘和设置都保持不变。

```text
barn mac stop [name...] [flags]
```

两分钟后仍未停止的虚拟机会被断电，结果中会注明（`"forced": true`）。`--force` 立即断电，相当于长按电源键，客机中未保存的工作会丢失。

| 选项 | 含义 / 默认值 | 可用版本 |
|---|---|---|
| `--all` | 停止全部虚拟机 | 1.1.0 |
| `--force` | 立即断电，不请求正常关机；客机中未保存的工作会丢失 | 1.1.0 |
{.wide .expand}

```bash
barn mac stop dev
barn mac stop --all
barn mac stop dev --force
```

## `barn mac restart` {#barn-mac-restart}

用 `restart` 让 `configure` 保存的设置生效，或重启运行异常的客机。

```text
barn mac restart [name] [flags]
```

它先正常关闭虚拟机，再重新启动。最多接受一个名称，没有 `--all`。

| 选项 | 含义 / 默认值 | 可用版本 |
|---|---|---|
| `-n, --no-wait` | 虚拟机运行后即返回，不等待 SSH | 1.1.0 |
| `--open` | 虚拟机运行后立即显示桌面 | 1.1.0 |
{.wide .expand}

```bash
barn mac restart dev
barn mac restart dev --open
```

## `barn mac configure` {#barn-mac-configure}

用 `configure` 修改虚拟机的 CPU、内存、共享目录、网络或剪贴板设置，不影响磁盘和账号。

```text
barn mac configure <name> [flags]
```

CPU、内存、共享目录和网段只能在停机时修改，否则命令以 `mac_running` 失败；剪贴板设置随时可改。保存的配置（包括剪贴板变更）在下次启动时生效；要在当前会话中切换剪贴板共享，请使用菜单栏。

省略的选项保留原值。`--share` 添加共享，或替换同名共享；`--unshare` 移除共享。`--subnet auto` 把虚拟机迁到第一个空闲网段，`mac_subnet_in_use` 就用它解决。

| 选项 | 含义 / 默认值 | 可用版本 |
|---|---|---|
| `--clipboard on\|off` | `on/off`、`true/false` 或 `yes/no`；省略时保留原设置 | 1.1.0 |
| `--cpu int` | vCPU 数，最少 2，最多为宿主逻辑 CPU 数；省略时保留当前值 | 1.1.0 |
| `--memory size` | 内存，须为整 MiB，范围为 4 GiB 至宿主内存；省略时保留当前值 | 1.1.0 |
| `--share [name=]path[:ro]` | 添加或替换同名共享；可重复，默认可写，可加 `:ro` 或 `:rw` | 1.1.0 |
| `--subnet string` | 迁到指定的私有 `/24` 网段，或用 `auto` 选第一个空闲网段 | 1.1.0 |
| `--unshare stringArray` | 停止共享指定名称的目录（可重复） | 1.1.0 |
{.wide .expand}

```bash
barn mac stop dev
barn mac configure dev --cpu 8 --memory 16G --share ~/src
barn mac configure dev --unshare src --clipboard off
barn mac start dev
```

## `barn mac recreate` {#barn-mac-recreate}

需要一块从基础镜像重新克隆的干净磁盘、同时保留虚拟机名称和设置时，用 `recreate`；加 `--update` 则把虚拟机换到最新准备好的 macOS。

> [!WARNING]
> `recreate` 会擦除虚拟机磁盘，客机里的文件、应用和设置全部丢失；宿主上的共享目录不受影响。请先把需要的内容从客机复制出来。

```text
barn mac recreate <name> [flags]
```

新虚拟机保留名称、账号、CPU、内存、磁盘容量、共享目录、剪贴板设置和地址，同时获得新的登录密码和新的 SSH 密钥。

`--update` 克隆 `barn mac image update` 准备的默认基础镜像，并沿用虚拟机自己的磁盘容量。如果新 macOS 版本只准备了其他容量的基础镜像，请先准备匹配容量，例如 `barn mac setup --disk 200G`。

| 选项 | 含义 / 默认值 | 可用版本 |
|---|---|---|
| `--force` | 跳过确认（无终端时必需） | 1.1.0 |
| `-n, --no-wait` | 虚拟机运行后即返回，不等待 SSH | 1.1.0 |
| `--update` | 使用最新准备好的 macOS 基础镜像 | 1.1.0 |
| `--yes` | 等同于 `--force`；没有 `-y` 简写 | 1.1.0 |
{.wide .expand}

```bash
barn mac recreate dev
barn mac image update --yes
barn mac recreate dev --update --yes
```

## `barn mac destroy` {#barn-mac-destroy}

不再需要某台虚拟机时，用 `destroy` 删除它。

> [!WARNING]
> `destroy` 会将每台指定的虚拟机断电，并删除其磁盘、SSH 密钥、密码和设置。共享的 macOS 基础镜像、其他虚拟机以及宿主上的共享目录都会保留。

```text
barn mac destroy <name...> [flags]
```

必须显式指定名称。之后可运行 `barn mac image prune` 清理不再被任何虚拟机使用的基础镜像。

| 选项 | 含义 / 默认值 | 可用版本 |
|---|---|---|
| `--force` | 跳过确认（无终端时必需） | 1.1.0 |
| `--yes` | 等同于 `--force`；没有 `-y` 简写 | 1.1.0 |
{.wide .expand}

```bash
barn mac destroy dev
barn mac destroy mac1 mac2 --yes
```

## `barn mac open` {#barn-mac-open}

用 `open` 查看和操作虚拟机桌面；虚拟机已停止时会先启动它。

```text
barn mac open [name] [flags]
```

关闭窗口后虚拟机继续在后台运行；窗口聚焦时共享剪贴板。正在运行但尚未完成首次启动的虚拟机可以在这里观察；已停止的则用 `barn mac up NAME --open`。

除 `--help` 外没有局部选项，仍可使用全局输出选项。

```bash
barn mac open
barn mac open dev
```

## `barn mac ssh` {#barn-mac-ssh}

用 `ssh` 在虚拟机里打开交互式 shell，或执行一条 shell 命令行。

```text
barn mac ssh [flags] [name] [-- command...]
```

不带命令时，`ssh` 打开交互会话。`--` 之后的命令会像普通 `ssh` 那样拼成一行交给客机 shell，并返回其退出码。参数规则见 [SSH 与 exec](../../mac/#ssh-and-exec)。

除 `--help` 外没有局部选项，仍可使用全局输出选项。

```bash
barn mac ssh dev
barn mac ssh dev -- 'sw_vers; id'
```

## `barn mac exec` {#barn-mac-exec}

在脚本中用 `exec`：它按原样传入参数运行一个程序，并返回该程序的退出码。

```text
barn mac exec [flags] [name] -- command...
```

必须在 `--` 之后提供命令。管道等 shell 表达式请显式使用 `sh -c`。加 `--json` 时，结果包含 `exit_code`、`stdout` 和 `stderr`。参数规则见 [SSH 与 exec](../../mac/#ssh-and-exec)。

除 `--help` 外没有局部选项，仍可使用全局输出选项。

```bash
barn mac exec dev -- sw_vers
barn mac exec dev -- printf '<%s>\n' 'a b' ''
barn --json mac exec dev -- sh -c 'echo out; exit 3'
```

## `barn mac password` {#barn-mac-password}

客机桌面在锁屏或管理员授权时要求输入登录密码，就用 `password` 查看。

```text
barn mac password [name] [flags]
```

密码与虚拟机的 SSH 私钥存放在一起，受同样的保护。`--copy` 把密码放进剪贴板，不在终端打印。

| 选项 | 含义 / 默认值 | 可用版本 |
|---|---|---|
| `-c, --copy` | 复制到剪贴板，不打印 | 1.1.0 |
{.wide .expand}

```bash
barn mac password dev
barn mac password dev --copy
```

## `barn mac ssh-config` {#barn-mac-ssh-config}

用 `ssh-config` 让 `ssh`、`scp`、`rsync` 和编辑器按名称连接每台虚拟机，并使用各自的私钥与固定主机密钥。

```text
barn mac ssh-config [flags]
```

不带选项时打印 OpenSSH 配置片段。第一台就绪的虚拟机会把该片段以一条 `Include` 安装到 `~/.ssh/config`，之后由生命周期命令保持更新。`--remove` 只删除 Barn 安装的内容，并保持移除状态，直到运行 `--install`。如果 `~/.ssh/config` 已把某个名称用于其他主机，该虚拟机只按地址发布。如果 `~/.ssh/config` 是符号链接或硬链接，命令以 `ssh_config_linked` 失败，并打印需要你手动加入的 `Include` 行。

`--install` 与 `--remove` 互斥。此命令不接受虚拟机名称。

| 选项 | 含义 / 默认值 | 可用版本 |
|---|---|---|
| `-i, --install` | 以一条 `Include` 将条目安装到 `~/.ssh/config` | 1.1.0 |
| `--remove` | 移除 Barn 安装的条目 | 1.1.0 |
{.wide .expand}

```bash
barn mac ssh-config
barn mac ssh-config --install
barn mac ssh-config --remove
```

## `barn mac logs` {#barn-mac-logs}

虚拟机无法启动、网络中断或关不了机时，用 `logs` 查看其运行日志的最后几行，其中包括 Apple Virtualization 报错。

```text
barn mac logs [name] [flags]
```

`--lines`/`-n` 必须为正数，默认 100。这里的 `-n` 表示行数，不是跳过等待。没有跟随输出的选项。

| 选项 | 含义 / 默认值 | 可用版本 |
|---|---|---|
| `-n, --lines int` | 读取最近的行数（默认 100） | 1.1.0 |
{.wide .expand}

```bash
barn mac logs dev
barn mac logs dev --lines 200
```

## `barn mac setup` {#barn-mac-setup}

用 `setup` 提前准备 macOS 基础镜像，例如在初始化脚本中运行，这样第一次 `up` 只需克隆。

```text
barn mac setup [flags]
```

它在你确认后从 Apple 下载 macOS，或读取 `--ipsw` 指定的文件，然后安装一次，生成所有虚拟机共用的、从未启动过的基础镜像。没有准备好的基础镜像时，`up` 也会自动完成这一步。

| 选项 | 含义 / 默认值 | 可用版本 |
|---|---|---|
| `--disk size` | 基础镜像容量，最小 32 GiB。默认沿用当前基础镜像的容量，首个基础镜像默认为 100 GiB | 1.1.0 |
| `--ipsw string` | 用这个恢复镜像安装 macOS，不从 Apple 下载 | 1.1.0 |
| `-y, --yes` | 不经询问直接从 Apple 下载 macOS（无终端时必需） | 1.1.0 |
{.wide .expand}

```bash
barn mac setup --yes
barn mac setup --ipsw ~/Downloads/restore.ipsw --disk 200G
```

## `barn mac image ls` {#barn-mac-image-ls}

用 `image ls` 查看哪些恢复镜像和基础镜像占用了磁盘，以及哪些虚拟机在使用它们。

```text
barn mac image ls [flags]
```

它列出每个恢复镜像和基础镜像的状态与磁盘占用。该命令只读取本地状态，不启动原生 runner。

别名：`list`。

除 `--help` 外没有局部选项，仍可使用全局输出选项。

```bash
barn mac image ls
barn --json mac image ls
```

## `barn mac image update` {#barn-mac-image-update}

用 `image update` 换到 Apple 最新的 macOS 27 版本。

```text
barn mac image update [flags]
```

它向 Apple 查询最新的 macOS 27 恢复镜像，经你确认后下载，并安装为新虚拟机的默认基础镜像。已有虚拟机继续使用原来的基础镜像，直到你运行 `barn mac recreate NAME --update`。

| 选项 | 含义 / 默认值 | 可用版本 |
|---|---|---|
| `--ipsw string` | 使用这个恢复镜像，不从 Apple 下载 | 1.1.0 |
| `-y, --yes` | 不经询问直接下载（无终端时必需） | 1.1.0 |
{.wide .expand}

```bash
barn mac image update --yes
barn mac image ls
```

## `barn mac image prune` {#barn-mac-image-prune}

用 `image prune` 回收没有任何虚拟机使用的基础镜像所占的磁盘空间。

```text
barn mac image prune [flags]
```

它列出当前默认基础镜像以外、未被使用的基础镜像。`--installers` 同时列出已下载的恢复镜像和未完成的下载。不传 `--yes` 不会删除任何内容。

| 选项 | 含义 / 默认值 | 可用版本 |
|---|---|---|
| `--installers` | 同时纳入已下载的恢复镜像和未完成的下载 | 1.1.0 |
| `-y, --yes` | 删除列出的未使用镜像；省略时只列出候选项 | 1.1.0 |
{.wide .expand}

```bash
barn mac image prune
barn mac image prune --installers --yes
```

## `barn mac doctor` {#barn-mac-doctor}

出问题时先运行 `doctor`；安装或升级 Barn 之后也运行一次。

```text
barn mac doctor [flags]
```

它检查 macOS 与 Apple 芯片支持、Mac 组件签名、可用磁盘空间、已准备的基础镜像，以及每台虚拟机的文件、密钥、共享目录、网络和 SSH。它不修改也不启动任何东西，任一检查失败时以 `3` 退出。

除 `--help` 外没有局部选项，仍可使用全局输出选项。

```bash
barn mac doctor
barn --json mac doctor
```

## 实现依据 {#implementation}

[command_mac.go](https://github.com/pgsty/barn/blob/v1.4.0/cmd/barn/command_mac.go)
