Checking current access rights: commands whoami
, id
1. Who am I in the system? Command whoami
Every Linux user operates under a specific account that has its own rights, limitations, and group memberships. In this lecture, you’ll learn how to check which account you’re working with, what your permissions are, and which groups you belong to. Knowing your current user parameters is crucial for everyday work as well as addressing security, management, and debugging matters.
When you are working in a Linux system, it’s useful to know which account you’re operating under at the moment. For instance, if you accidentally end up using a privileged user account (like root), you could make irreversible changes to system files. To avoid these situations, there’s the whoami
command.
Syntax
whoami
What does the command do?
The whoami
command outputs the name of the current user you’re working as in the system. It answers the all-important question: "Who am I?"
Example
$ whoami
student
In this case, the output student
means you are logged in as a user named student
. If you were operating as a user with elevated privileges (like root
), the output would be as follows:
$ whoami
root
Why is this important?
Imagine you use a command to delete all files in a directory (for example, rm -rf /important_directory
). As a regular user, the system might restrict you if you lack the necessary permissions. But as the root
user, it would execute your command without hesitation. The whoami
command helps you avoid such unpleasant situations.
2. Detailed User Info: The id
Command
Knowing your username is great, but it's not enough to fully understand your rights and privileges in the system. For more detailed information, we use the id
command.
Syntax
id [user]
If no username is specified, the id
command will show info about the current user. If you include another username, you can check the details of their account.
What Does the id
Command Output?
- UID (User ID) — The unique identifier for the user.
- GID (Group ID) — The unique identifier for the user's primary group.
- Group List — All groups the user belongs to (including the primary one).
Example
$ id
uid=1001(student) gid=1001(student) groups=1001(student),27(sudo),1002(developers)
- UID=1001: This is the unique identifier of the current user (student). In Linux systems, the UID of the
root
user is always 0. - GID=1001: This is the identifier for the user's primary group (student).
- Groups=1001(student), 27(sudo), 1002(developers): Here are the additional groups the user is part of. For example, the
sudo
group lets the student run commands as a privileged user.
If we run the command for another user:
$ id user2
uid=1002(user2) gid=1002(user2) groups=1002(user2),1001(developers)
You can see that the user user2
belongs to the developers
group.
3. Using UID and GID in real work
Every user and group is assigned their own numerical identifiers (UID and GID respectively). These play a crucial role within the system, often replacing the textual names of users and groups. Why is this needed?
- Some system processes work directly with numerical identifiers for efficiency.
- Access rights to files and folders are determined not by user or group names, but by their UID and GID.
- In configuration files and log journals (for example,
/etc/passwd
), you might encounter user UIDs instead of their names.
4. Exploring groups with groups
Additionally, you can use the groups
command to find out which groups the current user belongs to.
Syntax
groups [user]
If you omit the user name, you'll get a list of groups for the current account. By specifying a username, you'll find out the groups for that specific user.
Example:
$ groups
student developers sudo
$ groups user2
user2 developers
5. Working with Commands
And now a little practice to lock in the knowledge you’ve gained.
Checking the Current Account
- Open the terminal.
Run the command:
whoami
Check your username.
- Run the
sudo su
command (you'll be asked to enter the password), and then runwhoami
again to see who you’re working as.
Studying User Information
Run the command:
Review your user’s UID, GID, and which groups they belong to.id
Create a new user (if you have access to the
sudo
command):sudo adduser testuser
Then run:
id testuser
Analyzing Group Membership
Run:
groups
Note which groups your user belongs to.
Add the current user to a new group:
sudo groupadd testgroup sudo usermod -aG testgroup $(whoami)
Check the result:
groups
6. Common Mistakes and Tricky Moments
A lot of newbies confuse
whoami
andid
. Remember:whoami
only shows the name of the current user, whileid
provides more detailed information about rights and groups.If you don't have admin privileges (access to
sudo
), you might not be able to add users to groups or change their settings. In such cases, you should reach out to the system admin.The
id
command with a nonexistent user specified will throw an error:$ id nonexistinguser id: ‘nonexistinguser’: no such user
Now you know how to check which user is currently active, what rights and accesses they have. The whoami
and id
commands may seem simple, but they play a key role in understanding the user and access rights system in Linux. In real-world tasks, you'll often use them for diagnosing issues, checking rights, and configuring users.
GO TO FULL VERSION