Linux: Permission Denied When Running Script โ How to Fix It
You try to run a script and the terminal slaps you with:
bash: ./script.sh: Permission denied
You know the file exists. You can see it right there. But Linux wonโt let you execute it.
What causes this
Every file on Linux has three permission sets: read (r), write (w), and execute (x) โ for the owner, the group, and everyone else. When you create a new file (or clone a repo, or download a script), it typically doesnโt have the execute bit set.
The Permission denied error means the current user doesnโt have execute permission on that file. This happens when:
- You created the script with a text editor (editors donโt set the execute bit)
- You downloaded the file from the internet
- You cloned a repo where the execute bit wasnโt tracked (Git does track it, but not all tools preserve it)
- The file is on a filesystem mounted with
noexec(common for/tmpon hardened systems) - The file is owned by another user and you donโt have the right group permissions
Fix 1: Add execute permission
The most common fix. Give the file execute permission:
chmod +x script.sh
./script.sh
If you want to be more specific about who gets execute permission:
# Only the owner
chmod u+x script.sh
# Owner and group
chmod ug+x script.sh
# Everyone (use with caution)
chmod a+x script.sh
Fix 2: Run with the interpreter directly
You donโt actually need execute permission if you invoke the interpreter yourself:
bash script.sh
Or for other languages:
python3 script.py
node script.js
This works because youโre executing bash (which you do have permission to run), and bash reads the script file โ it only needs read permission for that.
Fix 3: Check file ownership
If chmod +x gives you a permission error too, the file is probably owned by another user:
ls -la script.sh
# -rw-r--r-- 1 root root 1234 Mar 16 10:00 script.sh
If itโs owned by root or another user:
sudo chown $(whoami) script.sh
chmod +x script.sh
Or just run it with sudo if itโs meant to be a system script:
sudo ./script.sh
Fix 4: Check the filesystem mount options
If youโre running a script from /tmp or an external drive and chmod +x seems to have no effect:
mount | grep $(df script.sh --output=source | tail -1)
Look for noexec in the output. If the filesystem is mounted with noexec, you canโt execute anything on it regardless of file permissions. Copy the script to your home directory instead:
cp /tmp/script.sh ~/script.sh
chmod +x ~/script.sh
~/script.sh
Related resources
How to prevent it
- Add a
chmod +xstep in your README when distributing scripts - Use
git update-index --chmod=+x script.shto track the execute bit in Git - If youโre writing a Makefile or CI pipeline that generates scripts, add
chmod +xright after creating them - Use a shebang line (
#!/bin/bash) at the top of your scripts โ it doesnโt fix permissions, but it makes the intent clear and helps when using Fix 2
Related permission errors
- zsh: Permission Denied โ Fix for macOS and Linux
- Docker: Permission Denied
- npm EACCES: Permission Denied
- GitHub Actions: Permission Denied
- Nginx: Permission Denied
Related: Bash Cheat Sheet