Your Agent Is Running as You: Give It Its Own Linux User and a systemd Sandbox

Here is how most home agent boxes get set up. You flash the Pi or the mini PC, log in as pi or your own name, clone the agent, run the install script, and start it. It works, so you leave it.

That means the agent runs as you. It can read your SSH private keys, your shell history, every .env file in every project folder, and your browser profile if there is one. If your login user has sudo without a password, which is the default on a lot of Raspberry Pi images, the agent can become root whenever it wants. If your user is in the docker group, it can become root that way too.

None of that is a problem on a good day. It becomes a problem on the day a web page, an email, or a scraped document talks your agent into running a command you did not intend. The prompt injection quarantine post covers how to keep untrusted text from being treated as instructions. This post covers the other half: when something does slip through, how much of the machine it can reach.

The fix is old and boring. Give the agent its own Linux user, give that user only the folders it needs, and let systemd wall off the rest. On Debian, Ubuntu, and Raspberry Pi OS this takes under an hour.

What you are actually protecting

Before you change anything, write down what lives on the box that the agent does not need. On a typical home agent box, the list looks like this:

  • Your SSH keys in ~/.ssh, which often open other machines, your Git host, or a VPS.
  • Other projects’ secrets, like the .env for a side project that happens to live in the same home folder.
  • Your shell history, which tends to contain tokens pasted on the command line.
  • Root itself, through passwordless sudo or the docker group.
  • System files the agent should never write, like /etc, /usr, and the boot partition.

The agent needs much less than that: its own code, its own config and secrets, a working folder, a place to write logs, and network access. Everything in this post is about making the list of things it can touch match the list of things it needs.

Step 1: Create a service user

Create a system user with no login shell and a home folder outside /home:

sudo useradd --system --create-home --home-dir /srv/agent --shell /usr/sbin/nologin agent

A few notes on the choices:

  • --system gives it a low user ID and keeps it out of the normal login screen.
  • /usr/sbin/nologin means nobody can SSH in or log in as this user, even with a password.
  • Putting the home folder in /srv/agent instead of /home/agent lets you hide all of /home from the agent later, including your own folder.

Do not add this user to sudo, adm, or docker. Check with:

id agent

You want to see the agent group and nothing else, unless you have a specific reason, like a gpio or video group for hardware the agent actually drives.

Step 2: Move the agent and its secrets

Copy the agent’s code, config, and data into the new home and hand ownership to the new user:

sudo rsync -a ~/agent/ /srv/agent/app/
sudo chown -R agent:agent /srv/agent
sudo chmod 700 /srv/agent

Then move the agent’s secrets into one file only that user can read:

sudo install -o agent -g agent -m 600 ~/agent/.env /srv/agent/agent.env

Two things to check while you are here.

First, look through the .env for secrets the agent does not use anymore. Every leftover key is something a bad run could leak. The secret rotation drill is a good excuse to clean these up and rotate anything that has lived in your home folder for months.

Second, if the agent needs to push to Git or SSH somewhere, give it its own deploy key, scoped to one repository or one host. Do not copy your personal ~/.ssh/id_ed25519 into the new home. That would undo the whole point.

Once the agent runs cleanly from /srv/agent, delete the old copy of the .env from your home folder.

Step 3: Write the systemd unit

If your agent already runs as a systemd service, edit the unit. If it runs from a tmux session or @reboot in your crontab, this is a good time to move it to systemd anyway, because that is where the sandbox lives.

# /etc/systemd/system/agent.service
[Unit]
Description=Self-hosted AI agent
After=network-online.target
Wants=network-online.target

[Service]
User=agent
Group=agent
WorkingDirectory=/srv/agent/app
EnvironmentFile=/srv/agent/agent.env
ExecStart=/usr/bin/node /srv/agent/app/index.js
Restart=on-failure
RestartSec=10

# Sandbox
NoNewPrivileges=yes
ProtectSystem=strict
ProtectHome=yes
ReadWritePaths=/srv/agent
PrivateTmp=yes
PrivateDevices=yes
ProtectKernelTunables=yes
ProtectKernelModules=yes
ProtectControlGroups=yes
RestrictSUIDSGID=yes
LockPersonality=yes
UMask=0077

[Install]
WantedBy=multi-user.target

Swap the ExecStart line for whatever actually starts your agent. Use the full path to the runtime, because the service user will not have your shell’s PATH, nvm setup, or aliases.

