Skip to main content

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

Example: Adding Read Permission to Owner

chmod u+r /home/user/file.txt

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

Example: Setting Permissions to 755

chmod 755 /home/user/script.sh

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/

Example: Changing Ownership to Root

chown root:root /etc/passwd

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/

Example: Changing Group Ownership

chgrp users /home/user/file.txt

7. Setting Default Permissions

Setting umask

  • umask controls default permissions for newly created files and directories
  • Default umask is 022, which creates files with 644 permissions and directories with 755
  • Use umask 077 to create files with read/write only for owner

Example: Setting umask to 077

umask 077

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/

Example: Recursive Permission Change

chmod -R 644 /home/user/documents/

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

Example: Setting SUID on passwd

chmod u+s /usr/bin/passwd

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/

Example: Adding User to ACL

setfacl -m u:john:rwx /home/user/file.txt

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

Example: Setting Immutable Attribute

chattr +i /etc/passwd

Was this article helpful?