Guest Access and Hosts
7 minute read
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; Available in shows availability from the 1.1.0 baseline onward (version baseline).
Remote commands and --
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:
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 |
- 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 exits2. 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,execalso accepts one quoted string and hands it to the guest shell. - Exit status: Barn exits with the remote command’s status, so
exit 17in the guest makesbarnexit17. Check whether a nonzero code came from the guest before reading it as a Barn error class.
barn ssh
Open an interactive shell on a node, or run a shell line there (argument rules). With --json, session output goes to stderr and the final report to stdout.
No options of its own.
barn exec
Run one command on a node with no shell in between (argument rules); 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.
barn logs
Read a node’s log; start here when a node does not become ready (troubleshooting). --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 |
barn provision
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 |
barn ssh-config
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 |
barn hosts install
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). 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 |
barn hosts uninstall
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 |
Implementation
command_root.go · command_access.go · main.go