Skip to content

Running OpenClaw as a systemd service

The deep dive on the OpenClaw systemd unit — user-scope vs system-scope, After=network-online.target, Restart=on-failure with backoff, EnvironmentFile, hardening directives, and the difference between loginctl enable-linger and a system unit.

You have OpenClaw (openclaw.ai) installed on a Linux box and you have decided that systemd is going to manage the gateway. Good call. This article is the deep dive on the unit file itself — the directives that matter, the difference between a user-scope service that runs as you and a system-scope service that runs as a dedicated openclaw user, the hardening flags that actually catch bad skills, and the rollback path when the unit refuses to start. It assumes Node 24 is installed, openclaw is on PATH, and openclaw onboard has run at least once.

Scenario

You are running OpenClaw on Ubuntu 24.04, Fedora 40+, Debian 12, or a similar systemd-managed distribution. You have two reasonable choices:

  • User scope: the agent runs as your normal user, the unit lives at ~/.config/systemd/user/openclaw.service, loginctl enable-linger keeps it alive when you log out. Right for a personal desktop or a workstation.
  • System scope: the agent runs as a dedicated openclaw system user, the unit lives at /etc/systemd/system/openclaw.service, it starts at boot regardless of who is logged in. Right for a small VPS, a home server, or any machine where the agent must be up before anyone signs in.

Both are covered below.

Outcome and prereqs

When this is done you’ll have:

  • A unit file in the right scope for your use case.
  • The gateway up on 127.0.0.1:18789, restarting on crash within 5 seconds, ignored when you stop it cleanly.
  • Logs in the journal under the openclaw identifier.
  • systemd-analyze security openclaw showing a low exposure score thanks to the hardening directives.

Prereqs:

  • OpenClaw installed globally with Node 24 (see the macOS install or Ubuntu/Fedora install for the prep).
  • ~/.openclaw/ populated by openclaw onboard.
  • sudo for the system-scope path. User scope needs no sudo at all.

Time and resource footprint

15 minutes attended. The unit itself adds no runtime overhead — systemd is already running.

Step 1 — User-scope unit (personal desktop)

This is the default for a single-user machine where you are the only person who needs the agent. Write ~/.config/systemd/user/openclaw.service:

[Unit]
Description=OpenClaw personal AI agent gateway
Documentation=https://openclaw.ai
After=network-online.target
Wants=network-online.target

[Service]
Type=simple
ExecStart=/usr/bin/openclaw gateway --port 18789
Restart=on-failure
RestartSec=5

# Backoff so a tight crash loop does not hammer the model API
StartLimitIntervalSec=60
StartLimitBurst=5

# Pull provider keys from the .env file written by onboarding
EnvironmentFile=%h/.openclaw/.env

# V8 compile cache cuts cold start times
Environment="NODE_COMPILE_CACHE=%h/.cache/openclaw-compile-cache"

# Hardening — the agent only writes under its own workspace
NoNewPrivileges=true
ProtectSystem=strict
ProtectHome=read-only
ReadWritePaths=%h/.openclaw %h/.cache/openclaw-compile-cache
PrivateTmp=true
ProtectKernelTunables=true
ProtectKernelModules=true
ProtectControlGroups=true
RestrictAddressFamilies=AF_INET AF_INET6 AF_UNIX
RestrictNamespaces=true
LockPersonality=true
SystemCallArchitectures=native

# Resource ceilings
MemoryMax=2G
TasksMax=512

# Journal logging
StandardOutput=journal
StandardError=journal
SyslogIdentifier=openclaw

[Install]
WantedBy=default.target

Adjust the ExecStart path to whichever path which openclaw returns — /usr/bin/openclaw, /usr/local/bin/openclaw, ~/.npm-global/bin/openclaw, or an nvm path like ~/.nvm/versions/node/v24.x.x/bin/openclaw. The unit takes the path literally and does not consult your interactive PATH.

A few directive notes:

  • %h expands to the user’s home directory in systemd unit files. It only works in user-scope units. The semantics are documented in systemd.unit(5) and systemd.exec(5).
  • Restart=on-failure plus the StartLimit* keys is the safe pattern: restart on crash, but back off after five crashes in a minute. Restart=always would happily keep restarting a unit that crashes on every config error.
  • ProtectHome=read-only plus ReadWritePaths=%h/.openclaw is the directive combination that confines the agent to its workspace. A custom skill that tries to rm -rf ~/Documents gets EACCES.
  • RestrictAddressFamilies=AF_INET AF_INET6 AF_UNIX is enough for HTTP, HTTPS, and local sockets. If you ever plug in a skill that talks raw Bluetooth or netlink, you will need to add AF_BLUETOOTH or AF_NETLINK — note this is documented in systemd.exec(5).

Apply it:

