chmod
¶
The chmod
command allows you to change the permissions of a file or directory.
You can either use symbolic notation for permissions, or use the octal notation for
specifying the permissions.
Octal notation is a concise way to set permissions on a file or directory.
It's based on the octal (base-8) numbering system, which uses digits from 0
to 7
.
Basics of Octal Notation¶
In octal notation, permissions are represented by a three- or four-digit number.
Each digit represents a permission set:
- The first digit sets permissions for the owner.
- The second digit sets permissions for the group.
- The third digit sets permissions for others (everyone else).
If present, a fourth digit at the beginning sets special permissions (like the setuid
,
setgid
, and sticky
bits).
Permission Values¶
Each digit in octal notation is the sum of its component permissions:
- Read (
r
) has a value of4
- Write (
w
) has a value of2
- Execute (
x
) has a value of1
Examples¶
-
Read, Write, and Execute for Owner; Read and Execute for Group and Others
chmod 755 file
- Owner:
rwx
(4 + 2 + 1 = 7
) - Group:
r-x
(4 + 0 + 1 = 5
) - Others:
r-x
(4 + 0 + 1 = 5
)
- Owner:
-
Read and Write for Owner; Read for Group and Others
chmod 644 file
- Owner:
rw-
(4 + 2 + 0 = 6
) - Group:
r--
(4 + 0 + 0 = 4
) - Others:
r--
(4 + 0 + 0 = 4
)
- Owner:
-
Special Permissions To set the
setuid
,setgid
, andsticky
bits, you can use a fourth digit (at the beginning of the number set):chmod 1755 file
- Special bits: 1
- Owner: rwx (7)
- Group: r-x (5)
- Others: r-x (5)
Special Permissions¶
Special permissions like setuid, setgid, and sticky bits are advanced file permissions that provide additional control over file execution and access.
setuid
(Set User ID)¶
- Octal Value:
4
- Symbolic Notation: s in the owner's execute field
- Effect: When a file with setuid is executed, it runs with the permissions of the file's owner, not the user who ran it.
Example:
chmod 4755 my_script
setgid
(Set Group ID)¶
- Octal Value:
2
- Symbolic Notation:
s
in the group's execute field - Effect: Similar to setuid but for the group. When a file with setgid is executed, it runs with the permissions of the file's group.
Example:
chmod 2755 my_script
sticky
Bit¶
- Octal Value:
1
- Symbolic Notation:
t
in the others' execute field - Effect: Mostly used on directories. It allows a file to be deleted only by its owner, the directory owner, or the root user, even if the directory has write permissions for others.
Example:
chmod 1755 my_directory
Combining Special Permissions¶
You can combine these special permissions by adding their octal values:
- Setuid + Setgid:
4 + 2 = 6
- Setuid + Sticky Bit:
4 + 1 = 5
- Setgid + Sticky Bit:
2 + 1 = 3
- All three:
4 + 2 + 1 = 7
Example:
chmod 6755 my_script