πŸ”§ Error Fixes
Β· 5 min read
Last updated on

Permission Denied β€” How to Fix It (Linux, Mac, Windows)


bash: ./script.sh: Permission denied
PermissionError: [Errno 13] Permission denied
EACCES: permission denied, open '/usr/local/lib/node_modules'

β€œPermission denied” means your current user account doesn’t have the required access level (read, write, or execute) for the file, directory, or operation you’re attempting.

How Linux/Mac permissions work

Every file and directory has three permission sets:

  • Owner β€” the user who created it
  • Group β€” users in the file’s group
  • Others β€” everyone else

Each set has three permissions:

  • r (read) β€” view file contents or list directory
  • w (write) β€” modify file or create/delete files in directory
  • x (execute) β€” run as program or enter directory
# Example: ls -la output
-rwxr-xr-- 1 alice developers 4096 May 15 script.sh
β”‚β”œβ”€β”€β”œβ”€β”€β”œβ”€β”€
β”‚ β”‚   β”‚   └── Others: read only
β”‚ β”‚   └────── Group: read + execute
β”‚ └────────── Owner: read + write + execute
└──────────── File type (- = file, d = directory)

Fix 1: Make a script executable

The most common case β€” you created a script but forgot to add execute permission.

# ❌ Script isn't executable
./deploy.sh  # Permission denied

# βœ… Add execute permission for the owner
chmod +x deploy.sh
./deploy.sh  # Works

# Or be specific about who gets execute
chmod u+x deploy.sh   # Only owner
chmod a+x deploy.sh   # Everyone

Why this happens: When you create a file (with a text editor, touch, or download), it doesn’t get execute permission by default. This is a security feature β€” you must explicitly mark files as executable.

Fix 2: Fix file ownership

If a file is owned by root or another user, you can’t modify it even if the permissions look right.

# Check ownership
ls -la config.yml
# -rw-r--r-- 1 root root 256 May 15 config.yml
#              ^^^^ owned by root!

# Change ownership to your user
sudo chown $USER:$USER config.yml

# Recursively for a directory
sudo chown -R $USER:$USER ./my-project/

# Change only the group
sudo chgrp developers shared-folder/

Common cause: Running a command with sudo creates files owned by root. Then your normal user can’t modify them. Avoid sudo unless necessary.

Fix 3: Use sudo correctly

Some operations legitimately require root access β€” writing to system directories, installing system packages, modifying system config.

# ❌ Direct write to system directory
echo "127.0.0.1 myapp.local" > /etc/hosts  # Permission denied

# βœ… Use sudo with tee (preserves redirect)
echo "127.0.0.1 myapp.local" | sudo tee -a /etc/hosts

# ❌ Don't do this β€” creates root-owned files in your project
sudo npm install
sudo pip install

# βœ… Fix the underlying permission issue instead

Rule: If you need sudo for development tools (npm, pip, docker), the tool is misconfigured. Fix the configuration rather than using sudo as a workaround.

Fix 4: SSH key permissions

SSH is strict about key file permissions. If your private key is readable by others, SSH refuses to use it.

# ❌ Key permissions too open
ssh user@server
# Permissions 0644 for '/home/user/.ssh/id_rsa' are too open.
# It is required that your private key files are NOT accessible by others.

# βœ… Fix SSH directory and key permissions
chmod 700 ~/.ssh              # Directory: owner only
chmod 600 ~/.ssh/id_rsa       # Private key: owner read/write only
chmod 644 ~/.ssh/id_rsa.pub   # Public key: owner read/write, others read
chmod 644 ~/.ssh/authorized_keys
chmod 644 ~/.ssh/known_hosts
chmod 600 ~/.ssh/config       # SSH config: owner only

Fix 5: npm/Node.js EACCES errors

# ❌ Global install fails
npm install -g typescript
# EACCES: permission denied, mkdir '/usr/local/lib/node_modules/typescript'

Don’t use sudo npm install -g β€” it creates root-owned files that cause more problems later.

Option A: Change npm’s default directory:

mkdir -p ~/.npm-global
npm config set prefix '~/.npm-global'
echo 'export PATH=~/.npm-global/bin:$PATH' >> ~/.bashrc  # or ~/.zshrc
source ~/.bashrc
npm install -g typescript  # Works without sudo

Option B: Use nvm (recommended):

# nvm installs Node in your home directory β€” no permission issues
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
nvm install --lts
npm install -g typescript  # Works without sudo

Fix 6: Docker permission denied

# ❌ Can't run docker commands
docker ps
# permission denied while trying to connect to the Docker daemon socket

# βœ… Add your user to the docker group
sudo usermod -aG docker $USER

# IMPORTANT: Log out and back in (or restart) for the group change to take effect
# Verify:
groups  # Should show 'docker' in the list

On macOS: Docker Desktop handles permissions automatically. If you get permission errors, restart Docker Desktop.

Fix 7: Python PermissionError

# ❌ Writing to a directory you don't own
with open('/var/log/myapp.log', 'w') as f:  # PermissionError

# βœ… Write to a user-accessible location
with open(os.path.expanduser('~/myapp.log'), 'w') as f:

# βœ… Or create the directory with proper permissions first
os.makedirs('/var/log/myapp', exist_ok=True)  # Needs sudo once

For pip install errors:

# ❌ System Python is protected
pip install requests  # Permission denied

# βœ… Use a virtual environment
python -m venv .venv
source .venv/bin/activate
pip install requests  # Works β€” installs in .venv/

Fix 8: Git permission denied

# ❌ Can't push to remote
git push origin main
# Permission denied (publickey)

# Check if your SSH key is loaded
ssh-add -l

# If empty, add your key
ssh-add ~/.ssh/id_rsa

# Test the connection
ssh -T git@github.com

See Git Permission Denied Publickey β€” Fix for a complete guide.

Permission numbers cheat sheet

NumberPermissionMeaning
7rwxRead + Write + Execute
6rw-Read + Write
5r-xRead + Execute
4rβ€”Read only
0---No access

Common combinations:

chmod 755 directory/   # Owner: full, Others: read+execute (standard for directories)
chmod 644 file.txt     # Owner: read+write, Others: read (standard for files)
chmod 600 secret.key   # Owner only (private keys, credentials)
chmod 777 file         # ❌ NEVER do this β€” everyone can read/write/execute

FAQ

Should I use chmod 777 to fix permission errors?

No. chmod 777 gives everyone full access, which is a security risk. It’s the equivalent of leaving your front door open. Find the specific permission needed and grant only that.

Why do I get β€œOperation not permitted” instead of β€œPermission denied”?

β€œOperation not permitted” (EPERM) means even root can’t do it β€” the operation is blocked by the system (e.g., modifying immutable files, SIP on macOS, or SELinux). β€œPermission denied” (EACCES) means your user lacks the required permission but root could do it.

How do I fix permissions on macOS with SIP?

System Integrity Protection prevents modifying /usr/bin, /System, and other protected paths β€” even with sudo. You can’t disable it without booting into Recovery Mode. Instead, install tools to /usr/local/bin (Homebrew does this automatically).

What’s the difference between chmod and chown?

chmod changes what actions are allowed (read/write/execute). chown changes who owns the file. You might need both: chown to make yourself the owner, then chmod to set the right permissions.