Linux / Unix id Command Examples

last updated in Categories , ,
id command

I am a new Linux and Unix system user. How do I find out the user and groups names and numeric IDs of the current user or any users on my server? How can I display and effective IDs on the system using command line options? In Linux, how do I find a user’s UID or GID?

To find a user’s UID (user ID) or GID (group ID) and other information in Linux/Unix-like operating systems, use the id command.

This command is useful to find out the following information:

  1. Get User name and real user ID
  2. Find a specific user’s UID
  3. List out all the groups a user belongs to
  4. Show the UID and all groups associated with a user
  5. Display security context of the current user
  6. Effective Linux or Unix user name and effective user ID (UID)
  7. Name of effective Linux or Unix user’s group and effective group ID (GID)

Purpose

Displays the system identifications of a specified user.

id command syntax

The basic syntax is:

id
id [UserNameHere]
id [options]
id [options] [UserNameHere]

By default id command shows the the user and group names and numeric IDs, of the calling process i.e. the current user who runs the id command on screen. If a login name or user ID ([UserNameHere]) given on command line, the user and group IDs of that user are displayed.

Linux / Unix id command examples

Let us see how to find a user’s UID or GID on Linux or Unix-like operating systems using 13 id command practical examples. First, open the Terminal application and then type:

Display your own UID and GID

Type the command:
id
Sample outputs:

Fig. 01: id command in action
Fig. 01: id command in action

How do I find a specific user’s UID?

In this example, find a vivek user’s UID, type:
id -u {UserNameHere}
id -u vivek

Sample outputs:

501

How do I find a specific user’s GID?

In this example, find a vivek user’s GID, run:
id -g {UserNameHere}
id -g vivek

Sample outputs:

20

How do I see the UID and all groups associated with a user name?

In this example, find the UID and all groups associated with a user called ‘root’, enter:
id {UserNameHere}
id root

Sample outputs:

uid=0(root) gid=0(wheel) groups=0(wheel),1(daemon),2(kmem),3(sys),4(tty),5(operator),8(procview),9(procmod),12(everyone),20(staff),29(certusers),61(localaccounts),80(admin),33(_appstore),98(_lpadmin),100(_lpoperator),204(_developer),398(com.apple.access_screensharing),399(com.apple.access_ssh)

Find out all the groups a user belongs to…

In this example, display the UID and all groups associated (secondary groups) with a user called ‘vivek’, run:
id -G {UserNameHere}
id -G vivek

Sample outputs

20 12 61 79 80 81 98 33 100 204 398 399

Display a name instead of a UID/GID

By default, id command displays number for the -G, -g and -u options. You can force id command to display the name of the UID or GID instead of the number for the -G, -g and -u options by passing the -n option as follows:
id -ng {UserNameHere}
id -nu {UserNameHere}
id -nG {UserNameHere}
id -nG vivek

Sample outputs:

staff everyone localaccounts _appserverusr admin _appserveradm _lpadmin _appstore _lpoperator _developer com.apple.access_screensharing com.apple.access_ssh

How do I display real ID instead of the effective ID for specified user?

You can show the real ID for the -g, -G and -u options instead of the effective ID by passing the -r option:

id -r -g {UserNameHere}
id -r -u {UserNameHere}
 
### [NOTE]###########################
### -r and -G only works on Linux ###
#####################################
id -r -G {UserNameHere}
 
id -r -u vivek

Sample outputs:

501

How do I display SELinux show security context on Linux?

To display only the security context of the current user, type:

### [NOTE]###########################
### This is a Linux only option   ###
#####################################
id -Z

Sample outputs:

unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023

Unix specific feature: Show process audit user ID

To display the process audit user ID and other process audit properties, which requires privilege. This option works on a few Unix-like operating systems such as FreeBSD or Unix:

### [NOTE]###########################
### This is a Unix only option    ###
#####################################
id -A

Output is displayed in the following format from my Apple OS X/FreeBSD:

auid=501
mask.success=0xffffffff
mask.failure=0xffffffff
termid.port=0x03000002
asid=100004

Shell variables related to UID and EID

Modern shell such as bash/ksh may set the following variables:

  • EUID – Expands to the effective user ID of the current user, initialized at shell startup. This variable is readonly.
  • UID – Expands to the user ID of the current user, initialized at shell startup. This variable is readonly.

You can print them using echo or printf shell commands as follows:

echo "Your UID : $UID"
echo "Your EID : $EUID"

Sample outputs:

Your UID : 501
Your EID : 501

Your shell use id command to set EUID and UID in the system-wide startup file such as /etc/profile. Here is a snippet from Linux based system:

....
..
 
if [ -x /usr/bin/id ]; then
    if [ -z "$EUID" ]; then
        # ksh workaround
        EUID=`id -u`
        UID=`id -ru`
    fi
    USER="`id -un`"
    LOGNAME=$USER
    MAIL="/var/spool/mail/$USER"
fi
 
....
..

Determining root privileges in a script

Linux and Unix sysadmin relates shell scripts must be run by root user. The following shell script shows how to determining root privileges in a script:

#!/bin/bash
 
## if root user not running this script, die with a message on screen ##
if [ $(id -u -r) -ne 0 ]
then
        echo "Requires root privileges. Please re-run using sudo."
        exit 1
fi
## add sysadmin related command below ##

Or you can use EUID as described above in a shell script:

(( EUID )) && { echo 'Requires root privileges. Please re-run using sudo.'; exit 1; }
           || echo 'Running script as root...'

id command options

OptionPurposeOS
-gDisplay only the effective group IDALL
-GDisplay all group IDsALL
-uDisplay only the effective user IDALL
-nDisplay a name instead of a number, for -u or -gALL
-rDisplay the real ID instead of the effective ID, with -u or -gALL
-ZShow only the security context of the current userSELinux
-ADisplay the process audit user ID and other process audit propertiesSelected Unix-like

Related media

This tutorials is also available in a quick video format:

See also
  • id(1) Linux command man page
CategoryList of Unix and Linux commands
File Managementcat
Network Utilitiesdighostip
Package Managerapkapt
Processes Managementbgchrootdisownfgjobskillkillallpwdxtimepidofpstree
Searchingwhereiswhich
User Informationidgroupslastlastcommlognameuserswwhowhoamilid/libuser-lidmembers

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Start the discussion at www.nixcraft.com

Historical Comment Archive

1 comment

  1. How can I specifies that the id command command show the login ID instead of the real or effective ID on Linux?

    Have a question? Post it on our forum!