Skip to main content

User Identification

How to Use the id Command in Linux: 5 Useful Examples

Every user in Linux has a unique, numeric user ID and a default group with a unique numeric group ID. The id command prints this information.

The id command in Linux is used for displaying the real and effective user ID and group ID of a user.

In this tutorial, I'll show you how to use id command in Linux with some of the most common and useful examples.

id command in Linux

This is the syntax for the id command:

id [options] [username]

If you don't provide a username, the command displays the details about the currently logged-in user.

id

Here's the output it displayed for me in Ubuntu Linux.

abhishek@linuxhandbook:~$ id
uid=1000(abhishek) gid=1000(abhishek) groups=1000(abhishek),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),119(lpadmin),130(lxd),131(sambashare)

In the above output, user abhishek has uid 1000 and gid 1000. That's the primary group the user abhishek belongs to by default.

Apart from that, the user abhishek is also member of certain other groups and those groups have also been displayed in the output.

Examples of id command

Here are the most common options for the id command:

Option Description
-u Print the effective user id
-g Print the effective group id
-G Print the IDs of all groups user belongs to
-n Print names instead of IDs (must be combined with -u, -g or -G)
-r Print real ID instead of effective IDs (must be combined with -u, -g or -G)

Real vs Effective user and group ID?

This could be confusing for you. When a user is created, it is given a username, a user ID (uid), a default group and the id of that default group is the gid for that user. This is the 'real' user and group ID.

Since in Linux, processes can be run as other user and group, there is also a second set of IDs called effective IDs.

Most of the time the real and effective UIDs and GIDs are the same. But there are situations when a regular user has to modify a privileged file. This is where the effective ID concept is used. Most common example is the using passwd command to change the password which modifies the /etc/passwd file owned by root.

I cannot go in detail here but I recommend reading about it here. You should also read about SUID, GUID and sticky bit permissions in Linux.

1. Print the UID and GID of a certain user

To print the uid and gid and all the other group IDs of a user, you just have to specify the username:

id username

You can list all the users in your Linux system to get the desired user name.

What is UID in Linux? How to Find UID of a User?
This Linux Basics guide teaches you everything important associated with UID in Linux.

2. Print only the UID of a user

You can use the -u option to print the UID in Linux. As mentioned previously, if you omit the username, it displays the information about the logged-in user.

id -u userame

Keep in mind that it displays only the UID, the numeric value, not the name.

abhishek@linuxhandbook:~$ id -u
1000

3. Print only the GID of a user

Similarly, you can use the option -g to print the GID of a user. When I say GID, it's the numeric ID of the default group the user belongs to.

id -g username

Again, it will only display the numeric ID of the group.

abhishek@linuxhandbook:~$ id -g nobody 
65534

4. Print the IDs of all other groups the user belongs to

A user can belong to several groups. This is basic feature of the Linux filesystem. You can use the usermod command and add it to sudo group to give it root privileges.

You can use the -G option to display the IDs of all the groups the user belongs to.

id -G username

This too displays the ID only.

abhishek@linuxhandbook:~$ id -G abhishek
1000 4 24 27 30 46 119 130 131

5. Print names instead of the numeric IDs

The numeric IDs only can be confusing. If you want the names, you can combine the -n option with -u, -g or -G to display the names.

For example, if you want to display all the groups a user belongs to, you can use it like this:

id -nG username

If I use the previous example with option -n here's what it shows:

abhishek@linuxhandbook:~$ id -nG abhishek
abhishek adm cdrom sudo dip plugdev lpadmin lxd sambashare

Bonus Tip: Print the real IDs

All the above examples display the effective IDs. Almost all the time it will be the real ID.

However, if you want to make sure to get the real ID, you can combine option -r with -u, -g or -G.

It can be used like this:

id -ru username

Alright! I think you know enough about the id command now. If you have questions or suggestions, feel free to leave a comment.