Flash Sale:75% Off Hosting + Free DomainEnds in13h47m14sView Plans
Hostvento logoHostvento

Fix "Permission denied" Errors in Linux

Learn how Linux permissions work and how to fix access errors with chmod, chown and sudo.

VPS and Cloud Servers2 min read3 steps2 screenshots

How Linux permissions work

Every file and folder has three kinds of permission:

  1. Read (r)
  2. Write (w)
  3. Execute (x)

They apply to three groups: the owner, the group and others. See them with:

ls -la
The Linux terminal
The output of ls -la

For example -rwxr-xr-- means: the owner can read, write and run; the group can read and run; others can only read.

Why you get "Permission denied"

  • The file or folder belongs to another user or group.
  • The permissions are wrong.
  • You try to reach a system file without root rights.
  • The script is not set as executable.

Fixes

1. Change the permissions. If you have the right to, add what is missing. Give the owner read, write and execute:

chmod u+rwx test.sh

2. Take ownership. If you are root or have sudo, give the file to your user:

sudo chown username test.sh

3. Use sudo. If you are not the owner and a system folder is involved, run the command as root:

sudo mkdir /opt/test

Use sudo only when you need it.

Permission denied when running a script

Make the script executable and run it:

chmod +x test.sh
./test.sh

If you still get an error, the file system may be mounted with noexec. Run the script with bash test.sh instead.

Other causes

  • A parent folder has no execute (x) permission. You need x on every folder in the path.
  • SELinux or AppArmor blocks the access. Check the logs (/var/log/audit/audit.log).
  • SSH keys: the private key must have mode 600 (chmod 600 ~/.ssh/id_rsa).
Warning: Do not use chmod 777 to "fix" problems. It lets everyone change the file. Give only the rights that are needed.

Quick recap

  • Read the permissions with ls -la.
  • Use chmod, chown or sudo to fix the access.
  • Make scripts executable with chmod +x.