# Guest Time Synchronization

> Compare Linux guest clocks with the host and enable optional scheduled corrections.

---

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

---

Barn 1.3.0 introduced `barn time` to measure and synchronize Linux guest clocks. This page covers Barn 1.4.0. All time commands accept the global options. [Version history](/docs/reference/commands/#versions) · [Global options](/docs/reference/commands/#global-options)

Barn uses the host wall clock as the reference and operates through management SSH, including when guests have no Internet access. Official Linux images provide the required Python 3 and passwordless sudo. The helper supports Python 3.6 and later. These commands apply to Linux guests in the deployment selected by `BARN_HOME`.

## 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
```

Omitted node names select the whole applied deployment. Stopped, uncreated, and exited nodes (after runtime state convergence under the deployment lock) are skipped. Positive offsets mean the guest is ahead of the host. Results include offset, half-round-trip uncertainty, and NTP synchronization, service, and source sampled before adjustment.

`sync` forces a correction in either direction. A backwards correction moves application wall time backwards; choose the timing deliberately for time-sensitive workloads. Barn checks the VM process, instance SSH identity and guest readiness marker, then samples three times after authentication. It uses the shortest round trip, compensates transport time with the guest monotonic clock, and measures again after setting the clock.

Measurements fail if all round trips exceed 250ms; post-adjustment verification fails if the remaining offset exceeds 500ms. Independent nodes continue after one fails. Failures produce per-node errors and a nonzero exit code; skipping stopped nodes is successful.

| 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}

All commands accept [global presentation options](/docs/reference/commands/#global-options), including `--json` and `--yaml`.

## Startup checks {#startup}

Startup commands such as `up`, `start`, and `restart` check selected ready guests before returning and correct offsets greater than two seconds. Failures appear in top-level lifecycle `warnings[]` with code `time_sync`; management SSH readiness retains its normal meaning. `--no-wait` skips the check. A subsequent `barn provision` therefore follows the clock check, although services already enabled in the guest may start earlier during boot.

## Scheduled checks {#auto}

Use an installed binary or another persistent executable path:

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

Enable records the executable path, `BARN_HOME`, and `PATH`. Re-run it after moving Barn or selecting a different data root. One opt-in timer belongs to the invoking Unix user and checks every running Linux guest independently of 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}` |

Checks run every minute; calendar scheduling catches missed events after resume without waking the host itself. The corresponding user session/manager must be running. Each invocation has a 45-second budget and exits afterwards. Busy deployments are skipped until a later check. The deployment lock prevents overlapping corrections and lifecycle operations.

Automatic checks correct offsets above two seconds. If drift returns during continuous guest execution within ten minutes of a correction while guest NTP reports synchronization, Barn reports `source-conflict` and pauses automatic correction for that node. Host sleep, VM suspension, and guest reboots are distinguished using the guest monotonic clock so repeated sleep/resume cycles still correct cleanly. Align the NTP source with the host or explicitly run `barn time sync`; a later offset within the threshold or a guest reboot also clears the conflict.

Status distinguishes installed files from an active scheduler registration and shows the latest scheduled result from `$BARN_HOME/time-last.json`. New results replace old results. Correction history lives in `$BARN_HOME/time-sync.json`, which is pruned when nodes or deployments are destroyed and removed alongside `time-last.json` by `barn purge`. Disable stops the task and removes its owned job files; manual and startup checks remain available.

## Host NTP and offline Pigsty {#ntp}

Clock alignment and NTP synchronization are separate results. Direct clock correction does not establish an NTP source or guarantee that `node_timex_sync_status` becomes `1`.

For continuous synchronization in an isolated lab, configure a host NTP server and point guest Chrony at its stable virtual-network address. macOS includes `sntpd`, disabled by default; Linux can serve time with chronyd. Configure the server to serve its local clock when no upstream source is reachable, and restrict access to the intended lab network.

Pigsty rewrites Chrony configuration during NODE deployment. Set `node_ntp_servers` in the Pigsty inventory to the intended reachable source. Barn leaves host and guest NTP configuration to the administrator. Once Chrony provides continuous synchronization, the Barn timer can be disabled while retaining manual correction.

## 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)

The [version baseline](/docs/reference/commands/#versions) identifies the released source and the changes across 1.1.0–1.4.0.
