How to Fix Linux Permission Denied (chmod) Error
In this tutorial, you'll learn about How to Fix Linux Permission Denied (chmod) Error. We cover key concepts, practical examples, and best practices.
You try to run a script or access a file and get Permission denied — the file lacks the correct read, write, or execute permissions for your user.
The Problem
bash: ./script.sh: Permission denied
Or:
touch: cannot touch '/etc/config.ini': Permission denied
Step-by-Step Fix
Step 1: Check current permissions
ls -la script.sh
Expected:
-rw-r--r-- 1 user user 1024 Jun 24 10:00 script.sh
Missing x in the first column means execute is not set.
Step 2: Add execute permission
chmod +x script.sh
Verify:
ls -la script.sh
Expected:
-rwxr-xr-x 1 user user 1024 Jun 24 10:00 script.sh
Step 3: Fix read/write permissions
# Add read and write for owner
chmod u+rw file.txt
# Add read for group and others
chmod go+r file.txt
# Full permissions for owner, read/execute for others
chmod 755 script.sh
Step 4: Change file ownership
If you need to access a file owned by another user:
sudo chown $(whoami):$(whoami) file.txt
Step 5: Fix directory permissions
For directories, execute (x) is needed to enter:
chmod +x /path/to/directory
chmod -R +x /path/to/directory # recursive
Step 6: Use sudo when necessary
For system files owned by root:
sudo nano /etc/hosts
Prevention Tips
- Set execute permission when creating scripts:
chmod +x script.sh - Use
umaskto control default permissions (e.g.,umask 022) - Never use
chmod 777for security reasons - Use
sudoonly when necessary - Check ownership with
ls -labefore troubleshooting
Common Mistakes with permission denied
- Non-exhaustive pattern matches that compile with warnings then crash at runtime
- Misunderstanding that
Stringis[Char]with poor performance for large text operations - Using
foldlinstead offoldl'causing stack overflow on large lists
These mistakes appear frequently in real-world LINUX code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro