Linux File Permission Cheat Sheet

✍🏼 Written on Sep 28, 2019   
❗️ Note: it has been days since this article was written, please be aware of its timeliness
🖥  Note:This blog post was originally written while learning Linux basics and has been migrated from an old WordPress blog.

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
2
3
4
drwxr-xr-x   9 x  staff   288  9 25 23:37 .
drwxr-xr-x 15 x staff 480 9 24 23:48 ..
drwxr-xr-x 16 x staff 512 9 26 08:18 .git
-rw-r--r--@ 1 x staff 68 9 13 00:34 README.md

Below is a left-to-right explanation of these characters.

Position and Character Meanings

  1. The 1st character indicates whether the current item is a file or folder: - for files, d for folders, and l for symbolic links.
  2. The 2nd to 4th characters represent the permissions of the file owner (the creator).
  3. The 5th to 7th characters represent the permissions of users in the file’s group.
  4. The 8th to 10th characters represent the permissions of other users outside the current group.
  5. In the 2nd–4th, 5th–7th, and 8th–10th positions, each set of 3 characters represents a permission group:
    1. The 1st character, ‘r’, stands for read, i.e., permission. Numerically, it’s 4 (decimal) or 100 (binary). If absent, it’s represented by -.
    2. The 2nd character, ‘w’, stands for write, i.e., permission. Numerically, it’s 2 (decimal) or 010 (binary). If absent, it’s represented by -.
    3. The 3rd character, ‘x’, stands for execute, i.e., 执行 permission. Numerically, it’s 1 (decimal) or 001 (binary). If absent, it’s represented by -.
  6. 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:

  1. SUID means the user executing the script (with execute permission) gains the file owner’s permissions (7).
  2. 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).

- EOF -
Originally published at: Linux File Permission Cheat Sheet - Xheldon Blog