Skip to content
Tutorials Center
My learning

Topics

Lesson 51 of 95 ยท Processes and the System

Users, Groups and sudo

Understand Linux users and groups: UIDs, id and groups, /etc/passwd and /etc/group, root, su and sudo, file access by UID, umask and least privilege.

Linux was built as a multi-user system from the start. Even on a laptop that only you use, many different users exist: you, the superuser root, and dozens of "service accounts" that own web servers, databases and logging daemons. The kernel does not know names at all; it knows numbers. Every process runs as a user ID and a set of group IDs, and every file belongs to one user and one group. All the security of the system rests on comparing those numbers.

In this lesson you will learn how to read your identity with id, how users and groups are stored in /etc/passwd and /etc/group, what makes root special, how su and sudo work (read-only: they are not available in our sandbox), how the kernel decides if a process may read a file, and what the principle of least privilege means in practice. It builds on File Permissions and Ownership.

Who am I? Numbers first, names second

The command id prints your user ID (UID), your primary group ID (GID) and all your supplementary groups. Normally it also prints names next to the numbers, taken from the user database. Our sandbox runs your code under an arbitrary numeric ID that has no name in the database, which is a perfect illustration of the point: the kernel only needs the number.

Bash Identity is a set of numbers

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

The failure at the end is normal here: with no entry for this number in /etc/passwd, id -un prints the message id: cannot find name for user ID ... (we hid it with 2>/dev/null) and exits with status 1. On a normal system id -un (or whoami) prints your login name. Scripts that need to know "am I root?" should compare the number: (( EUID == 0 )), because names can be changed or duplicated. Bash also keeps the numbers in the variables UID (real user) and EUID (effective user).

CONSOLE
$ id
uid=1000(ann) gid=1000(ann) groups=1000(ann),27(sudo),44(video),100(users)
$ whoami
ann
$ groups
ann sudo video users

(Read-only example: the actual numbers and names depend on the machine.) The real UID is who you are; the effective UID is the identity used for permission checks, which differs from the real one only for set-uid programs such as sudo or passwd.

The user database: /etc/passwd

On a classic Linux system users are listed in the plain text file /etc/passwd, one line per account, seven fields separated by colons:

TEXT
name : password : UID : GID : comment : home directory : login shell
ann:x:1000:1000:Ann Lee,,,:/home/ann:/bin/bash
  • name: the login name.
  • password: nowadays always x, meaning "look in /etc/shadow", where the password hashes are stored, readable only by root.
  • UID and GID: the numbers that matter. By convention UID 0 is root, 1 to 999 are system accounts and 1000 and up are people.
  • comment (GECOS): full name and other info.
  • home and shell: where cd without arguments goes, and which program starts when the user logs in. A shell of /usr/sbin/nologin or /bin/false marks an account that cannot log in interactively, which is what service accounts use.

The file is world-readable, so ordinary tools can parse it. Because each line is split by colons, awk -F: is perfect:

Bash Reading /etc/passwd with awk

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Do not edit /etc/passwd by hand on a real machine. Use useradd, usermod and userdel (or adduser on Debian and Ubuntu), because those tools also create the home directory, update /etc/shadow and keep the files consistent.

getent: the right way to ask

Some systems keep users elsewhere (LDAP, a company directory, systemd). The command getent asks the system's name service instead of reading the file, so it also finds accounts that are not in /etc/passwd:

Bash getent passwd and group

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

In scripts, getent passwd "$user" is the dependable way to look up a user's home directory or shell. eval echo ~"$user" works too, but eval is risky if $user comes from outside.

Groups: /etc/group

A group is a named set of users. Every user has one primary group (the GID in /etc/passwd) that new files get by default, plus any number of supplementary groups that give extra access. The file /etc/group has four fields:

TEXT
name : password : GID : members
docker:x:999:ann,bob

The primary group is not repeated in the member list; the supplementary members are listed after the last colon.

