How To Find UID And GID Using Id Command In Linux/Unix?

0/5 No votes

Report this app

Description

Linux/Unix are multi-user operating systems that means different users can access and use system resources simultaneously. To access the resources of a system you must have a user account on that system. And a group is a collection of users.

A system identifies users and groups with a UID and GID respectively. In this article, we will discuss to find the UID and GID from Linux.

UID (User Identifier)

UID is a number assigned to each user in a Linux or Unix system. The system identifies a user with its UID and uses it to determine which system resources a user can access. For example – the root user has UID 0.

GID (Group Identifier)

A group is a collection of users having similar permissions. Like a user who is identified by the UID, a group is identified by the GID in a system. GID 0 is reserved for the root group.

The id Command

Unix like operating system provides id command to display the UID and GID of a user. If the command is executed without any argument, it will print the user information of the current user. Following is the syntax of id command –

id [option] username

You can use the following options with this command –

Options Description
-g, --group It will print only effective group IDs
-G, --groups Display all group IDs
-n, --name Prints a name instead of a number for, -ugG
-u, --user Print only effective user IDs
-r, --real Display the real ID instead of the effective ID, with -ugG
-z, --zero Delimit entries with NUL characters, not whitespaces
-a Ignore the compatibility with other versions
-Z, --context Print only the security context of the process
--version Output the version information and exit

For more details, you can see the manual page of id command by using –

man id

How to display the UID and GID of the current user?

To display the user ID and Group ID of the current user, use the id command without any arguments –

id

Now here you can see the UID and GID of user lalit  both are 1000

How to display the UID and GID of a specific user?

To display the user ID of a specific user use the option -u with the id command, Run the following command –

id -u username

For example –

id -u lalit

This will display the user ID of lalit.

And to display the group ID use -g option with id command –

id -g lalit

This will display the group ID of lalit.

How to list all the groups a user belongs to?

Use the option -G with id command to display the list of the groups a user belongs to –

id -G lalit

Or use the option -n to display name instead of GID –

id -nG lalit

Is there any other way to display the UID and GID of a user?

The information such as UID, GID, default shell, etc is stored in /etc/passwd file. You can display the content of this file by using the following command in your terminal –

cat /etc/passwd | less

Now you will see a list of users with some other information. There are seven places separated by a colon, third and fourth places are UID and GID respectively. For example – UID and GID of the root user are 0 and 0.

That’s all for now. If you have any queries or suggestions related to this topic then you can write to us in the comments.

Leave a Reply

Your email address will not be published. Required fields are marked *