Linux File Permissions Explained for Complete Beginners
Understanding Why chmod +x Was Not Enough
1. Introduction
In Linux, every file and directory has a set of permissions that determine:
- Who can read the file
- Who can modify the file
- Who can execute the file
Understanding permissions is one of the most important skills for every Linux administrator.
In this lesson, we will learn:
- What file permissions are
- How to read permissions
- How
chmodworks - Why a shell script needs both read and execute permissions
- Why
chmod +xalone did not work
2. The Scenario
Suppose we have a shell script:
/tmp/xfusioncorp.sh
Let's check its permissions:
ls -l /tmp/xfusioncorp.sh
Output:
---------- 1 root root 40 Jun 22 18:23 /tmp/xfusioncorp.sh
3. Understanding ls -l
The command:
ls -l
means:
ls→ List files-l→ Show detailed information (long format)
Example:
-rwxr-xr-x 1 root root 40 Jun 22 18:23 file.sh
Every part has a meaning.
4. Breaking Down File Permissions
Example:
-rwxr-xr-x
Let's divide it:
- rwx r-x r-x
│ │ │ │
│ │ │ └── Others
│ │ └────── Group
│ └────────── Owner
└──────────── File type
5. The First Character
The first character tells us the file type.
| Symbol | Meaning |
|---|---|
- | Regular file |
d | Directory |
l | Symbolic link |
c | Character device |
b | Block device |
Example:
-rwxr-xr-x
^
The - means it is a regular file.
6. The Permission Groups
Linux divides permissions into three groups:
Owner (User)
The person who owns the file.
Group
Users that belong to the file's group.
Others
Everyone else on the system.
Example:
-rwxr-xr-x
└┬┘└┬┘└┬┘
│ │ │
Owner Group Others
7. The Three Permission Types
Linux has three permissions.
Read (r)
Value:
4
Allows:
- Open a file
- View its contents
Example:
cat file.txt
less file.txt
nano file.txt
Write (w)
Value:
2
Allows:
- Modify the file
- Delete its contents
- Add new content
Example:
echo "hello" >> file.txt
nano file.txt
Execute (x)
Value:
1
Allows:
- Run a program
- Execute a script
Example:
./script.sh
./program
8. Numeric Permission Values
| Permission | Value |
|---|---|
| r | 4 |
| w | 2 |
| x | 1 |
We add the numbers.
Example
rwx
4 + 2 + 1 = 7
rw-
4 + 2 = 6
r-x
4 + 1 = 5
r--
4 = 4
--x
1 = 1
9. Understanding chmod
chmod means:
Change Mode
It changes file permissions.
Syntax:
chmod permissions file
10. Symbolic Method
Add execute permission
chmod +x file
Add read permission
chmod +r file
Add write permission
chmod +w file
Remove execute permission
chmod -x file
11. Who Gets the Permission?
| Symbol | Meaning |
|---|---|
| u | User (Owner) |
| g | Group |
| o | Others |
| a | All |
Examples:
Owner only
chmod u+x file
Group only
chmod g+x file
Others only
chmod o+x file
Everyone
chmod a+x file
12. Our File's Initial State
---------- /tmp/xfusioncorp.sh
This means:
Owner : ---
Group : ---
Others : ---
Nobody can do anything.
13. Giving Execute Permission
We ran:
sudo chmod a+x /tmp/xfusioncorp.sh
Permissions became:
---x--x--x
This means:
Owner : execute
Group : execute
Others : execute
14. Trying to Execute the Script
./xfusioncorp.sh
Error:
/bin/bash: ./xfusioncorp.sh: Permission denied
Why?
15. Understanding How Shell Scripts Work
Suppose the script contains:
#!/bin/bash
echo "Welcome To KodeKloud"
The first line:
#!/bin/bash
is called the:
Shebang
It tells Linux:
"Run this file using
/bin/bash."
16. What Happens Internally?
When you run:
./xfusioncorp.sh
Linux does this:
Step 1
Kernel sees:
#!/bin/bash
Step 2
Kernel starts:
/bin/bash
Step 3
Bash tries to read:
xfusioncorp.sh
Step 4
Bash cannot read the file.
Step 5
Bash shows:
Permission denied
17. Why Can't Bash Read It?
Because permissions are:
---x--x--x
There is:
No read permission.
Bash needs:
Read permission.
18. The Fix
Give everyone:
- Read permission
- Execute permission
sudo chmod a+rx /tmp/xfusioncorp.sh
or
sudo chmod 555 /tmp/xfusioncorp.sh
19. Final Permissions
-r-xr-xr-x
Breaking it down:
Owner : r-x
Group : r-x
Others : r-x
Everyone can:
- Read
- Execute
Nobody can:
- Modify
20. Why Does This Work?
Now the sequence is:
Run script
↓
Kernel starts Bash
↓
Bash reads file
↓
Bash executes commands
↓
Script runs successfully
21. Verification
./xfusioncorp.sh
Output:
Welcome To KodeKloud
22. Important Concept
Compiled Program
Example:
./firefox
./nginx
Usually only needs:
--x
because the kernel executes the binary directly.
Shell Script
Example:
./script.sh
./backup.sh
Needs:
r-x
because the interpreter must read the file.
23. Commands Summary
View permissions
ls -l file
Add execute permission
chmod a+x file
Add read and execute permission
chmod a+rx file
Set permissions numerically
chmod 555 file
24. Final Takeaway
Remember these two rules:
Binary executable
Needs: x
Script file
Needs: r + x
This small difference is one of the most important concepts in Linux permissions and often appears in system administration interviews and certification exams.