Identity and groups
On this page
You created a note, but its metadata names both an owner and a group. Your account also belongs to more than one group. Which identity connects you to that file?
By the end, you can inspect your account, separate its primary group from supplementary groups, and read a practice file’s ownership without changing access.
Before you start
Section titled “Before you start”Finish Practice: Finding Files and Text. Keep your B04 lab directory. Use only disposable ~/lab/b11/identity:
mkdir -p ~/lab/b11/identitycd ~/lab/b11/identityprintf 'Owner inspection\n' > note.txtThis creates one small text file. If you already used this directory, follow Reset this lesson before repeating the setup. The inspection commands below do not change accounts, groups, or ownership.
Ask which account the command uses
Section titled “Ask which account the command uses”Run the shortest identity check first:
whoamilearnerwhoami prints the name associated with the command’s effective user ID: the identity used for ordinary permission checks. In this session it is learner. Your VM account can have another name. Read what the command reports rather than assuming the prompt’s label establishes identity.
A name is convenient for people. Linux also identifies accounts numerically. Ask for the number and its group context:
iduid=1001(learner) gid=1001(learner) groups=1001(learner),27(sudo)A UID is a user ID. Here UID 1001 maps to the account name learner. A GID is a group ID. The gid= field identifies the primary group of this shell: group learner, also numbered 1001 in this capture.
The repeated number does not make a user and a group the same object. User IDs and group IDs identify different kinds of identity. An account and its primary group often share a name, but that naming choice is not a requirement.
The groups= list contains the primary group plus supplementary groups, additional group memberships available to the process. Here sudo, numbered 27, is supplementary. There are two groups, not two user accounts. Membership can help determine access to a resource owned by a group.
Separate the number from the name
Section titled “Separate the number from the name”Predict first: will id -g print every group ID, or only the primary one?
id -gRun it, then compare with your guess
1001Only the primary group ID. Lowercase -g and uppercase -G ask different questions.
Use -u for the user ID. Add -n to a selected identity field when you want its name instead of its number:
id -u1001id -gnlearnerFor the complete group list, use uppercase -G. Combining it with -n prints the names:
id -Gnlearner sudoThese are observations of the running session. Changing an account’s configured memberships elsewhere does not automatically replace the group list of every already-running shell. A fresh login is normally needed to start a session with updated memberships. This lesson does not change them.
Connect identity to an owned file
Section titled “Connect identity to an owned file”Inspect the note with B06’s stat. The -c option selects a format: uppercase %U and %G request owner and group names; lowercase %u and %g request their numeric IDs. %n prints the filename.
stat -c '%U %G %u %g %n' note.txtlearner learner 1001 1001 note.txtRead the fields in order: owner name, owning group name, owner UID, owning GID, filename. The file has one owning user and one owning group. It does not contain a list of every group its owner belongs to.
The note belongs to user learner and group learner. Your shell’s supplementary membership in sudo did not make sudo the note’s owning group. Ordinary creation here uses the shell’s primary group; directory group inheritance can change that rule in configurations covered later.
In words, account identity, group memberships, and file ownership are separate facts. The diagram collects relationships for this account; its lines do not represent commands or a sequence. This model leaves out group-inheriting directories and additional access-control mechanisms.
learner@rootvolt:~/lab/b11/identity$ whoamilearnerlearner@rootvolt:~/lab/b11/identity$ iduid=1001(learner) gid=1001(learner) groups=1001(learner),27(sudo)learner@rootvolt:~/lab/b11/identity$ stat -c '%U %G %u %g %n' note.txtlearner learner 1001 1001 note.txtlearner@rootvolt:~/lab/b11/identity$ Steps (9)
- Type
whoami - Press Enter
- Output:
learner - Type
id - Press Enter
- Output:
uid=1001(learner) gid=1001(learner) groups=1001(learner),27(sudo) - Type
stat -c '%U %G %u %g %n' note.txt - Press Enter
- Output:
learner learner 1001 1001 note.txt
Ownership alone does not tell you whether a particular operation succeeds. The next lesson reads the permission bits that accompany those identities.
Mistakes to recognize
Section titled “Mistakes to recognize”Supplying a username to whoami. It reports the current effective identity and does not accept an account argument:
whoami learnerwhoami: extra operand ‘learner’Try 'whoami --help' for more information.Use whoami alone for this session. id can inspect a named account, but a mistyped or absent account has its own diagnostic:
id no-such-b11-accountid: ‘no-such-b11-account’: no such userThis says the account lookup failed. It does not say the current shell lost its identity. Check the spelling before assuming an account exists.
Treating sudo membership as an elevated shell. The captured whoami still says learner. A group can be relevant to delegated administration policy without making every command run as root.
Try it yourself
Section titled “Try it yourself”Goal: Report your effective account name and every group ID numerically. Explain which numeric group is primary by comparing with the earlier result. Do not change membership.
Hint
Add the name option to the user selection. Use the uppercase group-selection option without the name option for numeric groups.
Solution
id -unlearnerid -G1001 27The primary group is 1001 in this lab capture; 27 is supplementary. Your account’s numbers can differ. Explain the roles from the fields and options, not from memorizing these particular values.
Reset this lesson
Section titled “Reset this lesson”cd ~/lab/b11ls -AR identityrm -r ~/lab/b11/identityRerun Before you start. If the directory is absent, skip removal. The capture verifies this removal and recreation.
whoaminames the effective user;idreports numeric identities and groups.- A primary group and supplementary groups belong to the running session’s identity.
- Files have an owning user and an owning group, separate from the owner’s membership list.
- Inspection reports these facts without granting privileges or changing access.
Next, Reading permission strings connects identities to the access bits in a long listing.
Further reading
Section titled “Further reading”- On your system:
man id,man whoami, andman stat. - GNU Coreutils: id invocation, whoami invocation, and stat invocation.