mkdir -p ~/.cache/openclaw-compile-cache
systemctl --user daemon-reload
systemctl --user enable --now openclaw.service
systemctl --user status openclaw.service

Enable linger so the agent keeps running when you log out:

sudo loginctl enable-linger "$USER"

Watch the journal:

journalctl --user -u openclaw -f

Step 2 — System-scope unit (home server, VPS, shared workstation)

When the agent must be up before any user logs in, you need a system-scope unit running as a dedicated, non-login openclaw system user. Create the user and the state directory:

sudo useradd --system --home-dir /var/lib/openclaw --shell /usr/sbin/nologin openclaw
sudo install -d -m 0750 -o openclaw -g openclaw /var/lib/openclaw
sudo install -d -m 0750 -o openclaw -g openclaw /etc/openclaw

Run the OpenClaw onboarding as that user, with HOME pointing at the state directory:

sudo -u openclaw HOME=/var/lib/openclaw /usr/bin/openclaw onboard

Move the env file out of the user’s home into /etc/openclaw/openclaw.env, root-owned, openclaw-readable:

sudo mv /var/lib/openclaw/.openclaw/.env /etc/openclaw/openclaw.env
sudo chown root:openclaw /etc/openclaw/openclaw.env
sudo chmod 0640 /etc/openclaw/openclaw.env

Write /etc/systemd/system/openclaw.service:

[Unit]
Description=OpenClaw gateway (system service)
Documentation=https://openclaw.ai
After=network-online.target
Wants=network-online.target

[Service]
Type=simple
User=openclaw
Group=openclaw
WorkingDirectory=/var/lib/openclaw
Environment="HOME=/var/lib/openclaw"
EnvironmentFile=/etc/openclaw/openclaw.env

ExecStart=/usr/bin/openclaw gateway --port 18789

Restart=on-failure
RestartSec=5
StartLimitIntervalSec=60
StartLimitBurst=5

# Hardening
NoNewPrivileges=true
ProtectSystem=strict
ReadWritePaths=/var/lib/openclaw
ProtectHome=true
PrivateTmp=true
ProtectKernelTunables=true
ProtectKernelModules=true
ProtectControlGroups=true
RestrictAddressFamilies=AF_INET AF_INET6 AF_UNIX
RestrictNamespaces=true
LockPersonality=true
SystemCallArchitectures=native

# Resource ceilings
MemoryMax=2G
TasksMax=512
LimitNOFILE=65536

StandardOutput=journal
StandardError=journal
SyslogIdentifier=openclaw

[Install]
WantedBy=multi-user.target

The key differences from the user-scope unit:

  • User=openclaw and Group=openclaw — the unit drops privileges to the system user.
  • Environment="HOME=/var/lib/openclaw" because the agent reads ~/.openclaw/; without HOME set the dedicated user has HOME=/.
  • ProtectHome=true instead of read-only. The agent has no business reading /home.
  • ReadWritePaths=/var/lib/openclaw is the absolute path that systemd will allow writes to. No %h in a system unit.
  • WantedBy=multi-user.target so it comes up at boot before user logins.

Apply it:

sudo systemctl daemon-reload
sudo systemctl enable --now openclaw.service
sudo systemctl status openclaw.service
journalctl -u openclaw -f

Step 3 — Verify hardening

Run systemd-analyze against the unit to see how exposed the agent is:

systemd-analyze security openclaw
# or, for user units:
systemctl --user show openclaw -p FragmentPath
systemd-analyze security --user openclaw

The score is a 0-to-10 number where lower is better. The unit above generally lands in the “OK” or “exposed” band, never “unsafe”. The output explains every flag you have set (or have not set) and what would shave more exposure. Treat anything in red as a candidate for an explicit decision, not a default to ignore.

The semantics of every hardening directive are documented in systemd.exec(5). The single most important one for an agent that can run arbitrary skills is the combination of ProtectSystem=strict plus ReadWritePaths=..., which is what stops a misbehaving skill from writing to /etc or /usr.

Step 4 — Logs and observability

StandardOutput=journal and StandardError=journal route everything to the journal under the unit name and the SyslogIdentifier=openclaw tag. Useful queries:

# Live tail
journalctl --user -u openclaw -f          # user scope
journalctl       -u openclaw -f           # system scope

# Last 24 hours of errors
journalctl --user -u openclaw --since "24 hours ago" -p err

# Crash detection
journalctl --user -u openclaw --grep="crashed|exit-code|FATAL"

# Show how many times it has restarted today
journalctl --user -u openclaw --since today --grep="Started OpenClaw" | wc -l

Set up a daily cron (or a separate systemd *.timer unit) that scans for errors and emails you. The OpenClaw gateway is chatty in --verbose mode; in production you want the default log level and you want to look at errors only.

Step 5 — Reloads vs restarts

