CodeGym /Courses /Docker SELF /Checking current access rights: commands `whoami`, `id`

Checking current access rights: commands `whoami`, `id`

Docker SELF
Level 2 , Lesson 3
Available

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?

  1. UID (User ID) — The unique identifier for the user.
  2. GID (Group ID) — The unique identifier for the user's primary group.
  3. 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

  1. Open the terminal.
  2. Run the command:

    whoami
    

    Check your username.

  3. Run the sudo su command (you'll be asked to enter the password), and then run whoami again to see who you’re working as.

Studying User Information

  1. Run the command:

    id
    
    Review your user’s UID, GID, and which groups they belong to.
  2. Create a new user (if you have access to the sudo command):

    sudo adduser testuser
    

    Then run:

    id testuser
    

Analyzing Group Membership

  1. Run:

    groups
    

    Note which groups your user belongs to.

  2. 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

  1. A lot of newbies confuse whoami and id. Remember: whoami only shows the name of the current user, while id provides more detailed information about rights and groups.

  2. 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.

  3. 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.

1
Task
Docker SELF, level 2, lesson 3
Locked
Determining the current user
Determining the current user
1
Task
Docker SELF, level 2, lesson 3
Locked
Checking user identifiers
Checking user identifiers
1
Task
Docker SELF, level 2, lesson 3
Locked
Checking user and group information
Checking user and group information
1
Task
Docker SELF, level 2, lesson 3
Locked
Adding to a group and checking access rights
Adding to a group and checking access rights
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION