id Command Examples

Print User and Group Identity Information

About id

The id command displays user and group identity information for the specified user, or the current user if no username is specified. It shows the real and effective user ID (UID), group ID (GID), and all supplementary groups the user belongs to. This information is fundamental to understanding Linux permissions, security contexts, and access control.

Key Information Displayed:

Note: The id command is part of the GNU coreutils package and is available on all Linux systems by default. It requires no special privileges to run for viewing your own information.

Command Syntax

id [OPTION]... [USER]

Common Options

Option Description
-u, --user Print only the user ID
-g, --group Print only the primary group ID
-G, --groups Print all group IDs
-n, --name Print name instead of number (use with -u, -g, or -G)
-r, --real Print real ID instead of effective ID
-a Ignored, for compatibility with other versions
-Z, --context Print security context (SELinux)
--help Display help message
--version Display version information

Detailed Examples

Example 1: Display Current User Information

id
uid=1000(craig) gid=1000(craig) groups=1000(craig),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),116(lxd)
Without any arguments, id displays complete information about the current user:
  • uid=1000(craig) - User ID is 1000, username is craig
  • gid=1000(craig) - Primary group ID is 1000, group name is craig
  • groups=... - All groups: primary group (1000) plus supplementary groups (adm, cdrom, sudo, dip, plugdev, lxd)
This is the most common use case for checking your current identity and group memberships.
Tip: The first group listed after "groups=" is always your primary group. All subsequent groups are supplementary groups that grant additional permissions.

Example 2: Display Information for Another User

id apache
uid=48(apache) gid=48(apache) groups=48(apache)
Displays identity information for the user "apache" (web server user). This is useful for:
  • Verifying service account configurations
  • Troubleshooting permission issues for system services
  • Auditing user and group memberships
  • Understanding which groups grant access to resources
System service accounts typically have matching UID and GID, and minimal group memberships for security.

Example 3: Print Only User ID

id -u
1000
Returns only the numeric user ID of the current user. This is extremely useful in scripts:
  • Checking if running as root: if [ $(id -u) -eq 0 ]; then ...
  • Storing UID in variables: CURRENT_UID=$(id -u)
  • Conditional execution based on user identity
  • Building file paths with UID components
This clean numeric output makes it perfect for programmatic use.
Tip: Root user always has UID 0. System accounts typically have UIDs below 1000, while regular users start at 1000 (or 500 on older systems).

Example 4: Print Only Group ID

id -g
1000
Returns only the numeric primary group ID. The primary group is used for:
  • Default group ownership of newly created files
  • Process group context
  • Default permissions calculation
In scripts, you might use: chown $(id -u):$(id -g) filename to set ownership to current user and group.

Example 5: Print All Group IDs

id -G
1000 4 24 27 30 46 116
Displays numeric IDs of all groups (primary and supplementary). This is useful for:
  • Checking group membership in scripts
  • Verifying security group assignments
  • Auditing access control configurations
  • Troubleshooting group-based permissions
The first number is the primary group, followed by all supplementary groups.

Script usage:

if id -G | grep -q "\b27\b"; then
echo "User is in sudo group"
fi

Example 6: Print User Name Instead of ID

id -un
craig
Prints the username instead of the numeric UID. The -n option changes numeric output to names and must be combined with:
  • -u for username
  • -g for primary group name
  • -G for all group names
This is useful in scripts where you need the name rather than the number, such as in log messages or user notifications.
Tip: The command whoami is equivalent to id -un but more memorable for interactive use.

Example 7: Print Primary Group Name

id -gn
craig
Returns the name of the primary group instead of its numeric GID. Common uses include:
  • Display group context in prompts or scripts
  • Verify correct group assignment
  • Build human-readable log messages
  • Create backup paths with group names

Example in backup script:

BACKUP_DIR="/backups/$(id -un)/$(id -gn)"
mkdir -p "$BACKUP_DIR"
This creates a backup directory structure based on username and group name.

Example 8: Print All Group Names

id -Gn
craig adm cdrom sudo dip plugdev lxd
Lists all group names (primary and supplementary) in human-readable format. This is excellent for:
  • Auditing user permissions
  • Documentation and reports
  • Security compliance checking
  • Onboarding/offboarding verification
Understanding these groups helps identify what resources and privileges a user has access to.
Note: Common privileged groups include sudo (can use sudo), wheel (administrative access on some systems), docker (can use Docker), and adm (can read system logs).

Example 9: Check SELinux Security Context

id -Z
unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
Displays the SELinux security context (on SELinux-enabled systems like RHEL, CentOS, Fedora). The context consists of:
  • User: unconfined_u (SELinux user)
  • Role: unconfined_r (role in SELinux policy)
  • Type: unconfined_t (type enforcement domain)
  • Level: s0-s0:c0.c1023 (MLS/MCS security level)
