Chapter 7 · Lesson 5 of 11
Linux for servers

Linux users, groups and sudo explained (adduser, usermod, visudo)

Linux identifies every account by a user ID and group IDs stored in /etc/passwd and /etc/group, and sudo lets chosen users run commands as root according to rules in /etc/sudoers.

By the end you will

Inspect accounts, add a user to a group, grant limited sudo with visudo, and create a no-login service account for an app.

Time
25 min
Level
Intermediate
Check
4 questions
You need
  • An Ubuntu or Debian server you can reach over SSH
  • A user with sudo access

Every file, process and login on Linux belongs to a user and a group, and sudo is how a normal user does an admin task. Getting this wrong has two failure modes: people who cannot do their job because a group is missing, and accounts with far more power than they need. This lesson goes deeper than create a sudo user and shows how the pieces fit.

How it works#

An account is a line in /etc/passwd. Each line has seven colon-separated fields:

output
deploy:x:1001:1001:Deploy User:/home/deploy:/bin/bash

These are: name, a placeholder for the password (x means the hash is in /etc/shadow), UID, primary GID, a comment, the home directory and the login shell. /etc/group lists groups the same way:

output
sudo:x:27:alice,deploy
docker:x:999:deploy

The fields are the group name, a password placeholder, the GID and a comma-separated member list. Permissions are checked against your UID and the GIDs of your groups, which is the basis of file permissions. UID 0 is root. On Debian and Ubuntu, normal users start at 1000, and system accounts use lower numbers.

sudo runs a command as another user, root by default, if the rules in /etc/sudoers and /etc/sudoers.d/ allow it. On Ubuntu, members of the sudo group may run anything.

Step 1: Inspect accounts#

bash
id
id deploy
groups
getent passwd deploy
getent group sudo
cut -d: -f1 /etc/passwd

id prints your UID, GID and all groups. groups lists only group names. getent queries the account databases, including network ones, and is the right tool for scripts. cut -d: -f1 lists all account names. Never read hashes: /etc/shadow is root-only, and you should not need it.

Step 2: Create users and groups#

bash
sudo adduser alice
sudo groupadd appteam
sudo usermod -aG appteam alice

adduser is the friendly Debian wrapper. It asks for a password and creates the home directory. useradd is the low-level tool and does not create a home unless you pass -m, so prefer adduser for people. groupadd makes a group.

usermod -aG appteam alice adds Alice to a supplementary group. The -a is the one that matters. Without it, -G replaces her whole group list, and if you do that to an admin you can remove them from sudo.

Group changes apply at login. After adding yourself to a group, log out and back in, or start a new SSH session, then check with id. As a quick test in the current shell, run newgrp appteam.

To remove a user from a group, use sudo gpasswd -d alice appteam. To delete an account but keep its files, sudo deluser alice. Adding --remove-home deletes the home folder, which is destructive, so back it up first.

Step 3: Grant sudo#

The simple route on Ubuntu:

bash
sudo usermod -aG sudo alice
sudo -l -U alice

sudo -l lists what the current user may run, and -U alice shows it for another user. For a narrower rule, use a drop-in file and always edit with visudo:

bash
sudo visudo -f /etc/sudoers.d/deploy

Put this in the file:

output
deploy ALL=(root) NOPASSWD: /usr/bin/systemctl restart myapp, /usr/bin/systemctl status myapp

This lets deploy restart one service as root without a password, and nothing else. Use full paths. visudo checks the syntax on save and refuses a broken file. Files in /etc/sudoers.d/ must be mode 0440 and must not contain a dot or end with ~ in the file name, or they are ignored. visudo -f sets the mode correctly. Check the whole configuration with sudo visudo -c.

Be careful with NOPASSWD: ALL and with commands that can spawn a shell, such as editors, less or find. They turn a narrow rule into full root. Keep a second root-capable session open while you test.

Step 4: sudo -i, sudo -s and su#

bash
sudo -i
sudo -s
sudo -u postgres psql

sudo -i runs a login shell as root, with root’s home directory and profile. sudo -s runs a shell as root but keeps more of your environment and your current directory. sudo -u postgres psql runs one command as another user, which is the safer habit: act as root only for the single command that needs it. Exit with exit. Avoid leaving a root shell open, since every typo then has root power. See Linux environment variables and the shell profile for why the two shells behave differently.

