Skip to content

Guest Access and Hosts

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

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  [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 receivesOne string, parsed by the guest shell, as with plain sshThe program and its arguments, with no shell
Pipes, ;, $VARWork; quote them so your local shell leaves them aloneWrap the command in sh -c '…'
Spaces, quotes, empty argumentsRe-parsed by the guest shellPreserved exactly
No commandOpens an interactive login shellFails: 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 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.
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 [flags] [node] [-- command...]

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 ssh
barn ssh meta
barn ssh meta -- 'df -h /data; id'

barn exec

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

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 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 [node] [flags]

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.

OptionMeaning / defaultAvailable in
-f, --followcontinue streaming appended log data1.1.0
-s, --source stringlog source: serial, qemu, or events (default “serial”)1.1.0
barn logs meta
barn logs meta --source qemu
barn logs --source events --follow
barn --json logs meta --follow

barn provision

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.

OptionMeaning / defaultAvailable in
-p, --parallel intPositive concurrency limit; default 1; zero is invalid1.1.0
-s, --script stringlocal Bash script to stream to each selected guest1.1.0
--sudorun the guest script through sudo -n1.1.0
-t, --timeout durationhard deadline for the operation (default 1h0m0s)1.1.0
barn provision --script ./check.sh meta
barn provision --script ./bootstrap.sh --sudo --parallel 2 --timeout 30m

barn ssh-config

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.

OptionMeaning / defaultAvailable in
-i, --installinstall the deployment fragment and one Include in ~/.ssh/config1.1.0
--name stringSSH Host and fragment prefix (default “barn”)1.1.0
--removeremove the Include and fragment Barn installed1.1.0
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 [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). 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.

OptionMeaning / defaultAvailable in
-f, --file stringInventory nodes, aliases and vm_hosts; usable before VM creation1.2.0
-y, --yesapply the displayed privileged plan without confirmation (sudo may still ask for a password)1.1.0
barn hosts install --json
barn hosts install --yes
barn hosts install -f pigsty.yml --yes

barn hosts uninstall

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.

OptionMeaning / defaultAvailable in
-y, --yesapply the displayed privileged plan without confirmation (sudo may still ask for a password)1.1.0
barn hosts uninstall --json
barn hosts uninstall --yes

Implementation

command_root.go · command_access.go · main.go

Table reader