Users & Groups
Every process on Linux runs as some user, and every user belongs to one or more groups. Users and groups are what the permission model checks, what ps shows in its first column and what separates your files from a web server's. This lesson covers how to inspect them and, conceptually, how administrators manage them.
Who am I?
whoami
id
id -un
id -u
groupslearner
uid=1000(learner) gid=1000(learner) groups=1000(learner)
learner
1000
learner
| Command | Shows |
|---|---|
whoami / id -un |
Your user name |
id |
User ID (UID), primary group ID (GID) and all groups |
id -u / id -g |
Just the numeric UID / GID |
groups |
Your group names |
id alice |
The same for another user |
The kernel only knows numbers: the names come from lookups in /etc/passwd and /etc/group. UID 0 is always root, the administrator. On most distributions normal users start at UID 1000; lower numbers are system users for services (www-data, postgres, nobody).
/etc/passwd
Each line describes one account in 7 colon-separated fields:
learner:x:1000:1000:learner:/home/learner:/bin/bash
name pw UID GID comment home shellThe x means the password hash is kept in /etc/shadow, which only root can read. Because the file is text, the usual tools can query it:
grep '^learner:' /etc/passwd
echo "---"
awk -F: '$3 >= 1000 && $3 < 60000 { print $1, "home=" $6, "shell=" $7 }' /etc/passwd
echo "---"
getent passwd nobody | cut -d: -f1,7
getent passwd root | cut -d: -f1,6learner:x:1000:1000:learner:/home/learner:/bin/bash
---
learner home=/home/learner shell=/bin/bash
---
nobody:/sbin/nologin
root:/root
Service accounts get a shell like /sbin/nologin so nobody can log in as them. getent asks the system's user database, which also covers network directories such as LDAP, so it's more reliable than reading the file directly.
Groups
/etc/group lists groups as name:password:GID:members. Your primary group is in /etc/passwd; extra (supplementary) memberships are listed in /etc/group:
cat > group.sample <<'TXT'
root:x:0:
sudo:x:27:ada,linus
docker:x:998:ada
developers:x:1050:ada,grace,linus
ada:x:1001:
TXT
echo "members of developers: $(awk -F: '$1 == "developers" { print $4 }' group.sample)"
echo "groups that include ada:"
awk -F: '$4 ~ /(^|,)ada(,|$)/ { print " " $1 }' group.samplemembers of developers: ada,grace,linus
groups that include ada:
sudo
docker
developers
This is a sample file (the sandbox has very few groups), using typical names: membership of sudo (Debian/Ubuntu) or wheel (Fedora, Arch) lets a user run commands as root; membership of docker lets a user control the Docker daemon, which is practically root as well.
Managing users (requires root)
These commands change system files, so they need sudo and can't run in this sandbox:
sudo useradd -m -s /bin/bash -c "Ada Lovelace" ada # create user with a home directory
sudo passwd ada # set the password
sudo usermod -aG developers,docker ada # ADD to extra groups (-a!)
sudo groupadd developers # create a group
sudo userdel -r olduser # delete a user and their home
sudo chage -l ada # password expiry infoWarning:
usermod -G group userwithout-areplaces all of the user's supplementary groups. Always use-aGto add. Group changes apply at the next login (or withnewgrp).
On Debian and Ubuntu, adduser ada is a friendlier interactive front end to useradd.
Becoming another user: su and sudo
| Command | Effect |
|---|---|
sudo command |
Run one command as root (asks for your password) |
sudo -u postgres psql |
Run as another user |
sudo -i |
An interactive root shell |
su - ada |
Switch to user ada (asks for ada's password) |
sudo -l |
List what you're allowed to run |
sudo is configured in /etc/sudoers (always edit it with visudo, which checks the syntax) and logs every command, which is why it's preferred over sharing the root password.
$ sudo apt update
[sudo] password for ada:
$ sudo -l
User ada may run the following commands on server:
(ALL : ALL) ALLChecks in scripts
Scripts that need root should check at the start rather than fail halfway:
require_root() {
if (( $(id -u) != 0 )); then
echo "error: please run as root (try: sudo $0)" >&2
return 1
fi
}
require_root || echo "running as $(id -un), uid $(id -u): skipping system changes"
[ "$(id -un)" = learner ] && echo "files I create belong to: $(touch x; stat -c %U:%G x)"error: please run as root (try: sudo /w/main.sh)
running as learner, uid 1000: skipping system changes
files I create belong to: learner:learner
EUID, a Bash variable, is another way to read the effective UID: [[ $EUID -eq 0 ]].
Test yourself
What is the UID of the root user?
- 1
- 0
- 1000
- 65534
Which command adds ada to the docker group without removing her other groups?
- usermod -G docker ada
- usermod -aG docker ada
- groupadd docker ada
- chgrp docker ada
Where are password hashes stored?
- /etc/passwd
- /etc/shadow
- /etc/group
- ~/.password