Bash Reading /etc/group

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Typical groups give access to a resource: sudo (may use sudo), docker (may talk to the Docker daemon, which is practically root access), www-data (web server files), video, audio. Command to change membership on a real system (read-only, needs root): sudo usermod -aG docker ann. The -a (append) matters: without it the user is removed from all groups not listed. Group changes only apply to new login sessions, so log out and in again, or run newgrp docker for a temporary shell.

File access: the kernel compares numbers

Every file has an owner UID and a group GID plus nine permission bits (see File Permissions and Ownership). When a process opens a file, the kernel does the following, in order, and stops at the first rule that applies:

TEXT
   process uid == file uid ?   ->  use the OWNER bits  (rwx------)
   else process in file's group? -> use the GROUP bits (---rwx---)
   else                         ->  use the OTHER bits  (------rwx)

   root (uid 0) bypasses all of this (almost: it still cannot execute a file with no x bit at all)

One consequence worth remembering: only the first matching class counts. If you are the owner and the owner bits are --- but the group bits are rwx, you are still denied, because the kernel picks the owner class and ignores the others.

Bash The owner class wins, even if it grants nothing

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

In the first failure we own the file, so the kernel uses the owner bits (---), even though the other bits would allow everything. That is correct behaviour and a classic interview question. The second failure is the same lock from outside: /etc/shadow holds password hashes and is readable only by root and the shadow group, so an ordinary user gets Permission denied.

What ordinary users cannot do

Everything that changes ownership or touches protected parts of the system needs privileges. In our sandbox you can see this directly:

Bash Operations that need root (fails on purpose)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

The messages are chown: changing ownership of 'mine.txt': Operation not permitted, touch: cannot touch '/etc/my-new-config': Read-only file system (the sandbox mounts /etc read-only; on a normal system you would see Permission denied) and date: cannot set date: Operation not permitted. Only the owner of a file may chmod it, and only root may chown it to someone else (otherwise users could disown their huge files and escape disk quotas, or hand files to others to frame them).

root, su and sudo

root is the account with UID 0. The kernel skips nearly every permission check for it: it can read any file, kill any process, mount disks, change the clock and the network configuration. That also means one typing mistake as root (rm -rf / tmp/x with an accidental space) can destroy the system. So the golden rule is: do not work as root; borrow root power for single commands.

Two tools do the borrowing. They are not present in our sandbox (there is no root password and no sudoers file), so the examples below are read-only.

su (substitute user) starts a shell as another user and asks for that user's password:

CONSOLE
$ su - postgres      # become user postgres, with that user's login environment
Password:
postgres@db:~$ psql
postgres@db:~$ exit

sudo (superuser do) runs one command as another user, usually root, and asks for your own password. Who is allowed is configured in /etc/sudoers (edit it only with visudo, which checks the syntax before saving):

CONSOLE
$ sudo apt update                 # one command as root
$ sudo -u www-data touch /var/www/x   # as another user
$ sudo -l                         # list what I am allowed to do
$ sudo -i                         # a root login shell: avoid, use single commands
$ echo "x" | sudo tee /etc/motd   # redirections run as YOU: use tee to write as root

The last line is a classic trap: in sudo echo x > /etc/motd the shell opens /etc/motd for writing before sudo even starts, so the redirection runs with your permissions and fails. Piping into sudo tee FILE makes the privileged program do the writing.

su sudo
Password asked the target user's your own
Typical use switch identity for a session run one privileged command
Audit trail weak every command logged (usually /var/log/auth.log)
Configuration none /etc/sudoers, /etc/sudoers.d/

Check what is available, as a script would:

Bash Are su and sudo here?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

su exists as a program but cannot do anything for us: it asks for a password (Password:) and answers su: Authentication failure. sudo is not installed. Real machines have both, and a script that needs root should check up front: if (( EUID != 0 )); then echo "run as root" >&2; exit 1; fi.

