DEV Community

Adnan al-emran ontor
Adnan al-emran ontor

Posted on

Linux File Permissions Cheat Sheet

What Are File Permissions?

Linux file permissions control who can read, write, or execute a file or directory. They are set for three user groups:

  • Owner: The file creator.
  • Group: A user group that can access the file.
  • Others: All other users.

Viewing File Permissions

Use the ls -l command to display file permissions:


ls -l

Enter fullscreen mode Exit fullscreen mode

Example Output:


-rwxr-xr-- 1 user group 1234 Nov 16 14:00 file.txt

Enter fullscreen mode Exit fullscreen mode
Part Explanation
-rwxr-xr-- Permissions (explained below).
1 Number of hard links.
user Owner of the file.
group Group associated with the file.
1234 File size in bytes.
Nov 16 14:00 Last modification date.
file.txt File name.

Permission Breakdown

Each permission has three parts:

[Type][Owner][Group][Others]

Symbol Type/Permission
- Regular file.
d Directory.
r Read permission.
w Write permission.
x Execute permission (or enter a folder).

Changing File Permissions

  1. Using chmod (Symbolic Mode):

    
    chmod [permissions] [file]
    
    

    Example: Add execute permission to the owner:

    
    chmod u+x file.txt
    
    
    Symbol Meaning
    u User (owner).
    g Group.
    o Others.
    a All (user, group, others).

    Actions:

    Symbol Action
    + Add permission.
    - Remove permission.
    = Set exact permission.

    Example: Remove write permission for others:

    
    chmod o-w file.txt
    
    

  1. Using chmod (Numeric Mode):

    Each permission level is represented by a number:

    Permission Value
    r (read) 4
    w (write) 2
    x (execute) 1
    - (none) 0

    Combine values to set permissions:

    Owner (u) Group (g) Others (o) Command
    rwx (7) r-x (5) r-- (4) chmod 754 file.txt

Changing Ownership

  1. Change file owner:

    
    chown [owner] [file]
    
    

    Example:

    
    sudo chown user file.txt
    
    
  2. Change group:

    
    chown :[group] [file]
    
    

    Example:

    
    sudo chown :staff file.txt
    
    
  3. Change both owner and group:

    
    chown [owner]:[group] [file]
    
    

    Example:

    
    sudo chown user:staff file.txt
    
    

Recursive Changes

To apply changes to all files and directories:

  1. Change permissions recursively:

    
    chmod -R [permissions] [directory]
    
    

    Example:

    
    chmod -R 755 /path/to/folder
    
    
  2. Change ownership recursively:

    
    chown -R [owner]:[group] [directory]
    
    

    Example:

    
    sudo chown -R user:staff /path/to/folder
    
    

Testing Permissions

Create a test file or directory to practice:


touch test.txt
chmod 600 test.txt  # Owner can read/write, others denied.
chmod 755 test.txt  # Everyone can read, owner can write.
chmod -R 770 /test  # Owner/group full access, others denied.

Enter fullscreen mode Exit fullscreen mode

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more