Display User Information Within Linux Using The "id" Command

Display User Information With id Command
Display User Information With id Command.

Introduction

This guide will show you how to print out information about the current user including the groups they belong to.

If you want to show system information you can use the uname command.

id (Display Complete User Information)

On its own the id command prints a lot of information:

  • user id
  • username
  • group id
  • group name
  • id of other groups
  • names of other groups

You can run the id command as follows:

id

The id command will reveal all the information about the current user but you can also specify the name of another user.

For example:

id fred

id -g (Display Primary Group ID For A User)

If you want to find the primary group id for the current user type the following command:

id -g

This will list just the group id such as 1001.

You might be wondering what a primary group is. When you create a user, for example fred, they are assigned a group based on the settings of the /etc/passwd file. When that user creates files they will be owned by fred and assigned to the primary group. If other users are given access to the group they will have the same permissions as other users within that group.

You can also use the following syntax for viewing the primary group id:

id --group

If you want to see the primary group id for a different user specify the user's name:

id -g fred

id --group fred

id -G (Display Secondary Group ID For A User)

If you want to find the secondary groups a user belongs to type the following command:

id -G

The output from the above command will be along the lines of 1000 4 27 38 46 187.

As mentioned previously a user is assigned to a single primary group but they can also be added to secondary groups. For example fred might have a primary group of 1001 but he might also belong to groups 2000 (accounts), 3000 (managers) etc.

You can also use the following syntax for viewing the secondary group ids.

id --groups

If you want to see te secondary group id for a different user specify the user's name:

id -G fred

id --groups fred

id -gn (Display Primary Group Name For A User)

Displaying the group id is fine but as human beings it is much easier to understand things when they are named.

The following command shows the name of the primary group for a user:

id -gn

The output for this command on a standard Linux distribution is likely to be the same as the username. For example fred.

You can also use the following syntax for viewing the group name:

id --group --name

If you want to see the primary group name for another user include the user's name in the command:

id -gn fred

id --group --name fred

id -Gn (Display Secondary Group Name For A User)

If you want to display the secondary group names and not the id numbers for a user enter the following command:

id -Gn

The output will be something along the lines of fred adm cdrom sudo sambashare.

You can get the same information by using the following syntax:

id --groups --name

If you want to see the secondary group names for another user specify the user's name in the command:

id -Gn fred

id --groups --name fred

id -u (Display User ID)

If you want to display the user id for the current user type in the following command:

id -u

The output from the command will be something along the lines of 1000.

You can achieve the same effect by typing the following command:

id --user

You can find out the user id for another user by specifying the user's name as part of the command:

id -u fred

id --user fred

id -un (Display User Name)

You can display the user name for the current user by typing the following command:

id -un

The output from the above command will be something along the lines of fred.

You can also use the following command to display the same information:

id --user --name

There is little point in supplying another user's name to this command.

Summary

The main reason to use the id command is to find out what groups a user belongs to and sometimes to find out which user you are logged in as especially if you use the su command to switch between users.

In the latter case you can use the whoami command to find out who you are logged in as and you can use the groups command to find out which groups a user belongs to.

The su command should only be used if you need to run a number of commands as a different user. For ad-hoc commands you should use the sudo command.

More From Us