System users: accounts that run services

Video 118 of 140 in Virtualization Foundations, in the unit “Users, groups, permissions and sudo”. 2:34, with English and Hindi audio.

Many programs run in the background, such as a web server or a monitoring tool. They do not need a person to log in, and they should not run as root, because root can reach every file on the machine. Instead, each service gets its own system account, with only the rights it needs and no way to sign in. This article explains what a service account is, how to look at one, how to create one, and why the difference matters when a service has a bug.

What you will learn

What a service account needs

A system account has no login shell, so nobody can sign in as it by mistake. If the service has a bug, the damage stays inside what that account could already touch.

Looking at one service account

Show the account that the web server runs as
getent passwd www-data

On Ubuntu, the web server runs as www-data. Its login shell is nologin, so it cannot be used to log in. The ps command shows the account a running service runs as, so you can confirm the setup after the service starts.

Making a system account for your own service

Create a system account with no home folder and no login shell
sudo useradd --system --no-create-home --shell /usr/sbin/nologin reportsvc

The system option makes the account a system account. The no create home option gives it no home folder, and the shell option sets its login shell to nologin.

System accounts usually have user numbers below one thousand, while ordinary people's accounts start at one thousand. That is a convention, not a law.

Some services need a home folder for their own settings. Create it for that account only, and keep the account as its owner. If a service must write to a folder that only root owns, change that folder's owner to the service account, rather than running the service as root.

Why a service should not run as root

Picture a monitoring service that runs as root. Root can reach every file on the machine, so a bug in the service can reach them too. When the same service runs as its own account, it has rights only to its own folder. A bug then stops at that folder, and the rest of the machine stays out of reach.

Try it yourself

You need an account with sudo rights. The second step creates a new account, so choose a name that does not exist on the machine yet, such as reportsvc.

  1. Look at the account the web server runs as. Check its login shell at the end of the line.

    getent passwd www-data

    What you should see: The shell prints www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin

  2. Create a system account for a service, with no home folder and no login shell. Use a name that does not exist yet.

    sudo useradd --system --no-create-home --shell /usr/sbin/nologin reportsvc

    What you should see: Creates a system account with no home folder and no login shell.

Terms

System account
An account made for a background service, with few rights, usually no home folder and no login shell.
nologin
A login shell that refuses sign-in, so the account cannot open an interactive session.

Check yourself

Why should a monitoring service not run as root?

Root can reach every file on the machine. A service account has rights only to what the service needs, so a bug does much less damage.

What does the nologin shell do for a service account?

It stops anyone from opening an interactive session as that account.

What does the no create home option do in useradd?

It gives the new account no home folder.

Previous: su or sudo: two ways to act as root. Next: ACLs in brief: rights for named people.