What the 'Permission Denied' Error Means
The Permission denied (EACCES) error in Ubuntu occurs when a process attempts to access a file or directory, but the current user does not have sufficient permissions. The error text in the terminal typically looks like this:
bash: /path/to/file: Permission denied
Or when running a command:
ls: cannot open directory 'protected_directory': Permission denied
This error can appear when trying to read, write, delete, or execute a file, as well as when accessing network resources or devices.
Common Causes
The "Permission denied" error has specific technical causes. Here are the most frequent:
- The file belongs to another user — you are not the file owner, and the permissions for "others" do not allow your type of access (read/write/execute).
- The file's group does not include your group — if you are not in the owning group, and the permissions for "group" are insufficient.
- Permissions for "others" are denied — even if you are not in the group, permissions for other users may be set to none.
- The parent directory is inaccessible — entering a directory requires execute (x) permission on all directories in the path. If any directory denies execute permission, access to the file is blocked.
- The filesystem is mounted with
noexec,nosuid,nodevoptions — for example, if a partition is mounted asnoexec, executing any files on it is prohibited. - AppArmor or SELinux restrict access — Ubuntu has AppArmor enabled by default, which can prevent an application from accessing certain files even if standard permissions allow it.
- The file is open in exclusive mode or locked by another process (e.g., via
flock). - Attempt to write to a file opened read-only — even if you have write permissions, if the file is opened by another process with the
O_RDONLYflag, writing may be denied. - Using
sudofor a command, but the target file requires root permissions, and the command is run withoutsudo— for example,apt updaterequires sudo, but running it without will result in permission denied.
Solutions
Method 1: Using sudo for Privileged Operations
If the error occurs when running system commands (e.g., apt, systemctl, accessing /etc), you need superuser privileges.
- Add
sudobefore the command:sudo apt update - Enter your user password (your account must be in the
sudogroup). Check group membership:
Ifgroups $USERsudois not in the output, add your user to the group (requires existing root privileges):sudo usermod -aG sudo $USER - After adding to the group, log out and back in or run
newgrp sudo.
⚠️ Important: Do not use
sudofor all commands. Apply it only when necessary to avoid accidental system changes.
Method 2: Changing Permissions with chmod
If you own the file but lack the required permissions, change them using chmod.
- Check current permissions:
ls -l /path/to/file
Example output:-rw-r--r-- 1 user group 0 Feb 17 12:00 fileHere: owner (user) hasrw-, group (group)r--, othersr--. - Add permissions for owner (u), group (g), or others (o). For example, give the owner execute permission:
chmod u+x /path/to/file
Or give the group write permission:chmod g+w /path/to/file - Use numeric mode for quick configuration:
chmod 755 /path/to/file # owner: rwx, group and others: r-x chmod 644 /path/to/file # owner: rw-, group and others: r-- - For directories, always include execute permission (x), otherwise you cannot enter them:
chmod u+rx /path/to/directory
Method 3: Changing Ownership with chown
If the file belongs to another user (e.g., root) and you lack permissions to change it via chmod, change the owner.
- Check the current owner:
ls -ld /path/to/file - Change the owner to your current user (requires
sudo):sudo chown $USER /path/to/file
Here$USERis the environment variable with your username. - To recursively change all files in a directory:
sudo chown -R $USER /path/to/directory - You can change the group simultaneously:
sudo chown $USER:$USER /path/to/file # both owner and group become $USER - If you want the group to remain unchanged while changing the owner:
sudo chown new_user: /path/to/file
Method 4: Configuring Group Permissions and Adding to a Group
For shared directories (e.g., /var/www or /home/shared), group permissions are often used.
- Set group permissions on the directory:
sudo chgrp developers /path/to/directory # change group to 'developers' sudo chmod 2775 /path/to/directory # setgid bit: new files inherit the group - Add your user to the group:
sudo usermod -aG developers $USER - Log out and back in or run
newgrp developersto apply changes in the current session. - Verify the user has the correct permissions:
groups $USER
Method 5: Checking and Configuring AppArmor (for Ubuntu)
AppArmor can restrict an application's access to files even if standard permissions are granted.
- Check AppArmor status:
sudo apparmor_status
If the output includes "apparmor module is loaded", it is active. - Find profiles that might affect the problematic file:
sudo aa-status | grep -i "application_name"
Or review profiles in/etc/apparmor.d/. - Temporarily disable the profile for testing (not recommended for production):
sudo apparmor_parser -R /etc/apparmor.d/usr.bin.your_application - For a permanent solution, edit the profile:
- Open the profile file (e.g.,
/etc/apparmor.d/usr.bin.your_application). - Add a rule for file access, for example:
/path/to/file rw, - Reload the profile:
sudo systemctl reload apparmor
- Open the profile file (e.g.,
- If AppArmor is not the cause, check SELinux (rarely enabled by default in Ubuntu):
getenforce
If it returns "Enforcing", review logs:sudo ausearch -m avc -ts recent.
Method 6: Checking Filesystem Mount Options
If the file is on a separate partition (e.g., /mnt/data), check how it is mounted.
- Identify the mount point for the file:
df -h /path/to/file
The output shows the partition and mount point (e.g.,/dev/sdb1 on /mnt/data). - Check mount options:
mount | grep /mnt/data
Example:/dev/sdb1 on /mnt/data type ext4 (rw,noexec,relatime)— herenoexecprohibits execution. - If
noexecis present, modify options in/etc/fstab:- Edit
/etc/fstabwithsudo:sudo nano /etc/fstab - Find the line for the partition and remove
noexec(or replace withexec). - Example change:
/dev/sdb1 /mnt/data ext4 defaults 0 2 - Remount the partition:
sudo mount -o remount /mnt/data
- Edit
- Verify after remounting:
mount | grep /mnt/data
Method 7: Checking Locks and Anomalies
In rare cases, the error is caused by file locks or filesystem specifics.
- Check if the file is locked by another process (using
flockorfcntl):lsof /path/to/file
If the file is open, terminate the process (carefully):sudo kill -9 <PID> - For network filesystems (NFS, Samba), check permissions on the server and mount options (e.g.,
root_squashin NFS can map root to nobody). - Check file attributes (e.g., immutable bit via
chattr):lsattr /path/to/file
Ifi(immutable) is present, remove the attribute:sudo chattr -i /path/to/file - Check available disk space:
df -h. Sometimes operations fail with access errors when disk space is low.
Prevention
To avoid "Permission denied" errors in the future:
- Configure permissions when creating files — use
umaskto define default permissions. For directories,umask 002yields775; for files,664. - Use group permissions for shared resources — create a group, add users, set
chmod 2775on directories. - Do not run applications as root unnecessarily — use
sudoonly for specific commands, not for the entire shell. - Regularly audit permissions — check
/etc,/var, and home directories for excessive permissions (e.g.,chmod 777). - Configure AppArmor/SELinux carefully — when installing new software, check for existing profiles and adjust them as needed.
- Avoid mounting with
noexecon partitions where file execution is needed (e.g.,/tmpor/homein some scenarios).