Read

Module 5 · Users, groups and permissions

Users and groups: id, whoami, who

Every process runs as a user who belongs to groups; learn to ask the system who you are and what that means.

What you will learn

  • Explain what a user id (UID) and a group id (GID) are and why root is special.
  • Use `whoami` and `id` to see the identity of your shell.
  • Know what `who`, `w` and `last` report and why they are empty in the lab VM.

Who is running this command?

Linux is a multi-user system to the core. Every file has an owner, every process runs on behalf of a user, and the kernel decides what is allowed by comparing the two. Inside the kernel a user is just a number, the UID. Names such as root or alice exist only for us: a small text file, /etc/passwd, maps names to numbers, and the tools translate back and forth.

Users also belong to groups, identified by a GID. Each user has one *primary* group (new files get it) and any number of *supplementary* groups. Groups are how you say "these three people may read the project folder" without listing them on every file. UID 0 is root, the superuser: the kernel skips almost every permission check for it. In the practice VM you are root the whole time, which is convenient for learning and exactly what you should avoid on a real machine.

whoami and id

whoami prints the name of the effective user of your shell and nothing else. id gives the whole picture: UID, primary GID and every group, each as number and name. With options you can pick out a single piece: -u the UID, -g the GID, -G all group ids, and -n to show names instead of numbers. Give id a username and it describes that user instead of you.

~% whoami
root
~% id
uid=0(root) gid=0(root)
~% id -u
0
~% id -nG
root
~% id nobody
uid=65534(nobody) gid=65534(nogroup)
~% echo $USER $HOME
root /root

Notice the shell also keeps the identity in environment variables, $USER and $HOME. They are convenient in scripts but they are only *copies* set at login; id asks the kernel and is always right.

Who else is here: who, w, last

who lists the users currently logged in, with their terminal and login time. w adds what each one is doing and the load average. last shows the history of logins and reboots. All three read records that the login program writes to /var/run/utmp and /var/log/wtmp. In this VM nobody *logs in*: init starts a root shell directly on the console, so no record is ever written, who prints nothing, w shows only its header and last complains that wtmp does not exist. That is not a bug in the tools, it is a fact about how this tiny system boots. On a normal server all three are full of useful information.

CommandAnswersSource
whoamiWhat is my user name?kernel (effective UID) + /etc/passwd
idMy UID, GID and groupskernel + /etc/passwd, /etc/group
who / wWho is logged in right now/var/run/utmp
lastWho logged in before, and reboots/var/log/wtmp

Commands in this lesson

CommandWhat it does
whoamiPrint the current user name.
idUID, GID and groups, as numbers and names.
id -uOnly the numeric UID.
id -nGNames of all the groups you belong to.
id aliceThe same, for another user.
whoUsers logged in (needs utmp records).
wLogged-in users plus what they run and the load.

Quiz

  1. What is the UID of root?

    • 1
    • 0
    • 1000
    • It depends on the distribution
  2. Which command prints only your numeric user id?

    • `whoami`
    • `id -n`
    • `id -u`
    • `who -u`
  3. Why does `who` print nothing in the practice VM?

    • Because root is hidden from `who`
    • Because nobody logged in: the shell was started by init, so no utmp record exists
    • Because `who` needs network access
  4. A user's primary group is…

    • The group new files created by that user get
    • The group with the lowest GID they belong to
    • Always a group named after the user

Practice

  1. Save your user name into `/root/lab/l41/me.txt` using `whoami`.

  2. Write the full identity report of your shell (UID, GID and groups) to `/root/lab/l41/id.txt`.

  3. Put only the *names* of the groups you belong to into `/root/lab/l41/groups.txt`.

Open this lesson in the app to do the tasks in a real Linux machine and have them checked.