Attempting to execute an automation script, software deployment installer, or custom utility in the Linux command line by running ./script.sh only to be blocked with the error message: “bash: ./script.sh: Permission denied” or “-bash: ./setup.sh: Permission denied” halts administration workflows immediately. In Linux security architecture, the kernel verifies discretionary access control (DAC) permissions before attempting to load an executable into memory. Unlike Windows (which determines executability based on file extensions like .exe or .bat), Linux completely ignores file extensions and strictly requires the POSIX executable bit (x) to be set on the file’s mode. Furthermore, filesystem mount options (such as noexec) or mandatory access controls (SELinux / AppArmor) will prevent binary execution even if you are logged in as the root superuser. This guide explains why this permission barrier occurs and provides verified methods to fix it.
What Causes ‘Permission denied’ When Executing Scripts?
The Linux kernel rejects script execution under several permission and filesystem conditions:
- Missing Executable Permissions (Mode Bits): The file was newly created or downloaded via
git,curl, or text editors with default read/write permissions (e.g.,0644/-rw-r--r--) lacking the execute bit (+x). - Filesystem Mounted with the
noexecFlag: The script resides on a partition (frequently/tmp,/var/tmp, removable USB drives, or NFS shares) mounted with thenoexecsecurity mount option. - Wrong File Ownership and Group Attributes: The script is owned by another user and lacks read/execute permissions for your active user account or group.
- Malformed or Missing Shebang Line: An invalid interpreter path specified in the file header (e.g., pointing to a non-existent
#!/bin/bash^Mwith Windows CRLF carriage returns). - SELinux or AppArmor Security Context Blocks: Security policies restricting scripts located outside standard executable directories from calling system binaries.
Method 1: Grant Executable Permissions via chmod +x
The standard resolution for newly created or downloaded scripts is granting execute rights using the chmod command.
- Open your Linux terminal and navigate to the directory containing your script:
cd /path/to/directory - Inspect the current permissions and ownership of the script using
ls -l:
If the permissions displayls -l script.sh-rw-r--r--, the file lacks executable flags. - Grant executable permissions to the current user (or all users):
# Grant execute to all users: chmod +x script.sh # Or grant execute strictly to the file owner: chmod u+x script.sh - Verify the updated permissions: the output should now display
-rwxr-xr-xand the filename may appear in green text. - Execute the script normally:
./script.sh
Method 2: Execute Directly via the Shell Interpreter
If you cannot or do not wish to modify file permissions, passing the script directly as an argument to the Bash interpreter bypasses the execute bit requirement.
- In your terminal, invoke
bashorshdirectly followed by the script filename:bash script.sh # Or for POSIX standard shells: sh script.sh - How this works: The Bash binary itself (
/bin/bash) already has the executable bit enabled. When invoked this way, Bash merely needs read (r) permission onscript.shto parse its commands line by line. - If the script requires superuser privileges, run:
sudo bash script.sh
Method 3: Fix Filesystem ‘noexec’ Mount Restrictions
In enterprise Linux environments, system administrators frequently mount temporary directories (like /tmp) with the noexec flag to prevent malicious payload execution.
- Check if the filesystem hosting your script is mounted with
noexec:mount | grep " $(df -P . | tail -1 | awk '{print $6}') " - Inspect the mount options inside the parentheses. If you see
noexec, the kernel will refuse to execute any binary or script on this partition regardless ofchmod +xor root privileges. - Remediation Options:
- Move the script: Move your script to an executable partition (such as your user home directory
~/scriptsor/opt):mv /tmp/script.sh ~/script.sh && cd ~ && ./script.sh - Temporarily remount the volume with execution enabled:
sudo mount -o remount,exec /tmp
- Move the script: Move your script to an executable partition (such as your user home directory
Method 4: Convert Windows Line Endings (CRLF to LF)
Scripts authored on Windows systems contain carriage return characters (
or ^M) that corrupt the shebang line.
- Check if your script contains Windows-style CRLF line endings:
If you seehead -n 1 script.sh | cat -A#!/bin/bash^M$, the kernel attempts to invoke an interpreter namedbash, which fails with permission or not found errors. - Install and run the
dos2unixconversion utility:sudo apt install dos2unix # Debian / Ubuntu sudo dnf install dos2unix # RHEL / Fedora dos2unix script.sh - Alternatively, strip carriage returns using
sed:sed -i 's/ $//' script.sh - Test running
./script.shagain.
Method 5: Check File Ownership and SELinux Contexts
If the file is owned by another user or blocked by mandatory access controls:
- Verify that your user account owns the file:
sudo chown $USER:$USER script.sh - On RHEL, AlmaLinux, or Fedora systems running SELinux, check the security context:
ls -Z script.sh - If the context is mislabeled, restore the default target context:
restorecon -v script.sh
Script Permission Denied Troubleshooting Matrix
| Cause | Diagnostic Command | Primary Fix |
|---|---|---|
| Missing execute bit | ls -l shows -rw-r–r– | chmod +x script.sh |
| noexec mount option | mount | grep noexec | Move script to home directory (~/) |
| Windows line breaks | cat -A shows ^M | dos2unix script.sh |
| Script execution locked | Direct execution forbidden | Run explicitly with: bash script.sh |
Applying the executable bit and ensuring the script resides on an executable filesystem permanently resolves permission denied errors in the Linux terminal.