Linux id Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, we will explore the Linux id command, which is a useful utility for displaying information about the current user or any other user on the system. We will understand the purpose and usage of the id command, learn how to identify user and group information, and explore additional options and customizations.

The id command allows you to display the user ID (UID), group ID (GID), and the supplementary groups that a user belongs to. You can use it to get information about the current user or any other user on the system. Additionally, the id command can be used to display the effective user ID and group ID, which are the IDs used for permission checks when executing a command.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/UserandGroupManagementGroup(["`User and Group Management`"]) linux/BasicSystemCommandsGroup -.-> linux/help("`Command Assistance`") linux/UserandGroupManagementGroup -.-> linux/groups("`Group Displaying`") linux/UserandGroupManagementGroup -.-> linux/whoami("`User Identifying`") linux/UserandGroupManagementGroup -.-> linux/id("`User/Group ID Displaying`") subgraph Lab Skills linux/help -.-> lab-422729{{"`Linux id Command with Practical Examples`"}} linux/groups -.-> lab-422729{{"`Linux id Command with Practical Examples`"}} linux/whoami -.-> lab-422729{{"`Linux id Command with Practical Examples`"}} linux/id -.-> lab-422729{{"`Linux id Command with Practical Examples`"}} end

Understand the Purpose and Usage of the id Command

In this step, we will explore the purpose and usage of the id command in Linux. The id command is a useful utility that allows you to display information about the current user or any other user on the system.

The basic usage of the id command is as follows:

$ id
uid=1000(labex) gid=1000(labex) groups=1000(labex),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),113(lxd),128(sambashare)

This command displays the user ID (UID), group ID (GID), and the supplementary groups that the current user belongs to.

You can also use the id command to display information about another user by specifying the username as an argument:

$ id alice
uid=1001(alice) gid=1001(alice) groups=1001(alice),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),113(lxd),128(sambashare)

This will show the user and group information for the user alice.

The id command can also be used to display the effective user ID and group ID, which are the IDs used for permission checks when executing a command. You can use the -e option to display the effective IDs:

$ id -e
uid=1000(labex) gid=1000(labex)

Example output:

uid=1000(labex) gid=1000(labex) groups=1000(labex),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),113(lxd),128(sambashare)

Identify the User and Group Information Using the id Command

In this step, we will learn how to use the id command to identify the user and group information for the current user and other users on the system.

First, let's verify the user and group information for the current user:

$ id
uid=1000(labex) gid=1000(labex) groups=1000(labex),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),113(lxd),128(sambashare)

This output shows the user ID (UID), group ID (GID), and the supplementary groups that the current user labex belongs to.

You can also use the id command to display information about other users on the system. For example, to get the user and group information for the user alice, you can run:

$ id alice
uid=1001(alice) gid=1001(alice) groups=1001(alice),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),113(lxd),128(sambashare)

This command will display the UID, GID, and supplementary groups for the user alice.

Additionally, you can use the -u option to display only the user ID, and the -g option to display only the group ID:

$ id -u
1000
$ id -g
1000

Example output:

uid=1000(labex) gid=1000(labex) groups=1000(labex),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),113(lxd),128(sambashare)

Explore Additional Options and Customizations of the id Command

In this final step, we will explore some additional options and customizations available with the id command.

One useful option is the -n flag, which displays the name instead of the numeric ID. For example:

$ id -nu
labex
$ id -ng
labex

This will show the username and group name instead of the numeric IDs.

You can also use the --help option to see all the available options for the id command:

$ id --help
Usage: id [OPTION]... [USER]
Print user and group information for the specified USER,
or (when USER omitted) for the current user.

  -a             print all information in a readable format
  -Z, --context  print only the security context of the process
  -g, --group    print only the effective group ID
  -G, --groups   print all group IDs
  -n, --name     print a name instead of a number, for -ugG
  -r, --real     print the real ID instead of the effective ID, with -ugG
  -u, --user     print only the effective user ID
  -z, --zero     delimit entries with NUL character, not whitespace;
                   not permitted in default format
      --help     display this help and exit
      --version  output version information and exit

Examples:
  id
  id username
  id -a
  id -u
  id -g

This shows all the available options you can use with the id command to customize the output.

Finally, you can combine multiple options to get the desired information. For example, to get the username and group name for the current user:

$ id -un
labex
$ id -gn
labex

Example output:

$ id -un
labex
$ id -gn
labex

Summary

In this lab, you learned how to use the id command in Linux to display information about the current user or any other user on the system. The id command can show the user ID (UID), group ID (GID), and the supplementary groups that the user belongs to. You also explored how to use the -e option to display the effective user ID and group ID, which are the IDs used for permission checks when executing a command. Additionally, you learned how to use the id command to identify the user and group information for other users on the system by specifying their usernames as arguments.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like