In Linux, file and directory permissions are crucial for maintaining
security and proper access control. Below is a comprehensive
explanation of the key commands and keywords related to
permissions in Linux:
In Linux, file permissions are represented numerically for setting or
viewing purposes. Each permission is associated with a specific
number:
• Read (r): 4
• Write (w): 2
• Execute (x): 1
Numeric Representation
Permissions are expressed as a three-digit number, where each digit
is the sum of its component permissions:
• Owner (User): The first digit
• Group: The second digit
• Others: The third digit
Calculation Example:
• Read and Write: 4 (read) + 2 (write) = 6
• Read and Execute: 4 (read) + 1 (execute) = 5
• Write and Execute: 2 (write) + 1 (execute) = 3
• Read, Write, and Execute: 4 (read) + 2 (write) + 1 (execute) = 7
Example Usage
• chmod 755 filename:
o 7 (Owner: rwx)
o 5 (Group: r-x)
o 5 (Others: r-x)
• chmod 644 filename:
o 6 (Owner: rw-)
o 4 (Group: r--)
o 4 (Others: r--)
Understanding these numeric values helps in efficiently setting
permissions using the chmod command.
1. File Permissions Basics
r (Read): Allows reading the contents of a file or listing the contents
of a directory.
w (Write): Allows modifying a file or adding/removing files in a
directory.
x (Execute): Allows executing a file as a program or script, or entering
a directory.
2. Permission Types
Permissions can be set for three types of users:
User (Owner): The file or directory owner.
Group: Users who are part of the file’s group.
Others: All other users.
3. Viewing Permissions
ls -l: Lists files and directories with detailed permissions.
ls -l filename
Output Example:
-rwxr-xr-- 1 user group 12345 Sep 1 12:34 filename
-rwxr-xr--: Permissions (read, write, execute for owner; read, execute
for group; read for others)
1: Number of links
user: Owner
group: Group
12345: File size
Sep 1 12:34: Last modified date and time
filename: File name
4. Changing Permissions
chmod: Changes file or directory permissions.
Syntax:
chmod [options] mode file
Examples:
Numeric Mode:
chmod 755 filename
7 (Owner: rwx)
5 (Group: r-x)
5 (Others: r-x)
Symbolic Mode:
chmod u+x filename
u: User (owner)
+x: Add execute permission
chmod Options:
-R: Apply changes recursively to directories and their contents.
chmod -R 755 directory/
5. Changing Ownership
chown: Changes the owner and/or group of a file or directory.
Syntax:
chown [options] owner[:group] file
Examples:
Change Owner:
chown user filename
Change Owner and Group:
chown user:group filename
-R: Apply changes recursively.
chown -R user:group directory/
6. Changing Group Ownership
chgrp: Changes the group ownership of a file or directory.
Syntax:
chgrp [options] group file
Examples:
Change Group:
chgrp group filename
-R: Apply changes recursively.
chgrp -R group directory/
7. Setting Special Permissions
Setuid (s): When set on an executable file, the process runs with the
file’s owner permissions.
Example:
chmod u+s /usr/bin/program
Setgid (s): When set on a directory, files created within the directory
inherit the group of the directory, not the user’s primary group.
Example:
chmod g+s /path/to/directory
Sticky Bit (t): When set on a directory, only the file’s owner can delete
or rename the files within that directory.
Example:
chmod +t /path/to/directory
8. Examples and Use Cases
Add execute permission to a script:
chmod +x script.sh
Remove write permission from a file for the group:
chmod g-w file.txt
Change the owner of a directory and its contents:
chown -R newowner /path/to/directory
Set the setgid bit on a directory so that new files inherit the
directory’s group:
chmod g+s /shared/directory
Set the sticky bit on a directory to restrict file deletion:
chmod +t /tmp
9. Checking Effective Permissions
To check effective permissions on a file or directory:
namei -l path: Displays the permissions of each directory in the path.
Example:
namei -l /home/user/file.txt