"Permission denied" in the Linux shell: how to find the cause and fix it

"Permission denied" (errno EACCES) means the kernel refused access because of the file's mode bits, a directory on the path, or the mount options. Run `id` and `ls -l` on the file and its folders to see which check fails, then fix only that one, usually with `chmod`, `chown` or `sudo`.

Applies to:Linux (all distributions)
On this page

What it means

Permission denied is how shells and tools print the kernel error EACCES. The system found what you asked for, but your user is not allowed to do that action there. The message looks the same whether you are running a script, reading a file, entering a folder or creating something new, so it does not say which rule blocked you.

A similar message is Operation not permitted (EPERM). It is a different error code. One other message can look like permission trouble but is different: Permission denied (publickey) from SSH is a login key problem. This article covers the plain Permission denied case.

Symptoms and causes

You are likely dealing with this problem if:

  • bash: ./script.sh: Permission denied when starting a script
  • cat: file: Permission denied or cd: dir: Permission denied
  • touch: cannot touch 'x': Permission denied when creating a file
  • -bash: /mnt/usb/prog: Permission denied on a program stored on a USB drive

The usual causes, most common first:

Mode bits do not allow your user
The kernel picks one set of permissions only: owner if your effective user ID matches, otherwise group (primary or supplementary), otherwise other. If you are the owner, the owner bits apply even when the "other" bits are more open.
No execute bit on a script or program
Running ./script needs the x bit. Even root needs at least one execute bit set on the file.
A parent directory is not searchable
Every directory on the path needs search (x) permission for you. Being able to read the file itself is not enough if one folder above it blocks you.
No write permission on the directory
Creating or deleting a file depends on w and x on the containing directory, not on the file.
Not the right user or group
Supplementary groups count in the permission check, so a file that is meant to be reached through a group needs your account to be a member of it.
The filesystem is mounted noexec
A filesystem mounted with noexec refuses to run binaries from it. This is common on USB drives, /tmp and hardened systems.

How to fix it

Work through these in order. They start with the quickest, safest checks and move to deeper repairs; stop as soon as the problem is gone.

1.Find which check is failing

Easy

These commands only read information. They tell you which of the fixes below applies, so you do not change permissions blindly.

  1. Check which user and groups you are running as.
    Terminal
    id
  2. Look at the file, then at its directory. Then repeat for each parent folder on the path and look for one where your user lacks x.
    Terminal
    ls -l file
    ls -ld /path/to/dir
  3. Check the mount options of the filesystem for noexec.
    Terminal
    mount | grep <mountpoint>

2.Make a script executable, or run it through the interpreter

Easy

Fixes ./script.sh: Permission denied when the file simply lacks an execute bit.

  1. Add the execute bit for yourself.
    Terminal
    chmod u+x script.sh
  2. Or skip the bit and run the script with its interpreter.
    Terminal
    bash script.sh

3.Give access to the directory

EasyChanges settings

Fixes denials caused by a directory on the path that you cannot search, or cannot write to.

Before you start: Change only the directories you own or are responsible for. Do not use chmod 777, and never run chmod -R on / or system folders: chmod does not refuse recursion on / by default.

  1. Allow yourself to enter the directory, or add o+x if other users need it too.
    Terminal
    chmod u+x /path/dir
  2. If you need to create or delete files inside it, the directory needs w as well as x.
    Terminal
    chmod u+wx /path/dir
  3. To grant read access across a tree, use the capital X. It adds execute only on directories and on files that already have an execute bit, so ordinary data files do not become executable.
    Terminal
    chmod -R u+rX /path/dir

4.Fix ownership or group access

ModerateChanges settings

Helps when the file belongs to another user, or when access is meant to come from a group.

Before you start: Only a privileged process can change a file's owner. Avoid chown -R on system directories, because it can break the system.

  1. Change the owner and group of a file that should be yours.
    Terminal
    sudo chown user:group file
  2. If access is supposed to come from a group, ask your system administrator to add your account to that group.

5.Use sudo for system paths

EasyChanges settings

Files under system locations such as /etc are meant to be changed by an administrator.

Before you start: Run only the single command that needs it. Do not stay in a root shell, and do not loosen permissions on system files to avoid typing sudo.

  1. Prefix the command with sudo.
    Terminal
    sudo <command>

6.Deal with a noexec mount

ModerateChanges settings

Mode bits can be perfect and a program still will not start if its filesystem is mounted noexec.

Before you start: noexec is often set on purpose for security. Do not work around it on a machine you do not manage.

  1. Run the program through its interpreter where that applies, for example bash script.sh.
  2. Or copy the program to a location where execution is allowed, such as your home directory, and run it from there.

7.Rule out look-alike problems: read-only mounts and immutable files

ModerateChanges settings

These can also stop a write.

Before you start: A filesystem that turned read-only by itself may be reporting disk errors. Do not force it back to read-write before checking.

  1. If the read-only state was set on purpose, change it through your administrator or the mount options in /etc/fstab.
  2. The chown(2) manual page notes that immutable and append-only files block ownership changes. If a file refuses changes even though the owner and mode bits look right, ask whoever manages the system whether the file was protected deliberately.

Check that it worked

  • Repeat the command that failed and confirm the error is gone.
  • Run ls -ld on each directory in the path again and confirm your user can search every one.

If it still fails

  • If owner, mode bits and mounts all look correct, another access-control layer on your system may be involved (SELinux or AppArmor, depending on the distribution). Check your distribution's documentation for how to read its denial logs.
  • Containers and network shares have their own access rules. Check the documentation of the tool you use.
  • Ask your distribution's support forum or your system administrator, and include the output of id, ls -l for the file and each parent directory, and mount for the path.

Questions

Is chmod 777 a good quick fix?

No. It gives every user write and execute access and hides the real cause. Find the failing check and change only that.

Why does chmod -R 755 cause trouble?

It also makes plain data files executable. Use chmod -R u+rX instead, or set directories and files separately with find -type d and find -type f.

Why am I denied even though I can read the file?

Reading needs r on the file, but a folder higher up the path may lack x for you. Check each parent directory with ls -ld.

Sources

  1. path_resolution(7)Official · Linux man-pages project
  2. credentials(7)Official · Linux man-pages project
  3. errno(3)Official · Linux man-pages project
  4. chmod(1)Official · Linux man-pages project
  5. chown(2)Official · Linux man-pages project
  6. mount(8)Official · Linux man-pages project