Skip to main content

Linux and servers · Intermediate · 14 min

Give a service its own system user and directories

By Padding · Updated 18 Nov 2026
Ubuntu 26.04.1 LTS · systemd 259

You will create a locked system account for a small service, give it a config folder it can read but not change plus state and log folders only it can write, and declare the same thing the packaged way with systemd-sysusers and DynamicUser=.

On this page

A service should run under an account of its own. Not root, because a bug in a root process is a bug with the whole machine behind it. Not your own login, because then the service can read your SSH keys and shell history, and you can no longer tell its files from yours. A dedicated system account has no password, cannot log in, and owns only the few folders the service needs.

In this tutorial you will set that up for a small service called tally: a script that reads a greeting from its config, counts how many times it has run, and writes a log line each time. The pattern is the same for a Go binary, a Node app or a Python worker. You will:

  • create the tally system account by hand with useradd
  • lay out /etc/tally, /var/lib/tally and /var/log/tally with the right owner and mode in one command each
  • prove with sudo -u tally that the service can read its config but not change it
  • declare an account the way a growing number of packages do, with a sysusers.d file
  • see how systemd's DynamicUser= can make the account and folders for you

Step 1 · Install the service program

The program goes in /usr/local/bin, owned by root. The service account will run it but must never be able to edit it, otherwise a compromised service could rewrite its own code.

Bash
sudo tee /usr/local/bin/tally > /dev/null <<'EOF'
#!/bin/sh
# tally: read a greeting, count runs, log each run.
set -eu
conf_dir=${CONFIGURATION_DIRECTORY:-/etc/tally}
state_dir=${STATE_DIRECTORY:-/var/lib/tally}
log_dir=${LOGS_DIRECTORY:-/var/log/tally}

greeting=$(sed -n 's/^greeting=//p' "$conf_dir/tally.conf")
count=$(cat "$state_dir/count" 2>/dev/null || echo 0)
count=$((count + 1))
echo "$count" > "$state_dir/count"
echo "$(date -u +%Y-%m-%dT%H:%M:%SZ) run $count as $(id -un)" >> "$log_dir/tally.log"
echo "$greeting (run $count)"
EOF
sudo chmod 755 /usr/local/bin/tally

The three ${...:-...} lines matter later: when systemd creates a service's folders it passes their paths in CONFIGURATION_DIRECTORY, STATE_DIRECTORY and LOGS_DIRECTORY. Without systemd the script falls back to the usual places.

Check it worked

Bash
ls -l /usr/local/bin/tally

The line starts -rwxr-xr-x 1 root root. Anyone may run the file; only root may change it.

Step 2 · Create the system account

Bash
sudo useradd --system --user-group --shell /usr/sbin/nologin \
  --home-dir /var/lib/tally --no-create-home --comment "tally service" tally

What each option buys you:

  • --system takes the user ID from the system range (below 1000 on Ubuntu) and skips password ageing, so the account never expires out from under a running service.
  • --user-group makes a group called tally as well. You will use it to share the config read-only.
  • --shell /usr/sbin/nologin means anyone who tries to log in as tally, over SSH or with su, gets a refusal message instead of a shell.
  • --home-dir /var/lib/tally --no-create-home records a sensible home path without creating a skeleton home full of .bashrc files. You create that folder yourself in the next step.

useradd sets no password, so the account is locked for password logins from the start.

Check it worked

Bash
id tally
getent passwd tally
sudo passwd -S tally

id shows a user and group ID below 1000, for example uid=999(tally) gid=999(tally) groups=999(tally). The getent line ends with /var/lib/tally:/usr/sbin/nologin. In the passwd -S line the second field is L: the password is locked.

Try to get a shell as the account:

Bash
if sudo su - tally -c true; then echo "tally got a shell: check its login shell"; false; else echo "login refused, as intended"; fi

nologin prints This account is currently not available. and the command ends with login refused, as intended.

Step 3 · Lay out the config, state and log folders

A service usually needs three places, and each has a different owner:

| Folder | Owner and group | Mode | Why |
|---|---|---|---|
| /etc/tally | root:tally | 750 | Root edits the config; the service may only read it |
| /var/lib/tally | tally:tally | 750 | Data the service keeps between runs |
| /var/log/tally | tally:tally | 750 | Log files the service writes |

