Skip to main content

Command Palette

Search for a command to run...

Day 4: Mastering Linux File Permissions - The Ultimate Guide πŸ“πŸ”

Published
β€’12 min read
Day 4: Mastering Linux File Permissions - The Ultimate Guide πŸ“πŸ”

Welcome back! πŸ‘‹ Day 4 of the 100 Days Cloud DevOps Challenge is here, and today we're diving deep into one of the most fundamental concepts in Linux - file permissions! This might seem basic, but trust me, understanding permissions thoroughly is absolutely crucial for any DevOps engineer!

πŸ€” What Are File Permissions?

Imagine you have a diary πŸ“”. You can read it, write in it, and even show it to others (if you want!). But you probably don't want just anyone picking it up and writing in it, right?

Linux file permissions work exactly like that! They control:

  • Who can access a file or directory

  • What they can do with it (read, write, execute)

  • How the system enforces these rules

Every single file and directory in Linux has permissions attached to it. These permissions determine whether you can view the file, modify it, or run it as a program.

Think of it as a security guard at a building entrance checking ID badges and access levels! πŸ›‘οΈ

🎯 Why Do File Permissions Matter?

Let me show you why this is absolutely critical:

πŸ”’ Security First

Imagine if any user on your production server could modify your database configuration files, or worse, delete your entire application directory! Chaos! 😱

Permissions prevent this by ensuring only authorized users can perform sensitive operations.

🎭 Multi-User Environment

Linux is designed for multiple users to work on the same system simultaneously. Permissions ensure:

  • Your files stay private unless you share them

  • System files can't be accidentally (or maliciously) modified

  • Applications run with appropriate access levels

πŸ€– Automation & Scripts

In DevOps, we write tons of scripts for automation, deployment, backups, monitoring - you name it! These scripts need executable permissions to run. Understanding how to set permissions correctly is essential for automation to work!

🏒 Compliance Requirements

Regulations like PCI-DSS, HIPAA, and SOC 2 have specific requirements about file access controls. Proper permissions are not just best practice - they're often legally required!

πŸ› Troubleshooting

How many times have you seen "Permission denied"? Understanding permissions helps you quickly diagnose and fix these issues!

πŸ“ Where and When Do You Use This?

Where:

  • Script deployment (like our backup script today!)

  • Application installations

  • Web server configurations

  • Database file management

  • Log file access

  • SSH key management

  • CI/CD pipeline scripts

  • Container configurations

  • Scheduled cron jobs

When:

  • Deploying new scripts or applications

  • Setting up automated jobs

  • Hardening server security

  • Troubleshooting "Permission denied" errors

  • Following security best practices

  • Preparing files for different user access

  • During application deployments

πŸ’Ό Today's Challenge: xFusionCorp Backup Automation

The sysadmin team at xFusionCorp Industries has created an automated backup script called xfusioncorp.sh. They've distributed it to all servers, but there's a problem on App Server 1 - the script doesn't have executable permissions! πŸ˜…

Without executable permissions, the automation won't work. The backup job will fail!

The Task: Grant executable permissions to /tmp/xfusioncorp.sh on App Server 1, ensuring ALL users can execute it.

This is a super common scenario in real DevOps work - scripts get deployed but permissions aren't set correctly!

🧩 Understanding Linux Permissions - The Building Blocks

Before we solve the challenge, let's understand how permissions actually work!

The Three Permission Types

1. Read (r) - Value: 4 πŸ“–

  • For files: View the file contents

  • For directories: List the directory contents

  • Example: cat file.txt, ls /directory

2. Write (w) - Value: 2 ✏️

  • For files: Modify or delete the file

  • For directories: Create, delete, or rename files within

  • Example: echo "text" >> file.txt, rm file.txt

3. Execute (x) - Value: 1 ⚑

  • For files: Run the file as a program/script

  • For directories: Access the directory (cd into it)

  • Example: ./script.sh, cd /directory

The Three User Categories

1. Owner (u) πŸ‘€

  • The user who owns the file

  • Usually the person who created it

  • Has the most control

2. Group (g) πŸ‘₯

  • Users who belong to the file's group

  • Allows team-based access

  • Common in collaborative environments

3. Others (o) 🌍

  • Everyone else on the system

  • Any user not the owner or in the group

  • Usually most restricted

