File Permissions Linux
Files and directories have permissions that determine who can read, write, or execute them. These permissions are split into three categories:
User (u) - The owner of the file.
Group (g) - Users who are members of the file's group.
Others (o) - All other users.
Each category can have three types of permissions:
Read (r) - Allows the file to be read.
Write (w) - Allows the file to be modified.
Execute (x) - Allows the file to be executed.
Using chmod
To use chmod
, you need to specify the permissions you want to set, and the file or directory to which you want to apply them. There are two main ways to specify permissions: symbolic and numeric.
Symbolic Mode
Symbolic mode uses letters to represent permissions and categories. The format is [who][operator][permission]
, where:
who:
u
(user),g
(group),o
(others),a
(all)operator:
+
(add),-
(remove),=
(set exactly)permission:
r
(read),w
(write),x
(execute)
Examples:
chmod u+r file.txt
- Adds read permission for the owner.chmod g-w file.txt
- Removes write permission for the group.chmod a+x file.txt
- Adds execute permission for everyone.
Numeric Mode
Numeric mode uses a three-digit octal number to represent permissions. Each digit is a sum of permission values:
Read = 4
Write = 2
Execute = 1
Examples:
chmod 755 file.txt
- Sets permissions torwxr-xr-x
(read, write, execute for owner; read, execute for group and others).chmod 644 file.txt
- Sets permissions torw-r--r--
(read, write for owner; read-only for group and others).
Here's a quick reference table:
Octal Value | Permissions | Description |
7 | rwx | Read, write, execute |
6 | rw- | Read, write |
5 | r-x | Read, execute |
4 | r-- | Read only |
3 | -wx | Write, execute |
2 | -w- | Write only |
1 | --x | Execute only |
0 | --- | No permissions |