install -d creates a folder and sets its owner, group and mode in one go, so there is no moment where it exists with the wrong permissions:

Bash
sudo install -d -o root  -g tally -m 750 /etc/tally
sudo install -d -o tally -g tally -m 750 /var/lib/tally
sudo install -d -o tally -g tally -m 750 /var/log/tally

Create the config file the same way, empty and with its final mode, then write into it. tee keeps the mode of a file that already exists.

Bash
sudo install -o root -g tally -m 640 /dev/null /etc/tally/tally.conf
echo 'greeting=Hello from tally' | sudo tee /etc/tally/tally.conf > /dev/null

Mode 640 on root:tally means root can read and write, the tally group can read, and everyone else gets nothing. If the config later holds a database password or API key, it is already private: no other user on the server can read it. Put real secrets in this file or in an environment file with the same ownership, never inside the script.

Check it worked

Bash
sudo stat -c '%U:%G %a %n' /etc/tally /etc/tally/tally.conf /var/lib/tally /var/log/tally

You see:

Text
root:tally 750 /etc/tally
root:tally 640 /etc/tally/tally.conf
tally:tally 750 /var/lib/tally
tally:tally 750 /var/log/tally

Step 4 · Prove what the service can and cannot do

sudo -u tally runs one command as the account. It works even though the login shell is nologin, because sudo runs the command directly instead of starting a login session. That makes it the right tool for testing permissions exactly as the service will see them.

Run the service twice:

Bash
sudo -u tally /usr/local/bin/tally
sudo -u tally /usr/local/bin/tally

The output is Hello from tally (run 1) and then Hello from tally (run 2). The account read its config and wrote its count.

Now try to change the config, and to drop a new file next to it:

Bash
if sudo -u tally sh -c 'echo greeting=changed >> /etc/tally/tally.conf'; then echo "tally can edit its config: fix the mode"; false; else echo "config is read-only for tally"; fi
if sudo -u tally touch /etc/tally/extra.conf; then echo "tally can add files to /etc/tally"; false; else echo "/etc/tally is read-only for tally"; fi

Each attempt fails with Permission denied, followed by the two "read-only" lines. Last, check that an unrelated account cannot read the config at all. nobody stands in for any other user or service on the server:

Bash
if sudo -u nobody cat /etc/tally/tally.conf; then echo "other users can read the config"; false; else echo "config is private to root and tally"; fi

Check it worked

Bash
sudo cat /var/lib/tally/count
sudo cat /var/log/tally/tally.log
sudo ls -l /var/lib/tally /var/log/tally

The count is 2, the log has two lines ending run 1 as tally and run 2 as tally, and both files are owned by tally tally. Your own user cannot list these folders without sudo, which is what you want.

Step 5 · Declare an account with systemd-sysusers

Typing useradd works on one server. Many packages still call adduser from their install scripts, but a growing number, along with configuration tools, declare the account instead: a one-line file in /etc/sysusers.d/ (or /usr/lib/sysusers.d/ for files a package ships) describes the account, and systemd-sysusers creates whatever is missing. Running it again changes nothing, so it is safe in a setup script or on every boot.

Declare an account for a second service, ledger:

Bash
sudo mkdir -p /etc/sysusers.d
echo 'u ledger - "ledger service" /var/lib/ledger /usr/sbin/nologin' | sudo tee /etc/sysusers.d/ledger.conf
sudo systemd-sysusers /etc/sysusers.d/ledger.conf

The folder /etc/sysusers.d is not always there on a fresh server, so mkdir -p creates it if needed. The fields are type (u for a user, which also gets a group of the same name), name, ID (- lets the tool pick a free system ID), description, home and shell. systemd-sysusers prints something like Creating group 'ledger' with GID 998. and Creating user 'ledger' (ledger service) with UID 998 and GID 998.

systemd-sysusers never modifies an account that already exists. If you wrote the same kind of line for tally, it would see the account and leave it as it is.

Check it worked

Bash
getent passwd ledger
sudo systemd-sysusers /etc/sysusers.d/ledger.conf
getent passwd ledger