Reading Permission Strings

When you run ls -l, you see something like this:

-rwxr-xr-x 1 tony developers 256 Nov 9 10:00 xfusioncorp.sh

Let's break it down:

-rwxr-xr-x
β”‚β””β”¬β”˜β””β”¬β”˜β””β”¬β”˜
β”‚ β”‚  β”‚  └─── Others: r-x (read + execute)
β”‚ β”‚  └────── Group: r-x (read + execute)  
β”‚ └───────── Owner: rwx (read + write + execute)
└─────────── File type: - (regular file)

The first character indicates the type:

  • - = Regular file

  • d = Directory

  • l = Symbolic link

  • b = Block device

  • c = Character device

πŸ› οΈ Let's Solve This Challenge!

Step 1: Connect to App Server 1

bash

ssh tony@stapp01

Step 2: Gain Root Access

bash

sudo su -

Most permission changes on system-wide files require root privileges!

Step 3: Check Current Permissions

Always check what you're starting with:

bash

ls -l /tmp/xfusioncorp.sh

You might see something like:

-rw-r--r-- 1 root root 256 Nov 9 10:00 /tmp/xfusioncorp.sh

Let's decode this:

  • -rw-r--r-- means:

    • Owner (root): rw- (read + write, NO execute)

    • Group (root): r-- (read only)

    • Others: r-- (read only)

See the problem? Nobody can execute it! ❌

Step 4: Grant Executable Permissions

Here's where the magic happens! We have multiple ways to do this:

Method 1: Simple and Quick (Recommended for this task)

bash

chmod +x /tmp/xfusioncorp.sh

This adds execute (+x) permission to all categories (owner, group, others). Simple and effective! βœ…

Method 2: Explicit for All Users

bash

chmod a+x /tmp/xfusioncorp.sh

a means "all" - explicitly states we want execute for everyone. Same result as +x!

Method 3: Numeric Mode

bash

chmod 755 /tmp/xfusioncorp.sh

Let me explain 755:

  • 7 (owner) = 4+2+1 = rwx (read + write + execute)

  • 5 (group) = 4+0+1 = r-x (read + execute)

  • 5 (others) = 4+0+1 = r-x (read + execute)

This is the standard permission for scripts and executables!

Method 4: Explicit for Each Category

bash

chmod u+x,g+x,o+x /tmp/xfusioncorp.sh

Explicitly adds execute to:

  • u+x - Owner (user)

  • g+x - Group

  • o+x - Others

More verbose but very clear about intent!

Step 5: Verify Permissions Are Correct

Always verify your changes!

bash

ls -l /tmp/xfusioncorp.sh

Now you should see:

-rwxr-xr-x 1 root root 256 Nov 9 10:00 /tmp/xfusioncorp.sh

Perfect! Notice the x in all three permission groups! βœ…

Alternative Verification - Numeric Format:

bash

stat -c '%a %n' /tmp/xfusioncorp.sh

Output:

755 /tmp/xfusioncorp.sh

Detailed Information:

bash

stat /tmp/xfusioncorp.sh

This shows comprehensive file information including timestamps, inode, and permissions in multiple formats!

Let's make sure it actually works:

bash

# Execute directly
/tmp/xfusioncorp.sh

# Or from current directory
cd /tmp
./xfusioncorp.sh

If it runs (even if it shows errors from the script itself), the permissions are working! πŸŽ‰

Test as Different User:

bash

su - tony
/tmp/xfusioncorp.sh

This confirms that even non-root users can execute it!

🎨 Understanding Numeric Permissions Deeply

Let's master the numeric system because it's incredibly powerful and commonly used in scripts and automation!

The Calculation Method

Each permission has a numeric value:

  • Read (r) = 4

  • Write (w) = 2

  • Execute (x) = 1

  • None (-) = 0

You add these values together for each category!

Common Permission Combinations

For Files:

NumericSymbolicMeaningCommon Use644rw-r--r--Owner: read/write, Others: read onlyRegular files, configs600rw-------Owner: read/write, Others: nothingPrivate files, SSH keys755rwxr-xr-xOwner: full, Others: read/executeScripts, executables777rwxrwxrwxEveryone: everything⚠️ Dangerous! Avoid!700rwx------Owner: full, Others: nothingPrivate scripts

