11. Introduction to Permissions: chmod Basics
File permissions are crucial in Linux for security and function (especially for scripts). Permissions define who can do what with a file or directory.
Viewing Permissions (The ls -l output)
When you use ls -l, the first 10 characters define the permissions:
-rwxr-xr--
- First Character (
-ord): File type (-for file,dfor directory). - Next 3 Characters (rwx): Permissions for the User (Owner).
- Next 3 Characters (r-x): Permissions for the Group.
- Last 3 Characters (r--): Permissions for Others (Everyone else).
| Character | Meaning |
|---|---|
r | Read (view content) |
w | Write (modify or delete) |
x | Execute (run as a program/script) |
- | Permission denied |
Changing Permissions (chmod)
chmod (Change Mode) modifies permissions. We often use the Octal (numeric) notation:
| Permission | Octal Value |
|---|---|
Read (r) | 4 |
Write (w) | 2 |
Execute (x) | 1 |
Total permissions are the sum of values (e.g., rwx = 4+2+1 = 7).
Common Permission Sets:
- 777 (rwxrwxrwx): Full permission for everyone (Rarely recommended).
- 755 (rwxr-xr-x): Owner can read/write/execute; Group/Others can read/execute.
- 644 (rw-r--r--): Owner can read/write; Group/Others can only read.
Example: Making a Script Executable
To run a script, it must have execute permission (x):
bash $ touch script.sh $ ls -l script.sh -rw-rw-rw- 1 u0_a... u0_a... 0 Jun 1 10:00 script.sh
Change permissions to 755 (User: rwx, Group/Other: r-x)
$ chmod 755 script.sh
$ ls -l script.sh -rwxr-xr-x 1 u0_a... u0_a... 0 Jun 1 10:00 script.sh