How to Fix 'Permission Denied' Errors on Linux
Diagnose and fix 'Permission denied' errors on Linux by understanding file ownership, chmod, and when to use sudo — with real examples.
Why You See "Permission Denied"
Linux enforces strict file permissions. Every file has an owner and a set of read/write/execute permissions for the owner, group, and everyone else. "Permission denied" means the user running the command doesn't have the rights needed for that action.
Step 1 — Check the Permissions
ls -la /path/to/file
-rw-r--r-- 1 root root 1234 Jun 1 config.txt
This file is owned by root and only root can write to it. The first group (rw-) is owner; second (r--) is group; third (r--) is everyone else.
Step 2 — Try sudo First
If you're trying to access a system file or run a privileged command, prefix with sudo:
sudo nano /etc/hosts
sudo cat /var/log/auth.log
sudo systemctl restart nginx
If sudo works, the file is owned by root and you just needed elevated privileges. Done.
Step 3 — Fix File Ownership with chown
If you own the project but someone ran a command as root and changed ownership:
# Check who owns it
ls -la /var/www/html/
# Change owner to the current user
sudo chown $USER:$USER /var/www/html/index.html
# Recursive: fix whole directory
sudo chown -R $USER:$USER /var/www/html/
Step 4 — Fix Permissions with chmod
# Add execute permission to a script you own
chmod +x deploy.sh
# Give your user write access to a file
chmod u+w config.txt
# Fix a web directory
chmod 755 /var/www/html
chmod 644 /var/www/html/index.html
Common Scenarios and Fixes
Script won't run: "Permission denied"
chmod +x script.sh
./script.sh
Can't write to /etc/ file
sudo nano /etc/nginx/nginx.conf
Web server can't read your files
sudo chown -R www-data:www-data /var/www/html/
sudo chmod -R 755 /var/www/html/
SSH key permissions are wrong
chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_ed25519
chmod 644 ~/.ssh/id_ed25519.pub
chmod 600 ~/.ssh/authorized_keys
Check Your Current User and Groups
whoami # current user
id # user ID, group ID, and all groups
groups # list all groups the current user belongs to
If you need to access something owned by a specific group, add yourself: sudo usermod -aG groupname $USER then log out and back in.