id command in Linux with Examples


id - print real and effective user and group IDs


id [OPTION]... [USER]


Print user and group information for the specified USER, or (when USER is omitted) for the current user.


The options for id commands are:

-a   ignore, for compatibility with other versions

-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 characters, not whitespace;

   not permitted in default format

--help display this help and exit

   output version information and exit


1. The id command without any arguments shows the the user and group names and numeric IDs, of the calling process i.e. the current user who runs the command on screen.

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

2. To obtain information about another user, pass the username or user ID as an argument to the id command.

$ id rajat
uid=1500(rajat) gid=1500(rajat) groups=1500(rajat)

3. Use -u option to shows the user's effective user ID.

$ id -u expert

4. Use -g option to shows the user's effective group ID.

$ id -g expert

5. The id command with -G option displays the user's effective group ID and all groups associated (secondary groups).

$ id -G expert
1000 4 24 27 30 46 113 128

6. The id command with -n option displays the user's name instead of numbers. This option can be used only in combination with -u, -g, and -G options.

$ id -nG expert
expert adm cdrom sudo dip plugdev lpadmin sambashare

7. The id command with -z option delimits entries with NUL characters, not whitespace. This is used to pass the output of the command to another program for better processing. We can verify this by passing the output of id command to hexdump command that show NUL characters in the output.

$ id -z -nG expert
$ id -z -nG expert|hexdump -c
0000000   e   x   p   e   r   t  \0   a   d   m  \0   c   d   r   o   m
0000010  \0   s   u   d   o  \0   d   i   p  \0   p   l   u   g   d   e
0000020   v  \0   l   p   a   d   m   i   n  \0   s   a   m   b   a   s
0000030   h   a   r   e  \0