Step 5: Service accounts#

A service should not run as root or as a person. Create a dedicated account with no login:

bash
sudo adduser --system --group --home /var/lib/myapp --shell /usr/sbin/nologin myapp

--system picks a low UID and creates no password. --group creates a matching group. --home sets the data folder and --shell /usr/sbin/nologin blocks interactive logins. Then give the service its folders and run the unit under that user:

bash
sudo chown -R myapp:myapp /var/lib/myapp
output
[Service]
User=myapp
Group=myapp

If the app is compromised, the attacker gets only what myapp can touch. The unit file details are in run a service with systemd.

Check it worked#

bash
id alice
sudo -l -U alice
getent passwd myapp
sudo -u myapp id

After logging out and in, id alice lists the new group. getent passwd myapp should show the nologin shell and a UID below 1000. sudo -u myapp id proves you can act as that account.

Edge cases and gotchas#

  • Group changes need a new login. A running shell keeps its old groups. Reconnect or use newgrp.
  • docker group equals root. Anyone in docker can mount / into a container, so treat membership as root access.
  • A broken sudoers locks you out. Edit only with visudo. Keep a root console or second session available.
  • Do not delete the only sudo user. Confirm another admin works before deluser.
  • UIDs matter across servers and containers. Files on a bind mount are owned by number, so UID 1000 on the host may differ from the container.
  • Root login over SSH. Prefer a sudo user and disable direct root login, see disable SSH password login.

Troubleshooting#

SymptomLikely causeFix
alice is not in the sudoers file. This incident will be reported.The user is not in sudo or sudoerssudo usermod -aG sudo alice from an admin account
usermod: group 'docker' does not existThe group is not created yetInstall Docker or run sudo groupadd docker
id does not show the new groupOld sessionLog out and in, or run newgrp name
sudo: unable to resolve host server1: Name or service not knownHostname missing from /etc/hostsAdd the hostname to 127.0.1.1 in /etc/hosts
>>> /etc/sudoers.d/deploy: syntax error near line 1 <<<Typo in the ruleChoose e to edit again in visudo, and fix the line
sudo: /etc/sudoers.d/deploy is world writableWrong mode on the drop-insudo chmod 0440 /etc/sudoers.d/deploy
adduser: The user 'alice' already exists.Name is in usePick another name or use usermod on the existing user
This account is currently not available.The account has the nologin shellExpected for service accounts; use sudo -u name command

Quick reference#

bash
id; groups                       # who am I, which groups
getent passwd NAME               # one account line
sudo adduser NAME                # new person
sudo usermod -aG GROUP NAME      # append to a group
sudo gpasswd -d NAME GROUP       # remove from a group
sudo -l -U NAME                  # what sudo allows
sudo visudo -f /etc/sudoers.d/NAME   # safe sudo rule
sudo -u NAME command             # run as another user
sudo adduser --system --group --shell /usr/sbin/nologin NAME

For a service account in a real deploy, see deploy a Go service with Docker.

Next#

Accounts have their own environments. Continue with environment variables and the shell profile.

Check what you learned4 questions
1Which command appends a user to a group without removing their other groups?
2You added yourself to a group, but id still does not show it. What is the most likely reason?
3Why use visudo instead of editing /etc/sudoers with nano?
4Which file is the best place for a custom sudo rule on a package-managed system?

Frequently asked questions

How do I add a user to a group in Linux?

Run sudo usermod -aG groupname username. The -a flag appends, and without it usermod replaces all the user's supplementary groups. The user must log out and in for it to apply.

How do I give a user sudo access on Ubuntu?

Add them to the sudo group with sudo usermod -aG sudo username. For finer control, create a file in /etc/sudoers.d/ with visudo -f.

What is the difference between sudo -i and sudo -s?

sudo -i starts a login shell as root, with root's environment and home directory. sudo -s starts a shell as root but keeps most of your current environment.

What is a system user in Linux?

A system user is an account for a service, not a person. Created with adduser --system, it gets a low UID, no password and usually no login shell.

Sources

View MD Edit
Share
Helpful?
Progress is saved in this browser only.

Keep going: guides and tutorials

View MD Edit
Share
Helpful?
Run this on your own server

Levelrail deploys apps from git with health-checked releases and one-click rollback.

docker pull ghcr.io/glincker/levelrail:latest Install Levelrail From GLINR Studios