When you encounter a “Permission Denied” error in Linux while trying to execute a script or read a secure file, the first troubleshooting step is to determine exactly who the operating system thinks you are. While the whoami command prints your username, Linux security and file permissions are not based on names; they are based entirely on numerical IDs. To view your precise numerical User ID (UID) and the Group IDs (GIDs) of all the security groups you belong to, you must use the Linux id command.
Understanding UIDs and GIDs
Before using the command, it is crucial to understand how Linux handles identities:
- UID (User ID): The root administrator is always UID
0. The first normal human user created during OS installation is typically assigned UID1000, the next user is1001, and so on. Background system services (like Apache or Nginx) are usually assigned UIDs below 1000. - GID (Group ID): Every user has a primary group (which usually has the same name and number as their UID). However, a user can also belong to dozens of secondary groups. For example, belonging to the
sudoorwheelgroup grants administrator privileges, while belonging to thedockergroup allows you to run containers.
Basic Usage of the id Command
To view your own identity information, you run the command without any arguments.
- Open your terminal application.
- Type the command:
id - Press Enter.
The terminal will output a string of data formatted like this:
uid=1000(john) gid=1000(john) groups=1000(john),4(adm),27(sudo),120(docker)
This output tells you three things:
- uid=1000(john): The system sees you as user 1000. If a file is owned by UID 1000, you have permission to modify it.
- gid=1000(john): Your primary group is 1000. Any new files you create will be owned by this group.
- groups=…: You belong to several secondary groups, including
sudo(meaning you have administrative privileges) anddocker(meaning you can execute Docker containers without typing sudo).
Checking the Identity of Other Users
System administrators frequently need to verify the group memberships of other employees or background service accounts. You can do this by passing a username as an argument to the id command.
id www-data
This will print the UID and GIDs for the www-data user (the account typically used by web servers), allowing you to confirm that the web server has the correct group permissions to read your website’s files.
Extracting Specific IDs for Scripts
If you are writing a bash script that must be run as the root user, you do not want to parse the entire complex string output of the standard id command. You can use flags to extract only the specific raw numbers you need.
- Print only the UID: Use the
-uflag. This is incredibly useful for security checks.id -u(This will output simply “1000” or “0”). - Print only the primary GID: Use the
-gflag.id -g - Print all GIDs (primary and secondary): Use the
-Gflag.id -G
A common script pattern involves checking if the executing user is root (UID 0):
if [ "$(id -u)" -ne 0 ]; then
echo "Error: This script requires root privileges. Run with sudo."
exit 1
fi