For Directories:

NumericSymbolicMeaningCommon Use755rwxr-xr-xOwner: full, Others: list/accessStandard directories700rwx------Owner: full, Others: nothingPrivate directories775rwxrwxr-xOwner/Group: full, Others: readTeam directories

Quick Reference Examples

bash

# Most restrictive (owner only)
chmod 600 file.txt    # Owner: rw
chmod 700 script.sh   # Owner: rwx

# Standard file permissions
chmod 644 config.txt  # Owner: rw, Others: r

# Standard script/executable
chmod 755 script.sh   # Owner: rwx, Others: rx

# Group collaboration
chmod 775 /shared/dir # Owner/Group: rwx, Others: rx

# Most permissive (⚠️ use carefully!)
chmod 777 file.txt    # Everyone: rwx (security risk!)

πŸ” Symbolic vs Numeric - When to Use What?

Use Symbolic Notation When:

  • Making incremental changes (chmod +x script.sh)

  • Want to be explicit about what you're changing

  • Adding/removing single permissions

  • Writing self-documenting commands

  • Learning or teaching

Use Numeric Notation When:

  • Setting exact permissions from scratch

  • Automation and scripts (faster, more predictable)

  • Need to set all permissions at once

  • Working with configuration management tools

  • Advanced DevOps work

Pro Tip: In real DevOps work, you'll see both! Know both well! πŸ’ͺ

🎭 Special Permissions - Beyond the Basics

Want to level up? There are three special permissions:

1. Setuid (4000) πŸ”

  • File executes with owner's permissions

  • Example: /usr/bin/passwd (needs root to change passwords)

  • Notation: s in owner execute position

2. Setgid (2000) πŸ‘₯

  • File executes with group's permissions

  • Directories: New files inherit directory's group

  • Notation: s in group execute position

3. Sticky Bit (1000) πŸ“Œ

  • Only owner can delete files in directory

  • Common on /tmp directory

  • Notation: t in others execute position

Example:

bash

chmod 4755 file    # Setuid + rwxr-xr-x
chmod 2755 file    # Setgid + rwxr-xr-x
chmod 1777 /tmp    # Sticky bit + rwxrwxrwx

You'll see these in system directories, but for now, know they exist!

πŸ’‘ Key Takeaways

✨ File permissions control who can read, write, and execute files

✨ Three categories: Owner, Group, Others

✨ Three types: Read (4), Write (2), Execute (1)

✨ chmod +x grants execute permission to all users

✨ 755 is standard for scripts and executables

✨ 644 is standard for regular files

✨ Always verify permissions after changing them

✨ Scripts need execute (x) permission to run directly

✨ Without execute: use bash script.sh instead of ./script.sh

✨ Minimum necessary permissions = better security

✨ Both symbolic and numeric notations are important

πŸŽ“ Interview Questions You'll Ace

Q1: What's the difference between chmod +x and chmod 755?

Answer: While both add execute permission, they work differently. chmod +x adds execute permission to whatever exists - if a file is 644 (rw-r--r--), it becomes 755 (rwxr-xr-x). If it's 600 (rw-------), it becomes 700 (rwx------). In contrast, chmod 755 sets absolute permissions regardless of current state - it always results in rwxr-xr-x. Use +x for incremental changes and 755 when you want exact, predictable permissions. In automation, 755 is preferred for consistency.

Q2: Why can't I execute a script even though I can read it?

Answer: Read and execute are separate permissions in Linux. Read permission allows you to view the file contents (cat script.sh), but execute permission is required to run it as a program (./script.sh). This separation is intentional for security - you might want users to see code without being able to run it, or vice versa. The solution is to add execute permission with chmod +x script.sh, or run it explicitly with an interpreter: bash script.sh or sh script.sh, which only requires read permission.

Q3: What do the numbers 755, 644, and 777 represent in file permissions?

Answer: These are octal representations where each digit represents permissions for owner, group, and others respectively. Each permission has a value: read=4, write=2, execute=1. You add these values: 7\=4+2+1 (rwx), 5\=4+1 (r-x), 4\=4 (r--), 0\=no permissions. So 755 means owner has rwx (7), group has r-x (5), others have r-x (5). 644 is owner rw (6), others read-only (4) - standard for files. 777 is full permissions for everyone - generally a security risk and should be avoided except in specific temporary testing scenarios.

