Understanding Linux File Permission Numbers
In Linux, file permissions are commonly displayed as nine characters (e.g., drwxr-xr-x), but they can also be set using a three-digit numeric code. This numeric representation is fundamental for efficient permission management.
How Permission Numbers Are Constructed
Permissions are defined for three user categories:
- Owner (User): The file's creator/owner.
- Group: Users belonging to the file's group.
- Others: All other users on the system.
Each category can have three types of access, each with a numeric value:
- Read (r) = 4
- Write (w) = 2
- Execute (x) = 1
By adding the values for the desired permissions for each user category, you get a number from 0 to 7. The three category numbers form the three-digit permission code.
Common Permission Number Examples
Here are frequently used numeric permissions and their symbolic equivalents:
444 r--r--r-- Read-only for all users.
600 rw------- Read & write for owner only.
644 rw-r--r-- Owner: read/write; Group & Others: read-only.
666 rw-rw-rw- Read & write for all (no execute).
700 rwx------ Full access for owner only.
744 rwxr--r-- Owner: full; Group & Others: read-only.
755 rwxr-xr-x Owner: full; Group & Others: read & execute.
777 rwxrwxrwx Full access (read, write, execute) for everyone.
The leading character in symbolic notation (- for a regular file, d for a directory) indicates the file type. The numeric code itself does not specify type.
Calculating Numeric Permissions
Take 755 as an example:
- The first digit
7(4+2+1) is for the Owner:rwx(read, write, execute). - The second digit
5(4+0+1) is for the Group:r-x(read, execute, no write). - The third digit
5(4+0+1) is for Others:r-x(read, execute, no write).
Thus, the command chmod 755 filename sets these exact permissions.
Setting Permissions with chmod
The basic syntax for the chmod command is:
chmod [options] numeric_mode file_or_directory
The numeric_mode is the three-digit number. For example:
chmod 777 filegrants read, write, and execute to all users.chmod 644 filegives the owner read/write, and others read-only.
Special Permission Bits (The Fourth Digit)
You may encounter a four-digit code like 4755. The leading digit sets special flags:
- 4 (SUID): Set User ID. When an executable with SUID is run, it executes with the file owner's privileges, not the runner's. Warning: Use with caution due to security implications.
- 2 (SGID): Set Group ID. On a directory, new files inherit the directory's group.
- 1 (Sticky Bit): On a directory (e.g.,
/tmp), only the file owner or root can delete/rename files within it.
Mastering numeric permissions allows for quick and precise file and directory management with the chmod command.