What you will read?
The “Permission Denied” error is one of the most common issues you’ll face on a Linux server or desktop terminal. It happens when the current user doesn’t have the right to execute a file or access a resource. Let’s jump straight into how to fix it.
Make the File Executable
If you’re trying to run a script and get Permission denied
, the file might not be executable.
chmod +x script.sh ./script.sh
Still getting the error? Check if the file is located on a mounted volume or external filesystem with restricted execution permissions.
Use sudo
When Required
Sometimes, it’s not about the file — it’s about your privileges. For commands or scripts that need root access:
sudo ./script.sh
If sudo
itself returns “Permission denied”, check if your user is in the sudoers
group.
groups
If not, you need to add the user:
usermod -aG sudo yourusername
Log out and back in for changes to apply.
Check File Ownership
Maybe the file or directory isn’t owned by your user.
ls -l filename
To change the owner:
sudo chown yourusername:yourusername filename
This is especially useful if you’ve downloaded files or copied them from another user account.
SELinux or AppArmor Might Be Blocking Access
Some Linux distributions use security modules like SELinux or AppArmor which may silently block execution or access.
For SELinux (check status):
sestatus
Temporarily disable it for debugging (not recommended for production):
sudo setenforce 0
Or check audit logs:
sudo ausearch -m avc -ts recent
If you’re on Ubuntu and AppArmor is active, check profiles:
sudo aa-status
Check Mount Options
If you’re running a script from a mounted partition (like /media
or an NFS share), check if it’s mounted with noexec
option.
mount | grep yourmountpoint
If you see noexec
, remount it:
sudo mount -o remount,exec /mount/point
Hidden Characters or CRLF from Windows
Files edited in Windows might carry hidden carriage return characters (^M
) that cause permission errors.
Check with:
cat -A script.sh
To fix:
dos2unix script.sh
If you don’t have dos2unix
, install it via your package manager:
sudo apt install dos2unix
Executing a Directory Instead of a File
This might sound simple, but running a directory instead of a file gives the same error.
Double-check with:
file your_target
If it says “directory”, that’s your problem.
Wrong Shell in Script Shebang
If the shebang (#!
) in your script points to a non-existent shell, the script won’t execute.
Open the file and check the first line:
#!/bin/bash
which bash
If it doesn’t, adjust the shebang or install the missing shell.