# Guest Access and Hosts

> Open a shell on Linux guests, run commands and scripts, read logs, and publish SSH aliases and /etc/hosts names.

---

LLMS index: [llms.txt](/v1.5/llms.txt)

---

These commands reach running Linux guests from the host: `ssh` and `exec` run a shell or one command, `provision` streams a script to several nodes, and `logs` shows what a node printed. `ssh-config` and `hosts` publish node names so other host tools can reach the nodes too. Every command accepts the [global options](/v1.5/docs/reference/commands/#global-options); **Available in** shows availability from the 1.1.0 baseline onward ([version baseline](/v1.5/docs/reference/commands/#versions)).

## Remote commands and `--` {#remote-commands}

`barn ssh` and `barn exec` share one argument rule: Barn flags and at most one node name, then `--`, then the remote command. They differ in what the guest receives:

```text
barn ssh  [node] [-- command...]   # words joined into one line for the guest shell
barn exec [node] -- command...     # program and arguments passed separately
```

| | `barn ssh meta -- …` | `barn exec meta -- …` |
|---|---|---|
| Guest receives | One string, parsed by the guest shell, as with plain `ssh` | The program and its arguments, with no shell |
| Pipes, `;`, `$VAR` | Work; quote them so your local shell leaves them alone | Wrap the command in `sh -c '…'` |
| Spaces, quotes, empty arguments | Re-parsed by the guest shell | Preserved exactly |
| No command | Opens an interactive login shell | Fails: `exec` requires `--` and a command |
{.wide}

- **Before `--`**: Barn flags such as `--json`, plus an optional node name. It must be a resolved node name, not an IP address, alias, or group; an unknown name exits `2`. Without one, Barn picks the prepared control node, or else the first prepared node.
- **After `--`**: everything goes to the guest, including a second `--` and anything that looks like a Barn or OpenSSH flag. The first word must not be empty. For compatibility, `exec` also accepts one quoted string and hands it to the guest shell.
- **Exit status**: Barn exits with the remote command's status, so `exit 17` in the guest makes `barn` exit `17`. Check whether a nonzero code came from the guest before reading it as a Barn [error class](../../cli/#exit-codes).

```bash
barn ssh meta -- 'df -h /data; id'           # a shell line, quoted like plain ssh
barn exec meta -- printf '<%s>\n' 'a b' ''   # 'a b' stays one argument; '' is kept
barn exec meta -- sh -c 'uptime; id'         # shell features through sh -c
```

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

```text
barn ssh [flags] [node] [-- command...]
```

Open an interactive shell on a node, or run a shell line there ([argument rules](#remote-commands)). With `--json`, session output goes to stderr and the final report to stdout.

No options of its own.

```bash
barn ssh
barn ssh meta
barn ssh meta -- 'df -h /data; id'
```

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

```text
barn exec [flags] [node] -- command...
```

Run one command on a node with no shell in between ([argument rules](#remote-commands)); scripts and agents should use this form. Text mode passes the guest's output and exit status straight through. With `--json`, the report has `success`, `exit_code`, `stdout`, and `stderr`.

Aliases: `ex`. No options of its own.

```bash
barn exec meta -- hostname
barn exec meta -- printf '<%s>\n' 'a b' ''
barn --json exec meta -- sh -c 'echo out; exit 17'
```

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

```text
barn logs [node] [flags]
```

Read a node's log; start here when a node does not become ready ([troubleshooting](../../../start/troubleshooting/#a-node-did-not-become-ready)). `--source serial` (the default) reads the guest serial console and `qemu` reads QEMU diagnostics, with the QEMU argv shown only under `--verbose`. Both take one node and default to the prepared control node, or else the first prepared node. `events` reads the deployment-wide, bounded Barn event log, including `setup`; it takes no node and works before any deployment exists.

`--follow` keeps streaming: raw bytes in text mode, NDJSON with `--json`, and a YAML document stream with `--yaml`. Here `-f` means `--follow`, not `--file`.

Aliases: `l`.

| Option | Meaning / default | Available in |
|---|---|---|
| `-f, --follow` | continue streaming appended log data | 1.1.0 |
| `-s, --source string` | log source: serial, qemu, or events (default "serial") | 1.1.0 |
{.wide .expand}

```bash
barn logs meta
barn logs meta --source qemu
barn logs --source events --follow
barn --json logs meta --follow
```

## `barn provision` {#barn-provision}

```text
barn provision [node...] [flags]
```

Run a local Bash script on running nodes, for example to bootstrap or check several at once; with no node names, it runs on all of them. Barn streams the script over the deployment's verified SSH connection, so no copy stays on the guest, and records its SHA-256. It never creates or starts VMs.

`--script` is required. Nodes run one at a time unless you raise `--parallel`, which must be positive: unlike lifecycle commands, `provision` rejects `0`. `--timeout` bounds the whole operation. `--sudo` runs the script through `sudo -n`, so the guest user needs passwordless `sudo`, as official images provide. `--json` returns `script.sha256` and per-node `results[]` with `node`, `success`, `exit_code`, `stdout`, and `stderr`; if only some nodes fail, the exit code is `5`.

| Option | Meaning / default | Available in |
|---|---|---|
| `-p, --parallel int` | Positive concurrency limit; default 1; zero is invalid | 1.1.0 |
| `-s, --script string` | local Bash script to stream to each selected guest | 1.1.0 |
| `--sudo` | run the guest script through sudo -n | 1.1.0 |
| `-t, --timeout duration` | hard deadline for the operation (default 1h0m0s) | 1.1.0 |
{.wide .expand}

```bash
barn provision --script ./check.sh meta
barn provision --script ./bootstrap.sh --sudo --parallel 2 --timeout 30m
```

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

```text
barn ssh-config [node...] [flags]
```

Print the deployment's OpenSSH config fragment, which lets plain `ssh`, `scp`, and other OpenSSH tools reach nodes by name (`ssh meta`). Lifecycle commands such as `barn up` keep the default fragment, `~/.ssh/barn_config`, current, so you rarely need this by hand. Use it to inspect the fragment, to limit it to some nodes, or to install another one with `--name`.

`--install` writes the fragment and adds one `Include` line to `~/.ssh/config`. `--remove` removes only the fragment and `Include` installed under that name; it needs no deployment, takes no node names, and cannot be combined with `--install`. If `~/.ssh/config` cannot be edited safely, for example because it is a symlink, Barn writes the fragment and prints the `Include` line for you to add.

Aliases: `sc`.

| Option | Meaning / default | Available in |
|---|---|---|
| `-i, --install` | install the deployment fragment and one Include in ~/.ssh/config | 1.1.0 |
| `--name string` | SSH Host and fragment prefix (default "barn") | 1.1.0 |
| `--remove` | remove the Include and fragment Barn installed | 1.1.0 |
{.wide .expand}

```bash
barn ssh-config
barn ssh-config --install meta
barn ssh-config --install --name lab
barn ssh-config --remove --name lab
```

## `barn hosts install` {#barn-hosts-install}

```text
barn hosts install [flags]
```

SSH aliases help only OpenSSH tools. To let browsers, `psql`, and other host programs resolve node names and aliases, publish them in a Barn-owned block of `/etc/hosts`; `barn up` does not do this.

By default, the block lists the applied nodes' names and aliases. Since 1.2.0, `-f pigsty.yml` publishes the inventory's nodes plus `all.vars.vm_hosts` instead, even before the VMs exist ([local service names](../../configuration/#local-hosts)). Each run replaces the whole block, so a later run without `-f` drops names that came only from the inventory.

On a terminal, Barn shows the change and asks `Apply this /etc/hosts change? [Y/n]`. Without a terminal, or with `--json` or `--yaml`, it only prints the plan; `--yes` applies it without asking, though `sudo` may still want a password. Barn edits the file through a root-owned helper, `/opt/barn/libexec/barn-hosts-helper`, which it installs on first use and verifies by digest. A block that is already current needs no privilege.

Aliases: `i`.

| Option | Meaning / default | Available in |
|---|---|---|
| `-f, --file string` | Inventory nodes, aliases and vm_hosts; usable before VM creation | 1.2.0 |
| `-y, --yes` | apply the displayed privileged plan without confirmation (sudo may still ask for a password) | 1.1.0 |
{.wide .expand}

```bash
barn hosts install --json
barn hosts install --yes
barn hosts install -f pigsty.yml --yes
```

## `barn hosts uninstall` {#barn-hosts-uninstall}

```text
barn hosts uninstall [flags]
```

Remove the Barn-owned block from `/etc/hosts`, keeping every other line. It needs no inventory or deployment. Confirmation works as for `hosts install`, except that the terminal question, `Remove the Barn block from /etc/hosts? [y/N]`, defaults to no.

Aliases: `u`.

| Option | Meaning / default | Available in |
|---|---|---|
| `-y, --yes` | apply the displayed privileged plan without confirmation (sudo may still ask for a password) | 1.1.0 |
{.wide .expand}

```bash
barn hosts uninstall --json
barn hosts uninstall --yes
```

## Implementation {#implementation}

[command_root.go](https://github.com/pgsty/barn/blob/v1.4.0/cmd/barn/command_root.go) · [command_access.go](https://github.com/pgsty/barn/blob/v1.4.0/cmd/barn/command_access.go) · [main.go](https://github.com/pgsty/barn/blob/v1.4.0/cmd/barn/main.go)