What the sandbox lines do, in plain terms:

  • NoNewPrivileges=yes means nothing the agent starts can gain more rights than the agent has, so sudo and setuid programs stop working for it.
  • ProtectSystem=strict makes the whole filesystem read-only for this service.
  • ReadWritePaths=/srv/agent then opens up just the agent’s own folder for writing.
  • ProtectHome=yes hides /home, /root, and /run/user completely, so your SSH keys and other projects are not even visible.
  • PrivateTmp=yes gives the agent its own /tmp, so it cannot read temp files other programs leave behind.
  • PrivateDevices=yes hides raw hardware devices. Leave this one out if the agent drives GPIO, a camera, or a USB device.
  • UMask=0077 makes new files the agent creates readable only by itself.

If you already set memory limits from the OOM killer guide, keep MemoryMax= and friends in the same [Service] section.

Load and start it:

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

Step 4: Fix what breaks

Something will break the first time. That is normal, and the errors are usually clear. The common ones:

  • “Permission denied” or “Read-only file system” on a path. The agent writes somewhere outside /srv/agent, often a cache folder like ~/.cache or ~/.npm. Because the service user’s home is /srv/agent, most of these land in the right place automatically. If one does not, either point it into /srv/agent with an environment variable or add that one path to ReadWritePaths=. Do not reach for ProtectSystem=false.
  • “command not found.” The runtime or a tool lives under your home folder, for example Node installed with nvm. Install the runtime system-wide with apt, or into /srv/agent, and use the full path.
  • A headless browser fails to launch. Chromium wants a writable profile folder and sometimes shared memory. Give it a profile folder under /srv/agent and test with PrivateDevices=yes removed if it still refuses to start.
  • Git asks for credentials. The deploy key needs to be in /srv/agent/.ssh with mode 600 and owned by agent.

Fix one error at a time and restart. Do not loosen the whole sandbox to make one error go away.

Step 5: Move the scheduled jobs too

The service is only half of it. If your morning jobs run from your personal crontab, they still run as you, with all the access you just took away from the agent.

Move them to the agent user’s crontab:

sudo crontab -u agent -e

Or, better, turn each job into a systemd timer with the same User= and sandbox lines as the service. Timers also give you logs in journalctl and a clean place to add the hard time limits from the run timeouts guide.

Then empty the old entries from your own crontab so nothing runs twice.

Step 6: Check your work

systemd can grade the sandbox for you:

systemd-analyze security agent.service

It prints a list of protections and an overall exposure score from 0 to 10, where lower is better. A plain service with no sandbox usually scores around 9. With the lines above, expect somewhere in the 4 to 5 range. You do not need to chase a perfect score. The goal is that the big doors are shut.

Then test the doors directly, as the agent user, from inside the sandbox:

sudo systemd-run --pty --uid=agent --gid=agent \
  -p ProtectHome=yes -p ProtectSystem=strict -p ReadWritePaths=/srv/agent \
  -p NoNewPrivileges=yes /bin/bash

From that shell, try the things a bad run would try:

ls /home            # should be empty or denied
cat /etc/shadow     # should be denied
sudo -n true        # should fail
touch /etc/test     # should fail: read-only
touch /srv/agent/ok # should work

If any of the “should fail” lines succeed, fix the unit before you call this done. Then let the agent run through one full day of its normal jobs, and check that the morning output still shows up.

The short version

  1. Create an agent system user with no login shell, a home in /srv/agent, and no sudo or docker group.
  2. Move the agent’s code and a trimmed .env into /srv/agent, owned by that user, mode 600 on the secrets.
  3. Give it its own scoped deploy key instead of your personal SSH key.
  4. Run it under systemd with User=agent, ProtectSystem=strict, ProtectHome=yes, NoNewPrivileges=yes, and ReadWritePaths=/srv/agent.
  5. Move scheduled jobs out of your personal crontab and into the agent user’s crontab or systemd timers.
  6. Check with systemd-analyze security and a test shell that the agent cannot read your home folder or become root.

This will not stop a confused agent from doing something dumb inside its own folder. It does mean that a bad run costs you the agent’s own workspace, not your SSH keys, your other projects, and root on the box.

Let your agent help with the audit

If you want your agent to help with this, start with MarketMai’s free AI prompt pack and adapt one into a “list every file, folder, and credential this agent actually reads or writes in a normal day” task, which gives you the exact list for ReadWritePaths=. For production configs and runbooks covering setup, security hardening, monitoring, and cron, the OpenClaw Agent Ops Toolkit has the full set, and the MarketMai Ultimate Bundle puts 13 prompt, automation, and agent-operations products in one $49 download.

More from the build log

Suggested

Want the full MarketMai stack?

Get the core MarketMai guides and operator playbooks in one premium bundle for $49.

View Bundle