This is critical for troubleshooting SELinux permission denials and understanding security policy constraints.
Note: On systems without SELinux (like Debian/Ubuntu by default), this option will not produce output or may show an error.

Example 10: Comprehensive Security Audit Script

#!/bin/bash
# User Security Audit Script

echo "=== User Identity Audit ==="
echo "Username: $(id -un)"
echo "User ID: $(id -u)"
echo "Primary Group: $(id -gn) (GID: $(id -g))"
echo "All Groups: $(id -Gn)"
echo ""

# Check for privileged access
if id -Gn | grep -q "sudo\|wheel"; then
echo "⚠ User has administrative privileges"
fi

# Check if running as root
if [ $(id -u) -eq 0 ]; then
echo "⚠ Running as ROOT user"
fi

# Display other users
echo ""
echo "=== Audit for user: apache ==="
id apache 2>/dev/null || echo "User 'apache' not found"
This comprehensive script demonstrates multiple uses of the id command:
  • Displays complete identity information in readable format
  • Checks for privileged group membership (sudo/wheel)
  • Detects root execution for security warnings
  • Audits service accounts
  • Handles errors gracefully with conditional execution
This pattern is commonly used in system administration scripts, security audits, and deployment automation.

Understanding User and Group IDs

UID Ranges and Meanings

UID Range Purpose Examples
0 Root user (superuser) root
1-99 System users (static) bin, daemon, sys
100-999 System users (dynamic) apache, mysql, postfix
1000-59999 Regular users craig, john, admin
60000-65535 Reserved/special use nobody (65534)
Note: On older systems, regular user UIDs may start at 500 instead of 1000. The exact ranges are configured in /etc/login.defs.

Real-World Use Cases

Use Case 1: Verify Script is Running as Root

if [ $(id -u) -ne 0 ]; then
echo "This script must be run as root" >&2
exit 1
fi
Many system administration scripts require root privileges. This check ensures the script exits early with a clear error message if not run as root.

Use Case 2: Drop Privileges in Scripts

#!/bin/bash
# Start as root, then drop to regular user
if [ $(id -u) -eq 0 ]; then
# Run command as specific user
sudo -u craig ./application
else
./application
fi
Security best practice: start with elevated privileges for setup, then drop to unprivileged user for running the application.

Use Case 3: Check Group Membership

if id -Gn | grep -qw "docker"; then
docker ps
else
echo "You must be in the docker group to run Docker commands"
exit 1
fi
Verifies the user is in the docker group before attempting Docker operations. The -w flag ensures whole word matching (prevents "docker" from matching "docker-users").

Use Case 4: Create User-Specific Directories

USER_DIR="/var/app/users/$(id -un)"
mkdir -p "$USER_DIR"
chown $(id -u):$(id -g) "$USER_DIR"
chmod 700 "$USER_DIR"
Creates a secure directory for the current user with proper ownership and permissions. Only the user can access their directory (700 permissions).

File Locations and Configuration

Related System Files

File Purpose
/etc/passwd User account information (UID, primary GID, home directory, shell)
/etc/group Group information and supplementary group memberships
/etc/shadow Encrypted passwords (readable only by root)
/etc/gshadow Secure group account information
/etc/login.defs Configuration for UID/GID ranges and password policies

Troubleshooting Common Issues

Issue: User not in expected group

Problem: Running id doesn't show a group you were just added to.

Solution: Group changes require a new login session. Either:
  • Log out and log back in
  • Use newgrp groupname to start a new shell with the group
  • Use su - $USER to start a new session
Tip: After adding a user to a group with usermod -aG groupname username, the user must log out and back in for the change to take effect.

Issue: id command shows different user

Problem: After using su or sudo, id shows unexpected results.

Solution: Understand the difference between:
  • su username - Changes user but keeps environment
  • su - username - Full login shell with user's environment
  • sudo command - Runs command as root but keeps your groups
  • sudo -u user command - Runs command as specified user

Advanced Tips

Scripting Tip: Always quote the output of id commands in scripts: USER="$(id -un)" to handle edge cases with special characters.
Security Tip: Regularly audit user group memberships, especially for privileged groups like sudo, wheel, and docker. Unauthorized membership can be a security risk.
Performance Tip: The id command is extremely fast and causes minimal system overhead, making it safe to use frequently in scripts without performance concerns.
Warning: Never hardcode UIDs or GIDs in scripts when possible. Use id or name-based permissions to make scripts portable across systems where IDs may differ.

Related Commands

Comparison with Similar Commands

Command Output Use Case
id Complete UID, GID, and groups Full identity information
whoami Just username Quick username check
groups List of groups Check group memberships
id -u Numeric UID Scripts checking for root (UID 0)