chmod Permission Denied on Script — Making Shell Scripts Executable on Linux
About chmod Permission Denied on Script
Fix permission errors when trying to run or chmod shell scripts on Linux, including execute permission, shebang lines, and filesystem restrictions. This guide covers everything you need to know about this topic, including common causes, step-by-step solutions, and answers to frequently asked questions.
Here are the key things to understand: Shell scripts require execute (x) permission to run directly: chmod +x script.sh. The shebang line (#!/bin/bash) tells the kernel which interpreter to use for the script. Scripts on FAT32, NTFS, or noexec-mounted filesystems cannot be made executable. Even root cannot set execute permission on noexec filesystems. Scripts can always be run via the interpreter directly: bash script.sh (bypasses execute permission). Understanding these fundamentals will help you diagnose and resolve this issue more effectively.
The most common reasons this occurs include: Script does not have execute permission (missing chmod +x after creation). Script on a FAT32/NTFS partition that does not support Unix permissions. Filesystem mounted with noexec option preventing execution of any scripts/binaries. Script has Windows-style line endings (CRLF) causing the shebang to fail. Identifying the root cause is the first step toward finding the right solution.
To resolve this, follow these recommended steps: Add execute permission: 'chmod +x script.sh' then run with './script.sh'. If chmod fails on FAT/NTFS: run the script via interpreter directly: 'bash script.sh'. Check filesystem mount options: 'mount | grep noexec' — remount without noexec if appropriate. Fix line endings: 'dos2unix script.sh' or 'sed -i "s/\r//" script.sh' for Windows-created scripts. Verify shebang: first line should be #!/bin/bash or #!/usr/bin/env bash (with no leading spaces). If these steps do not resolve the issue, consider consulting additional resources or a qualified professional.
This article is part of our Linux Error Codes collection on Error Codes Wiki. We provide comprehensive, up-to-date information to help you find solutions quickly.
Quick Answer
Why can I run 'bash script.sh' but not './script.sh'?
'bash script.sh' launches bash and tells it to read the file as input — no execute permission needed on the file. './script.sh' asks the kernel to execute the file directly, which requires the execute permission bit to be set.
Overview
Fix permission errors when trying to run or chmod shell scripts on Linux, including execute permission, shebang lines, and filesystem restrictions.
Key Details
- Shell scripts require execute (x) permission to run directly: chmod +x script.sh
- The shebang line (#!/bin/bash) tells the kernel which interpreter to use for the script
- Scripts on FAT32, NTFS, or noexec-mounted filesystems cannot be made executable
- Even root cannot set execute permission on noexec filesystems
- Scripts can always be run via the interpreter directly: bash script.sh (bypasses execute permission)
Common Causes
- Script does not have execute permission (missing chmod +x after creation)
- Script on a FAT32/NTFS partition that does not support Unix permissions
- Filesystem mounted with noexec option preventing execution of any scripts/binaries
- Script has Windows-style line endings (CRLF) causing the shebang to fail
Steps
- 1Add execute permission: 'chmod +x script.sh' then run with './script.sh'
- 2If chmod fails on FAT/NTFS: run the script via interpreter directly: 'bash script.sh'
- 3Check filesystem mount options: 'mount | grep noexec' — remount without noexec if appropriate
- 4Fix line endings: 'dos2unix script.sh' or 'sed -i "s/\r//" script.sh' for Windows-created scripts
- 5Verify shebang: first line should be #!/bin/bash or #!/usr/bin/env bash (with no leading spaces)