Ubuntu Permission Denied Fix: Troubleshooting Guide

Permission Denied Troubleshooting - chmod/chown/sudo

What You'll Learn

  • How to quickly diagnose Permission denied errors on Ubuntu
  • When to use chmod, chown, or sudo
  • How to avoid common mistakes (like opening permissions too wide)

💡 Quick Summary

Permission denied usually falls into one of these categories:

  1. No execute permissionchmod +x
  2. Wrong owner/groupchown / chgrp
  3. Requires admin privileges → Run with sudo
  4. Can't enter directory (no x permission) → Check directory permissions

Start with ls -la (or namei -l) to see who's blocking what.

Table of Contents

  1. Check the Symptoms
  2. Check Permissions (ls -la)
  3. Case A: Script Won't Execute
  4. Case B: Can't Write
  5. Case C: Wrong Owner/Group
  6. Case D: Need Admin Privileges
  7. Case E: Can't Enter Directory
  8. Trace the Path (namei)
  9. What NOT to Do

1. Check the Symptoms

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)

$ ls -la /path/to/target

Reading Permissions

  • r: read
  • w: write
  • x: execute (or enter for directories)

Three groups: owner | group | others

Examples:

  • -rw-r--r--: Owner can write, others can only read
  • drwxr-x---: Owner has full access, group can read/enter, others have no access

Case A: Script Won't Execute (no x)

Check:

$ ls -la script.sh

If you see -rw-r--r-- (no x), add execute permission:

$ chmod +x script.sh

Case B: Can't Write (no w / wrong owner)

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)

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!

Case D: Need Admin Privileges (sudo)

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)

cd permission denied usually means the directory lacks execute (enter) permission.

$ ls -ld /path/to/dir

3. Trace the Path (namei)

When you can't figure out where the block is:

$ namei -l /path/to/target

This shows permissions for each directory in the path.

4. What NOT to Do

❌ 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.

📋 Test Environment

Commands tested on Ubuntu 24.04 LTS / bash 5.2.

Next Reading