Ubuntu Permission Denied Fix - chmod, chown, sudo Troubleshooting Guide
What You'll Learn
- How to quickly diagnose
Permission deniederrors on Ubuntu - When to use
chmod,chown, orsudo - How to avoid common mistakes (like opening permissions too wide)
Quick Summary
Permission denied usually falls into one of these categories:
- No execute permission ->
chmod +x - Wrong owner/group ->
chown/chgrp - Requires admin privileges -> Run with
sudo - Can't enter directory (no x permission) -> Check directory permissions
Start with ls -la (or namei -l) to see who's blocking what.
Prerequisites
- OS: Ubuntu
- Shell: bash
- Permissions:
sudoaccess assumed (contact admin if unavailable)
1. Check the Symptoms
Conclusion: The error type — file, script, or directory — determines which fix path to take.
Example: Can't read a file
$ cat /path/to/file cat: ...: Permission denied
Example: Can't execute a script
$ ./script.sh bash: ./script.sh: Permission denied
Example: Can't enter a directory
$ cd /path/to/dir bash: cd: ...: Permission denied
2. Check Permissions (ls -la)
Conclusion: Run
ls -la— the rwx bits for owner, group, and others reveal what is missing.
$ ls -la /path/to/target
Reading Permissions
r: readw: writex: execute (or enter for directories)
Three groups: owner | group | others
Examples:
-rw-r--r--: Owner can write, others can only readdrwxr-x---: Owner has full access, group can read/enter, others have no access
Case A: Script Won't Execute (no x)
Conclusion: Run
chmod +xwhen thexbit is absent — it adds only execute, nothing else.
Check:
$ ls -la script.sh
If you see -rw-r--r-- (no x), add execute permission:
$ chmod +x script.sh
This only adds "execute" permission. No need to widen read/write permissions.
Case B: Can't Write (no w / wrong owner)
Conclusion: Write access is set by the directory — use
chownorsudoaccordingly.
Write permissions depend on the directory.
If you should own the location -> Fix ownership (Case C)
If it's a system directory (/etc, /var, /usr) -> Use sudo (Case D)
Case C: Wrong Owner/Group (chown/chgrp)
Conclusion: Use
chown user:group /path— always verify the path before adding-R.
Change owner (example: user is ubuntu):
$ sudo chown ubuntu:ubuntu /path/to/dir
Recursive (be careful!):
$ sudo chown -R ubuntu:ubuntu /path/to/dir
Warning: -R affects everything under the path. Double-check before running!
Change group only:
$ sudo chgrp www-data /path/to/dir
Case D: Need Admin Privileges (sudo)
Conclusion: Use
sudofor system files — temporary elevated access, ownership unchanged.
System files and services require admin access:
$ sudo vim /etc/nginx/nginx.conf
$ sudo systemctl restart nginx
Case E: Can't Enter Directory (no x)
Conclusion: Directories need
xto enter — fix withchown, notchmod 777.
cd permission denied usually means the directory lacks execute (enter) permission.
$ ls -ld /path/to/dir
If you should be able to enter, fix ownership or group (Case C).
Do not use chmod 777 carelessly.
3. Trace the Path (namei)
Conclusion: Run
namei -l /path— shows each directory's permissions and reveals the block.
When you can't figure out where the block is in a long path:
$ namei -l /path/to/target
This shows permissions for each directory in the path, making it easy to find the problem directory.
4. What NOT to Do
Conclusion: Never use
chmod 777orchown -R /— both create holes or destroy the system.
Don't use chmod 777 for everything
It might work temporarily but creates security vulnerabilities, especially for web servers and config files.
Don't run chown -R on root
$ sudo chown -R ubuntu:ubuntu / # NEVER do this!
This will break your system. Always verify the path before using -R.