Both getent lines are identical and end in /var/lib/ledger:/usr/sbin/nologin, and the second systemd-sysusers run prints nothing: the account was there, so nothing changed.

Step 6 · Let systemd make the account with DynamicUser=

When a service runs under systemd, you can skip the account altogether. With DynamicUser=yes, systemd allocates a user and group when the service starts and releases them when it stops. StateDirectory=, LogsDirectory= and ConfigurationDirectory= tell it which folders to create and with what ownership, and it passes their paths to the program in the environment variables the tally script already reads.

This unit runs the same program as a separate service, tally-dynamic, with no account created by you:

Bash
sudo install -d -o root -g root -m 755 /etc/tally-dynamic
echo 'greeting=Hello from a dynamic user' | sudo tee /etc/tally-dynamic/tally.conf > /dev/null
sudo tee /etc/systemd/system/tally-dynamic.service > /dev/null <<'EOF'
[Unit]
Description=tally with a dynamic user

[Service]
Type=oneshot
ExecStart=/usr/local/bin/tally
DynamicUser=yes
ConfigurationDirectory=tally-dynamic
StateDirectory=tally-dynamic
LogsDirectory=tally-dynamic
EOF

How the folders work:

  • StateDirectory=tally-dynamic creates /var/lib/tally-dynamic, owned by the dynamic user. With DynamicUser=yes the real folder sits in /var/lib/private/tally-dynamic, which other users cannot enter, and /var/lib/tally-dynamic is a link to it. The data survives restarts even though the user ID may change.
  • LogsDirectory=tally-dynamic does the same under /var/log.
  • ConfigurationDirectory=tally-dynamic points at /etc/tally-dynamic but leaves it owned by root, so the service can read and not write. The simplest choice is a world-readable config (644), as here. Keep secrets out of it: use LoadCredential=, which hands the service a private copy at start-up, or add SupplementaryGroups= for a group that may read a 640 file.

Use a static account from the earlier steps when other things need a stable name to grant access to, such as a database role, a group shared with Nginx or files outside the service's own folders. Use DynamicUser= for self-contained services. For the rest of the unit file (restarts, logs, enabling at boot) follow /tutorials/run-an-app-as-a-systemd-service.

Check it worked

Check the unit file before starting it:

Bash
systemd-analyze verify /etc/systemd/system/tally-dynamic.service

No output means no errors. Then start the service and read its output:

Bash
sudo systemctl daemon-reload
sudo systemctl start tally-dynamic
sudo journalctl -u tally-dynamic -n 5 --no-pager
sudo ls -l /var/lib/

The journal shows Hello from a dynamic user (run 1), and ls shows tally-dynamic -> private/tally-dynamic. Run sudo systemctl start tally-dynamic again and the count goes to 2.

Step 7 · Remove what you created

The commands below delete the accounts, folders and files from this tutorial. Run them only on the demo names: check each path before you press Enter if you adapted the names for a real service.

Bash
sudo rm -f /etc/systemd/system/tally-dynamic.service /etc/sysusers.d/ledger.conf /usr/local/bin/tally
sudo rm -rf /etc/tally /var/lib/tally /var/log/tally /etc/tally-dynamic
sudo rm -rf /var/lib/private/tally-dynamic /var/lib/tally-dynamic /var/log/private/tally-dynamic /var/log/tally-dynamic
sudo userdel tally
sudo userdel ledger

userdel also removes each account's own group when no other user has it as their main group. If you started tally-dynamic, run sudo systemctl daemon-reload afterwards so systemd forgets the unit.

Check it worked

Bash
getent passwd tally ledger || echo "accounts removed"
getent group tally ledger || echo "groups removed"

Both commands print their "removed" line.

Next steps

  • Wrap a service account in a full unit with restarts and logging: /tutorials/run-an-app-as-a-systemd-service.
  • Narrow what the service can touch further with ProtectSystem=, ReadWritePaths= and friends: /tutorials/sandbox-a-systemd-service-with-security-options.
  • Go deeper on owners, groups and modes: /tutorials/linux-file-permissions-chmod-chown.

Need this done for you?

Padding does this work for growing companies, from the first diagnosis to a system that keeps working.

Platform Engineering

Get new tutorials

New tutorials, about twice a month.