Post

File Permissions and Ownership in Linux

File Permissions and Ownership in Linux

๐Ÿ” File Permissions and Ownership in Linux


1. ๐Ÿ“„ Default File Permissions & umask

  • Default permissions:

    • Files: 666 (read & write for all)
    • Directories: 777 (read, write, execute for all)
  • Actual permissions = Default - umask

    • View current umask:

      1
      
      umask
      
    • Example: If umask is 022, new file permission becomes 644 (666 - 022) and directory permission becomes 755 (777 - 022)


2. ๐Ÿ“œ Reading ls -l Output

1
ls -l file.txt

Example output:

1
-rw-r--r-- 1 user group 1234 Jun 5 10:00 file.txt
PartMeaning
-File type (- = file, d = directory, l = symlink)
rw-r--r--Permissions: owner (rw-), group (rโ€“), others (rโ€“)
userFile owner
groupGroup owner
1234File size
Jun 5 10:00Last modified date
file.txtFile name

3. ๐Ÿงฎ Managing Permissions

a. Numeric (Octal) Permissions

DigitMeaning
7rwx
6rw-
5r-x
4r--
0---
1
chmod 755 myscript.sh  # rwxr-xr-x

b. Symbolic (Text) Permissions

1
2
3
chmod u+x file.txt     # Add execute for user
chmod g-w file.txt     # Remove write for group
chmod o=r file.txt     # Set read-only for others

c. Group Ownership and Permissions

  • Change owner:

    1
    
    chown username file.txt
    
  • Change group:

    1
    
    chgrp groupname file.txt
    
  • Set group and owner:

    1
    
    chown user:group file.txt
    

4. โš™๏ธ Customizing File Creation Mask (umask)

  • View current umask:

    1
    
    umask
    
  • Temporarily set umask:

    1
    
    umask 027
    
  • Permanently set:

    • Add to ~/.bashrc or /etc/profile:

      1
      
      umask 027
      

This post is licensed under CC BY 4.0 by the author.