Linux File Permissions Cheat Sheet
Understanding File Permissions
Each file and directory in Linux has permissions assigned to three categories:
- Owner (u): The user who owns the file.
- Group (g): The group associated with the file.
- Others (o): Everyone else.
Permissions are represented as:
r - Read | w - Write | x - Execute
Example: -rwxr--r--
Breakdown: The owner has read (r), write (w), and execute (x) permissions, the group has only read (r), and others have only read (r).
Changing Permissions with chmod
chmod 755 filename - Sets read, write, and execute for owner, and read & execute for group and others.
chmod u+x filename - Adds execute (x) permission to the owner.
chmod g-w filename - Removes write (w) permission from the group.
chmod o+r filename - Adds read (r) permission for others.
chmod a+x filename - Adds execute (x) permission for everyone.
Changing Ownership with chown
chown user:group filename - Changes file owner and group.
chown user filename - Changes only the owner of the file.
chown :group filename - Changes only the group of the file.
chown -R user:group directory/ - Recursively changes ownership for all files in a directory.
Special Permissions
chmod 4755 filename - Set user ID (SUID), allowing execution as the file owner.
chmod 2755 filename - Set group ID (SGID), making files inherit the group of the parent directory.
chmod 1755 directory - Sticky bit, preventing others from deleting files they don't own.