Linux Command Line · Lesson 22 of 43
Users, groups and who you are
Learn how Linux identifies users and groups, read the output of id, whoami and groups, and find account details in /etc/passwd and /etc/group.
UNDERSTAND IT. THEN TRY IT.
What you can do after this lesson
You will know what "who you are" means to Linux, how to check it, and where accounts and groups are recorded. This is the base for permissions: before you can understand who may read, change or run a file, you need to know who the "who" is.
Users and IDs
Linux is a multi-user system, even on a laptop with one person. Every process runs as some user, and every file is owned by some user. The kernel does not use names for this. It uses a number, the user ID or UID. A name such as asha is a label for a UID such as 1000, kept in a table.
Three kinds of accounts exist:
- root has UID
0and may do anything. It is the administrator account. - System accounts have low UIDs and run services such as web servers or databases. They have no password login.
- Regular users are real people, with UIDs starting at
1000on most distributions (500on some older ones).
Who am I?
whoami prints your user name:
whoamilearnerid prints more: your UID, your primary group and every group you belong to. On your own machine the numbers will be different, but the shape is the same:
uid=1000(asha) gid=1000(asha) groups=1000(asha),27(sudo),100(users)id -u prints only the UID, id -un the name, id -gn your primary group name, and groups prints the group names:
id -u > /dev/null && echo "id works"id worksGroups
A group is a named set of users. Groups make it possible to share files without making them public. For example, everyone in a developers group can be given access to a project folder. Every user has one primary group, which is used for the files they create, and may belong to any number of supplementary groups. Two groups come up often:
sudo(orwheelon Fedora and Red Hat systems): its members may usesudo.docker: its members may run Docker commands (and effectively have administrator rights).
The account database: /etc/passwd
Accounts live in a plain text file. Each line describes one account, with seven fields separated by colons:
grep "^root:" /etc/passwdroot:x:0:0:root:/root:/bin/bashFrom left to right: the name, a placeholder x (the real password hash is in the protected /etc/shadow), the UID, the primary GID, a description, the home directory and the login shell. A shell of /usr/sbin/nologin or /bin/false means the account cannot log in interactively. To get the names of all accounts, take the first field:
cut -d: -f1 /etc/passwd | head -n 3root
bin
daemonThe exact accounts depend on the system. Groups follow the same pattern in /etc/group, with the name, a placeholder, the GID and a comma-separated list of members.
Looking up one user
getent passwd NAME asks the system database, including accounts that come from a network directory and do not appear in the local file:
getent passwd rootroot:x:0:0:root:/root:/bin/bashSwitching and creating users
su (switch user) starts a shell as another user, and sudo runs one command as another user, normally root, which a later lesson covers. Administrators create accounts with useradd or the friendlier adduser, change passwords with passwd and remove accounts with userdel. These need root rights, and they change the system, so we only show their shape:
sudo adduser maria
sudo usermod -aG sudo mariaThe -aG option appends to the groups. Forgetting -a replaces all the user's supplementary groups, a common and painful mistake.
Why this matters for security
Because everything has an owner, a normal user cannot read another user's private files or change system files. A program you run has your rights, and no more. That is why you should work as a normal user and use administrator rights only for the single command that needs them.
Common mistakes
Working as root all the time. One typo as root can break the system. Use a normal account.
Confusing user names and UIDs. If a user is renamed or an account is recreated with a different UID, files may seem to belong to a number.
Editing /etc/passwd by hand. Use useradd, usermod and friends, which keep the files consistent.
Forgetting -a in usermod -G. It removes the user from the other groups.
Exercise
On your machine, run whoami, id and groups. Write down your UID and your groups. Find your own line in /etc/passwd with grep "^$(whoami):" /etc/passwd and name its seven fields. Check whether you are in the sudo or wheel group.
KNOWLEDGE CHECK
A quick check. You’ve got this.
Try each question. Every answer comes with an explanation.
Ready for the next small win?
Mark the lesson complete when you are ready.