Skip to content

Security Operations / Host hardening · Step 2 of 10

Lesson: Service AccountsSwitch
← Security Operations

Security investigations

Service Accounts

Run each service under its own limited account so one fault does not expose everything.

A service account is a user that exists to run a program, not for a person. If the program is compromised, the attacker gets that account's access. Give it as little as possible.

One account per service

If two services share an account, a flaw in one reaches the other's files. Many packages already create their own account. See who runs what:

$ ps -eo user,comm | head -4
USER     COMMAND
root     systemd
www-data nginx

Output varies. Web servers use www-data on Debian and Ubuntu and often nginx or apache elsewhere.

Create one

$ sudo useradd --system --user-group --no-create-home --shell /usr/sbin/nologin reportsvc
$ getent passwd reportsvc
reportsvc:x:998:998::/home/reportsvc:/usr/sbin/nologin

Your numbers will differ. The path in field six is only recorded; no directory was made. The nologin shell refuses interactive logins. Its usual path is /usr/sbin/nologin, with /sbin/nologin also common on RHEL family systems. It does not stop the account running a service, and it is not a complete lock on every kind of access.

Do not add a service account to the sudo, wheel or docker groups without a strong reason. Membership of docker is effectively root access.

Tell systemd to use it

[Service]
User=reportsvc
Group=reportsvc
ExecStart=/usr/local/bin/report-server
StateDirectory=reportsvc
NoNewPrivileges=yes

StateDirectory= creates /var/lib/reportsvc owned by the service user. NoNewPrivileges=yes stops the process and its children gaining privileges through things like setuid programs.

Add this with sudo systemctl edit --full name.service or in your own unit file, then run sudo systemctl daemon-reload and restart. Check the result:

$ ps -o user,pid,comm -C report-server

Keep program files owned by root so the service cannot rewrite its own code. Let it write only to its data and log directories. Secrets it needs should be readable by it and not by everyone.

Try it

On a test system, create a practice system account with the command above, then run sudo -u practice id to see its identity. Remove it afterwards with sudo userdel practice. The accounts lab covers the files involved.

Check your understanding

Service Accounts

All 3 sections · no timer

Unlimited retries

Questions from every section are combined into one assessment. Answer them all correctly to complete the article.

Your completion saves in this browser. Sign in to sync it to your account.