OpenClaw supports hot reload of channel config when gateway.reload.mode is set to hybrid (the default), per the OpenClaw configuration docs. The split is:

  • Channel and skill config changes — reload by signaling the running gateway: openclaw gateway reload. No systemd restart needed.
  • Gateway port, bind, auth, TLS — these require a process restart: systemctl --user restart openclaw.
  • Node version change, OpenClaw npm package upgrade — systemctl --user restart openclaw after the upgrade.

systemctl reload openclaw will pass through to openclaw gateway reload only if you add an ExecReload= line to the unit. If you want that:

ExecReload=/usr/bin/openclaw gateway reload

Verification

After every change to the unit:

systemctl --user daemon-reload     # user scope, or `sudo systemctl daemon-reload` for system
systemctl --user restart openclaw  # or `sudo systemctl restart openclaw`
systemctl --user status openclaw

status should show Active: active (running) and the PID. Then:

openclaw doctor
ss -lntp 'sport = :18789'

A passing openclaw doctor plus a listening socket on 127.0.0.1:18789 is the canonical “the daemon is real” check.

Kill the process by PID and watch the supervisor respawn it within five seconds — that exercises Restart=on-failure RestartSec=5 end to end.

Rollback

# User scope
systemctl --user disable --now openclaw.service
rm ~/.config/systemd/user/openclaw.service
systemctl --user daemon-reload

# System scope
sudo systemctl disable --now openclaw.service
sudo rm /etc/systemd/system/openclaw.service
sudo systemctl daemon-reload

# Optional: remove the dedicated user (system scope only)
sudo userdel openclaw
sudo rm -rf /var/lib/openclaw /etc/openclaw

The user’s ~/.openclaw/ (user scope) or /var/lib/openclaw/ (system scope) holds the agent’s entire stateful surface. Archive it before deleting.

Troubleshooting

systemctl --user fails with “Failed to connect to bus”. You are SSHed in without a login session. sudo loginctl enable-linger "$USER" and reconnect, or run from a TTY/console session first. The loginctl(1) man page covers the semantics.

Failed to start openclaw.service: Unit not found. You wrote the unit in the wrong scope. User units go in ~/.config/systemd/user/, system units in /etc/systemd/system/. systemctl --user daemon-reload only picks up user units; sudo systemctl daemon-reload picks up system units. They are entirely separate.

Service starts, immediately stops, journal shows Failed to read environment file ... permission denied. The openclaw system user cannot read /etc/openclaw/openclaw.env. sudo chown root:openclaw /etc/openclaw/openclaw.env && sudo chmod 0640 /etc/openclaw/openclaw.env.

status shows Active: failed (Result: exit-code) and the journal has EACCES: permission denied, open '/etc/.openclaw/openclaw.json'. You forgot Environment="HOME=/var/lib/openclaw" on a system unit. Add it, daemon-reload, restart.

Unit restarts every five seconds in a loop. StartLimitBurst=5 will eventually stop it after five tries. The real fix is in the journal — read the most recent error before the loop. The most common cause is a missing API key in the EnvironmentFile or a config file referencing a model the provider does not have.

systemd-analyze security reports unsafe. You removed too much hardening. The default unit in this article lands in the “OK” band. Diff your unit against it and re-add the directives.

Port conflict. ss -lntp 'sport = :18789' shows the offending PID. Two OpenClaw instances on the same machine is the usual cause (user-scope unit and system-scope unit both running). Pick one.

ExecReload does not pick up changes. OpenClaw’s hot-reload covers only the keys documented in the gateway config docs. Anything else needs a real restart.

Production-readiness

For a system-scope OpenClaw service on a server you depend on:

  • The unit is in version control alongside your other infrastructure-as-code.
  • journalctl -u openclaw --since "24h" -p err is wired into your alerting (CloudWatch, Loki, or a cron + email).
  • systemd-analyze security openclaw runs in CI and gates merges if the score regresses.
  • /var/lib/openclaw and /etc/openclaw both in your backup target.
  • Node pinned (apt-mark hold nodejs on Debian/Ubuntu, dnf versionlock on Fedora), upgraded deliberately with a manual systemctl restart openclaw after.
  • A second machine paired to the same chat channels so a single host outage does not black out remote control.

For organizations running OpenClaw under systemd on a fleet — engineer workstations, home servers, or production VPSes — talk to a senior operator at Stack Harbor about your self-hosted OpenClaw deployment.

Sources

  • OpenClaw home page — https://openclaw.ai.
  • OpenClaw README and gateway flags — https://github.com/openclaw/openclaw.
  • systemd.service(5), systemd.exec(5), systemd.unit(5), loginctl(1) man pages.
  • systemd-analyze security — https://www.freedesktop.org/software/systemd/man/systemd-analyze.html.
  • OpenClaw gateway configuration reference — https://docs.openclaw.ai/gateway/configuration.

Last verified on OpenClaw current (May 2026).