Q4: How do directory permissions differ from file permissions?

Answer: While the notation is same, the meaning changes for directories. Read (r) allows listing directory contents with ls. Write (w) allows creating, deleting, or renaming files within the directory. Execute (x) allows accessing the directory with cd and accessing files inside - it's required to "enter" the directory. Without execute on a directory, you can't access anything inside even if you have read permission! This is why standard directory permissions are 755 (rwxr-xr-x) - everyone needs execute to access contents, but only owner can modify.

Q5: What's the security risk of using chmod 777?

Answer: chmod 777 gives read, write, and execute permissions to everyone - owner, group, and all other users on the system. This means any user can view, modify, or execute the file. For scripts, malicious users could modify code that runs with elevated privileges. For directories, anyone can create, delete, or rename files inside. It violates the principle of least privilege and is a common security vulnerability. It's particularly dangerous for system files, configuration files, or scripts in production. Better alternatives: 755 for scripts (others can execute but not modify), 644 for files (others can only read), or 700 for truly private files.

Q6: How would you find all files with execute permission in a directory recursively?

Answer: Use the find command: find /path -type f -perm /111 finds files where any user (owner/group/others) has execute permission. The /111 tests if any of the execute bits are set (user=001, group=010, others=100 in octal). For more specific cases: find /path -type f -perm -755 finds files with at least 755 permissions (the minus means all specified bits must be set). To find exactly 755: find /path -type f -perm 755. Add -ls flag to see detailed permissions: find /path -type f -perm /111 -ls. This is useful for security audits and troubleshooting permission issues.

🚨 Common Mistakes to Avoid

❌ Using 777 in Production

  • NEVER do this unless absolutely necessary and temporary

  • Major security vulnerability

  • Shows lack of understanding in interviews

❌ Forgetting to Verify

  • Always check permissions after changing them

  • ls -l is your friend!

❌ Wrong File Type

  • Adding execute to text files that aren't scripts

  • Confusion between files and directories

❌ Over-Permissive Defaults

  • Giving write permission to group/others unnecessarily

  • Opening security holes

❌ Recursive Changes Without Care

  • chmod -R 777 / = disaster! 😱

  • Always think before using -R flag

❌ Not Understanding the Difference

  • Mixing up read/write/execute meanings

  • Forgetting directory permissions work differently

πŸš€ Real-World DevOps Scenarios

Scenario 1: Deployment Scripts

bash

# Wrong
scp script.sh server:/opt/app/
# Won't execute! No permissions!

# Right
scp script.sh server:/opt/app/
ssh server "chmod +x /opt/app/script.sh"

Scenario 2: Cron Jobs

bash

# Wrong - script in crontab but not executable
0 2 * * * /opt/backup/backup.sh

# Right - ensure script is executable
chmod 755 /opt/backup/backup.sh
0 2 * * * /opt/backup/backup.sh

Scenario 3: Docker Entrypoint

dockerfile

# Wrong
COPY entrypoint.sh /
CMD ["/entrypoint.sh"]
# Will fail!

# Right
COPY entrypoint.sh /
RUN chmod +x /entrypoint.sh
CMD ["/entrypoint.sh"]

Scenario 4: SSH Keys

bash

# Wrong - SSH will refuse keys with wrong permissions
chmod 644 ~/.ssh/id_rsa
# Error: Permissions 0644 are too open

# Right
chmod 600 ~/.ssh/id_rsa      # Private key
chmod 644 ~/.ssh/id_rsa.pub  # Public key

🎯 What's Next?

Day 4 conquered! πŸŽ‰ We've mastered Linux file permissions - understanding how to read them, set them, and why they matter. This is knowledge you'll use literally every single day in DevOps!

Tomorrow, Day 5 awaits with new challenges! Keep building that foundation! πŸ’ͺ


Day: 4/100
Challenge: KodeKloud Cloud DevOps
Date: November 9, 2025
Topic: Linux File Permissions & chmod

What's your most memorable "Permission denied" moment? Share in the comments! πŸ˜„

More from this blog

πŸš€ DevOps Challenge- KodeKloud Solutions

73 posts