Managing File Permissions and Ownership
1. Understanding Permission Model
| Permission Type | Symbol | Value | Description |
|---|---|---|---|
| Read | r |
4 |
Allows reading file contents or listing directory contents |
| Write | w |
2 |
Allows modifying file contents or creating/deleting directory entries |
| Execute | x |
1 |
Allows executing files or accessing directory contents |
Example: Permission Representation
ls -l /etc/passwd
-rw-r--r-- 1 root root 2458 Apr 10 12:30 /etc/passwd
Note: The first character is
d for directories, - for files. The next three characters represent owner permissions, followed by group and others permissions.2. Viewing Permissions
| Command | Description | Example |
|---|---|---|
| ls -l | Lists files with detailed permissions | ls -l /home/user/file.txt |
| stat | Displays detailed file or filesystem status | stat /home/user/file.txt |
| getfacl | Shows extended ACLs for a file or directory | getfacl /home/user/file.txt |
3. Changing Permissions with Symbolic Mode
| Operator | Description | Example |
|---|---|---|
| u | User (owner) | u+r |
| g | Group | g-w |
| o | Others | o+x |
| a | All (user, group, others) | a-r |
| + | Add permission | u+w |
| - | Remove permission | g-x |
| = | Set exactly these permissions | o=rw |
4. Changing Permissions with Octal Mode
| Octal Value | Binary Equivalent | Permission | Description |
|---|---|---|---|
| 7 | 111 |
rwx |
Read, Write, Execute |
| 6 | 110 |
rwx |
Read, Write, No Execute |
| 5 | 101 |
r-x |
Read, No Write, Execute |
| 4 | 100 |
r-- |
Read Only |
| 3 | 011 |
-wx |
No Read, Write, Execute |
| 2 | 010 |
-w- |
No Read, Write, No Execute |
| 1 | 001 |
--x |
No Read, No Write, Execute |
| 0 | 000 |
--- |
No Access |
5. Changing Ownership
| Command | Description | Example |
|---|---|---|
| chown | Changes the owner of a file or directory | chown user:group /home/user/file.txt |
| chown -R | Changes ownership recursively | chown -R user:group /home/user/dir/ |
6. Changing Group Ownership
| Command | Description | Example |
|---|---|---|
| chgrp | Changes the group of a file or directory | chgrp developers /home/user/file.txt |
| chgrp -R | Changes group recursively | chgrp -R developers /home/user/dir/ |
7. Setting Default Permissions
Setting umask
umaskcontrols default permissions for newly created files and directories- Default umask is
022, which creates files with644permissions and directories with755 - Use
umask 077to create files with read/write only for owner
8. Applying Permissions Recursively
| Flag | Description | Example |
|---|---|---|
| -R | Apply changes recursively to subdirectories | chmod -R 755 /home/user/dir/ |
| -R --reference=file | Apply permissions from a reference file | chmod -R --reference=/home/user/ref.txt /home/user/dir/ |
9. Setting Special Permissions
| Permission | Symbol | Description | Example |
|---|---|---|---|
| Set UID (SUID) | u+s |
Run as owner of file, not current user | chmod u+s /usr/bin/passwd |
| Set GID (SGID) | g+s |
Run as group of file, not current group | chmod g+s /home/user/dir/ |
| Sticky Bit | o+t |
Only owner can delete files in directory | chmod o+t /tmp |
10. Using Access Control Lists
| Command | Description | Example |
|---|---|---|
| setfacl | Set ACL entries for a file or directory | setfacl -m u:john:rwx /home/user/file.txt |
| getfacl | Get ACL entries for a file or directory | getfacl /home/user/file.txt |
| setfacl -R | Set ACL recursively | setfacl -R -m u:john:rwx /home/user/dir/ |
11. Understanding File Attributes
| Attribute | Description | Command to Set | Command to View |
|---|---|---|---|
| Immutable | Prevents modification or deletion of file | chattr +i /home/user/file.txt |
lsattr /home/user/file.txt |
| Append Only | Allows only appending to file | chattr +a /home/user/file.txt |
lsattr /home/user/file.txt |
| No Dump | Prevents file from being backed up | chattr +d /home/user/file.txt |
lsattr /home/user/file.txt |