umask: the default permissions of new files

Programs create files asking for generous permissions (666 for files, 777 for directories). The umask is a mask of bits to remove from that request. Umask 022 removes group and other write, giving 644 and 755; 077 removes everything for group and other, giving private files (600 and 700). Because it is a per-process setting inherited by children, you can set it in a subshell to affect just a few commands (see File Permissions and Ownership for the table).

Bash umask decides the permissions of new files

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Files get 666 minus the mask, directories 777 minus the mask. (We print only the last three digits: the sandbox's working directory has the setgid bit, so new directories inherit it and stat shows a leading 2, as in 2755.)

For scripts that write passwords, tokens or keys, start with umask 077 so the files are never readable by others, not even for a moment. The alternative of creating a file and then running chmod 600 leaves a short window in which it exists with looser permissions.

The principle of least privilege

Security people repeat one rule: give every user, process and script only the access it needs, and nothing more. In practice:

  • Run services as their own unprivileged user, not as root. If a web server has a bug, the attacker gets a user that can only touch the web folder.
  • Prefer sudo for single commands over a permanent root shell, and prefer giving a user sudo for a specific command over full root.
  • Use groups for shared access (chgrp devs project/ && chmod g+rwX project/), instead of chmod 777.
  • Never use chmod 777 to "fix" a permission problem. Find out which user needs which access and grant exactly that.
  • Keep secrets private: chmod 600 for keys and tokens (SSH refuses keys that are readable by others).
  • Avoid running as root in containers (add a USER line in the image), and review sudoers and the members of the sudo and docker groups regularly.
  • Scripts should check their privileges at the start, drop them as soon as possible, and never trust names (use numbers) or paths provided by users.

Managing groups and the shadow files

Account tools change several text files at once, so you should know which ones. Besides /etc/passwd and /etc/group, there are two shadow files that only root can read: /etc/shadow keeps password hashes and ageing data, and /etc/gshadow keeps group passwords and the lists of group administrators and members. Splitting the files means that anyone can look up a user name or a group name (ls -l needs it), while the secrets stay private.

TEXT
$ ls -l /etc/passwd /etc/shadow /etc/group /etc/gshadow
-rw-r--r-- 1 root root   1842 Mar  3 09:10 /etc/passwd
-rw-r----- 1 root shadow  991 Mar  3 09:10 /etc/shadow
-rw-r--r-- 1 root root    812 Mar  3 09:10 /etc/group
-rw-r----- 1 root shadow  690 Mar  3 09:10 /etc/gshadow

The group-management commands are small and each does one job. They all need root and none can run in our sandbox, so these are read-only:

Terminal
$ sudo groupadd developers               # create a group; the GID is picked automatically
$ sudo groupadd -g 2500 -r svc-backup    # fixed GID; -r makes it a system group (low GID range)
$ sudo groupadd -o -g 2500 twin          # -o (--non-unique) allows a GID that is already used
$ sudo groupmod -n engineers developers  # rename the group (files keep the numeric GID)
$ sudo groupmod -g 3000 engineers        # change the GID (re-own files yourself afterwards!)
$ sudo gpasswd -a alice engineers        # add a member
$ sudo gpasswd -d alice engineers        # remove a member
$ sudo gpasswd -A bob engineers          # make bob an administrator of the group
$ sudo gpasswd -M alice,bob,carol engineers   # set the complete member list
$ sudo gpasswd -r engineers              # remove the group password
$ sudo gpasswd -R engineers              # restrict: nobody can join with newgrp
$ sudo groupdel engineers                # delete the group
$ sudo grpck                             # check /etc/group and /etc/gshadow for errors
$ sudo pwck                              # the same for /etc/passwd and /etc/shadow

A few facts that cause real trouble:

  • groupdel refuses to delete the primary group of an existing user. Change or delete that user first. It does not touch files: files owned by the deleted GID are shown with a bare number until you fix them with find / -gid 3000 -exec chgrp ....
  • groupmod -g and usermod -u change the database, not the files on disk. After changing a number, re-own the files with find and chgrp/chown.
  • Membership takes effect at the next login. id in an old terminal still shows the old groups; newgrp engineers or logging in again refreshes them.
  • A user who is a group administrator (set with gpasswd -A) can add and remove members of that group without being root.
  • grpck and pwck report duplicate names, members that do not exist, primary groups that do not exist and wrong field counts; run them after hand-editing the files (use vipw and vigr, which lock the files while you edit).

What a new account receives

useradd -m creates the home directory and copies the contents of /etc/skel/ into it: the template .bashrc, .profile and .bash_logout. Put a file in /etc/skel and every new user gets it. useradd -r makes a system account (a UID below SYS_UID_MAX, normally 999) for a service such as nginx or postgres: no home by default, a shell of /usr/sbin/nologin, no ageing. Regular users start at 1000. Defaults for all of this live in /etc/login.defs and /etc/default/useradd.

The adduser command on Debian and Ubuntu is a friendlier Perl wrapper around useradd that asks questions; scripts should use useradd.

Checking the consistency yourself

grpck is just a validator for a simple file format, so you can write the same checks with awk. This lab builds a deliberately broken pair of files in a private directory and finds three kinds of problems.

Bash A grpck-style checker with awk

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

The trick NR==FNR is true only while reading the first file, so the first block of the script loads a lookup table and the second block uses it.

Talking to other users: write, wall and mesg

On a multi-user machine you can message people who are logged in. write alice sends a line to alice's terminal (end with Ctrl+D). wall "Rebooting in 5 minutes" broadcasts to every terminal, and wall -n suppresses the banner. A user can refuse messages with mesg n and allow them again with mesg y; mesg alone prints the current state. Root's messages ignore the setting. who and w show who is logged in, and pinky prints similar information in a compact format (login name, real name, terminal, idle time, host). These tools only reach terminals, so they are mainly used for maintenance announcements.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. How does the kernel identify the owner of a file?

    The kernel only stores and compares numbers. Names are looked up from /etc/passwd (or another database) when tools like ls need to display them.
  2. 2. What does the x in the password field of a line in /etc/passwd mean?

    Modern systems keep password hashes in /etc/shadow, which only root can read. The x is a placeholder, so that the world-readable passwd file reveals no hashes.
  3. 3. You own a file with mode 007 (owner ---, group ---, other rwx). Can you read it?

    The kernel stops at the first class that matches: owner, then group, then other. As the owner you get exactly the owner bits, even if the other bits are more generous. You can still chmod the file.
  4. 4. Why does sudo echo hi > /etc/motd fail for an ordinary user even though sudo is installed?

    The redirection > is handled by your shell, which opens /etc/motd as you. Use echo hi | sudo tee /etc/motd so that the privileged program opens the file.
  5. 5. Which is the best way to find out inside a script if it runs as root?

    Root is defined by UID 0, not by name. EUID holds the effective user ID, which is what permission checks use. $USER can be unset or changed.

Summary

  • The kernel works with numeric IDs: every process has a UID and GIDs, every file an owner UID and a group GID. Names come from /etc/passwd and /etc/group (ask with getent).
  • id shows your identity; test for root with (( EUID == 0 )).
  • /etc/passwd has seven colon-separated fields, /etc/group four; password hashes live in /etc/shadow.
  • Access is decided by the first matching class: owner, then group, then other. Root bypasses the check.
  • Only root may chown; only the owner (or root) may chmod. sudo runs single commands as root, su starts a shell as another user; redirections happen before sudo, so use sudo tee.
  • umask subtracts permission bits from new files; use umask 077 for secrets.
  • Least privilege: separate service accounts, groups for sharing, no chmod 777, no routine root shells.

Next: Packages, Services and systemd