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
- A service runs under its own system account, not as root.
- The account gets only the rights its service needs, so a bug does limited damage.
- A system account's login shell is nologin, so no one can sign in as it.
- System accounts usually have user numbers below one thousand, which is a convention and not a law.
- If a service must write to a folder that only root owns, change the folder's owner to the service account.
What a service account needs
- Its own name: each service runs as its own account, so its files and rights stay separate from every person's.
- Few rights: only the folders and files that service needs.
- No login: its login shell is nologin, so no one can open a session as it.
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
getent passwd www-dataOn 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
sudo useradd --system --no-create-home --shell /usr/sbin/nologin reportsvcThe 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.
Look at the account the web server runs as. Check its login shell at the end of the line.
getent passwd www-dataWhat you should see: The shell prints www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin
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 reportsvcWhat 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.