# Guest Time Synchronization

> Measure Linux guest clocks against the host, correct them, and enable optional scheduled checks.

---

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

---

While the host sleeps or a VM is paused, the guest does not run, so its clock falls behind the host's; in an offline lab, guest NTP has no source to correct it. `barn time` measures each Linux guest against the host clock and sets it over management SSH, so it works without guest Internet access. Barn checks clocks on demand with `barn time sync`, when `up`, `start`, `restart`, `reload`, or `recreate` brings guests to ready, and, if you opt in, every minute from an OS timer.

The guest side needs Python 3.6 or later and passwordless `sudo`. `barn time` is new in 1.3.0 ([version baseline](/v1.5/docs/reference/commands/#versions)), acts on the deployment that `BARN_HOME` selects, and accepts the [global options](/v1.5/docs/reference/commands/#global-options).

## Measure and correct clocks {#commands}

```bash
barn time status
barn --json time status meta node-1
barn time sync
barn time sync meta node-1 --timeout 30s
```

`status` measures; `sync` sets each guest clock to the host's, forward or backward, and measures again. Without node names, both cover the whole applied deployment and skip stopped, exited, and uncreated nodes, which is not a failure. Results show the offset (positive means the guest is ahead), its uncertainty (half the round trip), and the guest's NTP synchronization state, service, and source before any change.

A backward step moves application wall time backward too, so choose the moment deliberately when time-sensitive workloads run in the guest.

| Command | Option | Default | Meaning | Available in |
|---|---|---|---|---|
| `time status [node...]` | `--timeout` | `1m` | Total deadline, from `1s` to `10m` | 1.3.0 |
| `time sync [node...]` | `--timeout` | `1m` | Total deadline, from `1s` to `10m` | 1.3.0 |
{.wide .expand}

Barn checks the VM process, the instance SSH identity, and the guest readiness marker, takes three samples, and uses the one with the shortest round trip, compensating for transport time with the guest's monotonic clock. A measurement fails if all three round trips exceed 250 ms, and a `sync` fails if more than 500 ms of offset remains afterwards. Nodes fail independently, each with its own error, and any failure makes the exit code nonzero.

## Startup checks {#startup}

`up`, `start`, `restart`, `reload`, and `recreate` check the guests they bring to ready and correct offsets above two seconds before returning, so a `barn provision` that follows sees the corrected time; services already enabled in the guest may start earlier during boot. A failed check does not fail the command: it appears in the lifecycle result's top-level `warnings[]` with code `time_sync`, and its `next` suggests `barn time sync` for the affected nodes. `--no-wait` skips the check.

## Scheduled checks {#auto}

Enable the timer when the host sleeps often or VMs stay paused between sessions:

```bash
barn time auto enable
barn time auto status
barn --json time auto status
barn time auto disable
```

`enable` installs a timer for the invoking Unix user that records the executable path, `BARN_HOME`, and `PATH`. Run it from an installed binary, and again after moving Barn or switching `BARN_HOME`. The timer checks every running Linux guest of the recorded `BARN_HOME`, whatever the current directory or inventory.

| Host | Scheduler | Job files |
|---|---|---|
| macOS | launchd calendar job | `~/Library/LaunchAgents/com.pgsty.barn.time.plist` |
| Linux | systemd user timer and oneshot service | `${XDG_CONFIG_HOME:-~/.config}/systemd/user/barn-time-sync.{timer,service}` |

A check runs every minute for at most 45 seconds, corrects offsets above two seconds, and skips the deployment while another `barn` command holds its lock. Missed checks run after the host resumes, but the timer never wakes the host, and it needs the user's launchd session or systemd user manager to be running.

If drift returns within ten minutes of a correction while the guest ran continuously and its NTP reports synchronized, the guest's NTP source disagrees with the host. Barn reports `source-conflict` and stops correcting that node automatically, so the two do not keep pulling the clock apart. Host sleep, VM pause, and guest reboots do not count; the guest's monotonic clock tells them apart. To clear the conflict, align the guest's NTP source with the host or run `barn time sync`; an offset back within the threshold or a guest reboot also clears it.

`status` shows whether the job files are installed and the scheduler has them active, plus the latest result from `$BARN_HOME/time-last.json`, which each run replaces. Correction history lives in `$BARN_HOME/time-sync.json`; Barn prunes it when nodes or deployments are destroyed, and `barn purge` removes it with `time-last.json`. `disable` stops the timer and removes its job files; manual and startup checks keep working.

## Host NTP and offline Pigsty {#ntp}

`barn time` corrects the clock but gives the guest no NTP source, so Pigsty's `node_timex_sync_status` need not become `1`. For continuous synchronization in an offline lab, serve time from the host: macOS ships `sntpd`, disabled by default, and Linux can use chronyd. Let the server fall back to its local clock when no upstream source is reachable, and allow only the lab network. Pigsty rewrites the Chrony configuration when it deploys NODE, so point the guests at the host's `.1` address on the lab `/24` through `node_ntp_servers` in the Pigsty inventory. Once Chrony keeps the guests synchronized, you can disable the Barn timer and keep `barn time sync` for manual corrections.

## Implementation {#implementation}

[command_time.go](https://github.com/pgsty/barn/blob/v1.4.0/cmd/barn/command_time.go) · [timesync](https://github.com/pgsty/barn/tree/v1.4.0/internal/timesync) · [timejob](https://github.com/pgsty/barn/tree/v1.4.0/internal/timejob)
