days since this article was written, please be aware of its timeliness
Preface
Frequent command-line users often notice files preceded by a 10-character string -rw-r--r--, which represents the operational permissions for different user groups on the current file/folder. For example:
1 | |
Below is a left-to-right explanation of these characters.
Position and Character Meanings
- The 1st character indicates whether the current item is a file or folder:
-for files,dfor folders, andlfor symbolic links. - The 2nd to 4th characters represent the permissions of the file owner (the creator).
- The 5th to 7th characters represent the permissions of users in the file’s group.
- The 8th to 10th characters represent the permissions of other users outside the current group.
- In the 2nd–4th, 5th–7th, and 8th–10th positions, each set of 3 characters represents a permission group:
- The 1st character, ‘r’, stands for
read, i.e.,读permission. Numerically, it’s 4 (decimal) or 100 (binary). If absent, it’s represented by-. - The 2nd character, ‘w’, stands for
write, i.e.,写permission. Numerically, it’s 2 (decimal) or 010 (binary). If absent, it’s represented by-. - The 3rd character, ‘x’, stands for
execute, i.e.,执行permission. Numerically, it’s 1 (decimal) or 001 (binary). If absent, it’s represented by-.
- The 1st character, ‘r’, stands for
- The root user always has full permissions (rwx), numerically represented as 777 (4 + 2 + 1 per group) or 111 in binary.
Example
-r-xrwxr-- indicates the current item is a file. The owner has read and execute permissions but cannot modify it. Other users in the owner’s group can read, modify, and execute it, while other users can only read but not modify or execute it.
Basic Usage
chmod To modify the file hello.js to be readable, writable, and executable: chmod 777 hello.js or chmod rwx hello.js
Additional Notes
Permissions can also be represented numerically (without the highest-bit -/d/l information from letter-based notation) using four digits, such as 0777. The leading ‘0’ represents SUID and GUID concepts:
- SUID means the user executing the script (with execute permission) gains the file owner’s permissions (7).
- GUID means the user gains the file group’s permissions (5).
To set SUID, change the leading ‘0’ to ‘4’ (e.g., 4777). To set GUID, change it to ‘2’ (e.g., 2777). For both, use ‘6’ (e.g., 6777).
-
Previous
Data Structures and Algorithms in JavaScript -
Next
Understanding Pointers in C Language Correctly
I often wish that when facing some key decisions in life, someone could tell me the best course of action so that I would not waste my precious time. Putting myself in others' shoes, I therefore write blogs often, hoping to record in this tiny corner of the vast Internet the once-in-a-lifetime experiences that matter to me, and to help those who seek help.