Linux file permissions are important for managing access to files and folders. There are three main permission types: read (r), write (w), and execute (x). These permissions are given to three groups: the owner, the group, and others (everyone else).
Here's a breakdown of Linux file permissions:
Symbolic Representation:
r
: Read permissionw
: Write permissionx
: Execute permission-
: No permission
Numeric Representation:
4
: Read permission2
: Write permission1
: Execute permission0
: No permission
Permission Categories:
Owner (u):
r
: Read permission (4)w
: Write permission (2)x
: Execute permission (1)
Group (g):
r
: Read permission (4)w
: Write permission (2)x
: Execute permission (1)
Others (o):
r
: Read permission (4)w
: Write permission (2)x
: Execute permission (1)
Examples:
Symbolic Representation:
rwxr-xr--
: Read, write, and execute for the owner; read and execute for the group; read-only for others.rw-r--r--
: Read and write for the owner; read-only for the group and others.rwxrwxr-x
: Read, write, and execute for the owner and group; read and execute for others.
Numeric Representation:
700
: Read, write, and execute for the owner; no permissions for the group and others.644
: Read and write for the owner; read-only for the group and others.775
: Read, write, and execute for the owner and group; read and execute for others.
Changing Permissions:
chmod Command:
To add permission, use
+
.To remove permission, use
-
.To set permission explicitly, use
=
.
Example:
chmod +x file.txt # Add execute permission to file.txt
chmod u=rw,go=r file.txt # Set explicit permissions
chown Command:
Change file owner:
chown new_owner:new_group file.txt
Change only the group:
chown :new_group file.txt
chgrp Command:
- Change the group:
chgrp new_group file.txt
- Change the group:
View Permissions:
ls Command:
ls -l
: Displays detailed information, including permissions.ls -la
: Displays hidden files as well.
Special Permissions:
Setuid (SUID):
Allows a user to execute a file with the permissions of the file owner.
Displayed as an
s
in the owner's execute position:-rwsr-xr-x
.
Setgid (SGID):
Similar to SUID but applies to directories. Files created in the directory inherit the group of the parent directory.
Displayed as an
s
in the group's execute position:-rwxr-sr-x
.
Sticky Bit:
Restricts the deletion of files in a directory to the file owner, the directory owner, or the root user.
Displayed as a
t
in the others' execute position:-rwxr-xr-t
.
Examples of Setting Special Permissions:
chmod u+s file.txt
: Set the setuid bit.chmod g+s directory
: Set the setgid bit for a directory.chmod +t directory
: Set the sticky bit for a directory.
These basic ideas are important for knowing how to handle file access in Linux systems. Be careful when changing permissions, especially for important system files and folders.