error linux

Linux Exit Code 126 - Command Not Executable

Understanding Linux exit code 126 - the command was found but could not be executed, usually due to permission issues.

What It Means

Exit code 126 indicates that the command was found on the system but could not be executed. This is almost always a permissions issue — the file exists but lacks the execute permission bit, or the file format is not recognized as an executable.

Common Causes

  • Script file missing the execute permission (chmod +x)
  • Binary file with wrong permissions
  • Trying to execute a non-executable file (e.g., a text file)
  • Wrong file format for the system architecture (e.g., ARM binary on x86)
  • Missing or incorrect shebang line (#!/bin/bash)
  • File system mounted with noexec option
  • SELinux or AppArmor denying execution

How to Fix

Add execute permissions

# Check current permissions
ls -la script.sh
# -rw-r--r-- 1 user user 1234 Jan 1 12:00 script.sh  (no 'x' flag)

# Add execute permission
chmod +x script.sh

# Or set specific permissions
chmod 755 script.sh  # rwxr-xr-x

# Now it can be run
./script.sh
echo $?  # 0 (success)

Add a proper shebang line

# Script must start with a shebang line
#!/bin/bash
echo "Hello, World!"

# For Python scripts
#!/usr/bin/env python3
print("Hello, World!")

# For Node.js scripts
#!/usr/bin/env node
console.log("Hello, World!");

Run with an interpreter explicitly

# Instead of executing directly, call the interpreter
bash script.sh
python3 script.py
node script.js

Check for noexec mount

# Check if the filesystem is mounted with noexec
mount | grep noexec

# If /tmp is noexec, copy to a different location
cp /tmp/script.sh ~/script.sh
chmod +x ~/script.sh
~/script.sh

# Or remount without noexec (requires root)
sudo mount -o remount,exec /tmp

Check file format

# Verify the file type
file ./mybinary
# Should show: ELF 64-bit LSB executable, x86-64...
# Not: ASCII text, or ELF 32-bit (on a 64-bit system)

# Check for Windows line endings (can cause issues with shebang)
file script.sh
# If it shows "with CRLF line terminators":
dos2unix script.sh