Linux bash: Permission denied
Encountering "Permission denied" means your user lacks the necessary permissions to execute, read, or write a file or directory; this guide explains how to fix it.
What This Error Means
The "Permission denied" error in a Linux bash shell is a clear signal from the operating system that your current user account does not have the necessary access rights to perform a requested operation on a specific file or directory. Fundamentally, Linux, like other Unix-like systems, controls access to resources using a robust permissions model. Every file and directory has an owner, a group, and a set of permissions defining what the owner, members of the group, and all other users can do.
When you encounter this error, it means you're attempting one of the following without the required permissions:
* Execute a file: Trying to run a script (./script.sh) or a binary. This requires execute (x) permission.
* Read a file: Attempting to view the contents of a file (cat, less, more), copy it, or include it in an application. This requires read (r) permission.
* Write to a file: Trying to modify a file (nano, vi, echo >), save changes, or delete it (rm). This requires write (w) permission.
* Access a directory: Navigating into a directory (cd), listing its contents (ls), or creating/deleting files within it. Directories have slightly different interpretations for permissions:
* r on a directory means you can list its contents.
* w on a directory means you can create, delete, or rename files within it.
* x on a directory means you can traverse it (i.e., cd into it, or access files within it if you know their names). You can't cd into a directory without x permission, even if you have r.
This error is a security feature, preventing unauthorized users or processes from tampering with critical system files or other users' data.
Why It Happens
From my perspective as a systems engineer, "Permission denied" often surfaces due to a few common scenarios, usually stemming from how files are created, moved, or managed within a multi-user environment.
Here are the primary reasons why you might hit this roadblock:
- Default Permissions: When new files or directories are created, they inherit default permissions based on the system's
umasksetting. Sometimes, these defaults are too restrictive for a specific use case, especially for scripts that need to be executable. - Changed Context: A file or script might have been created by
root(e.g., during an installation or system update) or another user, and your current non-root user simply doesn't have the necessary rights. I've seen this many times in production when deployments are done by arootuser initially, and then a less privileged application user needs to access or modify those files. - Incorrect Ownership: A file might be owned by a different user or group than the one attempting to access it, and "other" permissions are too restrictive.
- Missing Execute Flag: This is a classic for shell scripts. You've written a perfectly good
bashscript, but forgot to make it executable. The system sees it as just another text file, not a program to run. - Security Hardening: On production systems, permissions are often intentionally tightened for security. While good practice, it can lead to "Permission denied" if the application or user trying to access the resource hasn't been explicitly granted the required permissions.
- Misconfigured
sudoorsu: If you've been usingsudoto perform some operations, you might inadvertently create files or directories owned byrootor another user, leading to permission issues for your regular user later.
Common Causes
Let's break down the specific operations that most frequently trigger a "Permission denied" error:
-
Executing a script or binary:
- You run
./myscript.shor/usr/local/bin/myprogramand get the error. - Cause: The file
myscript.shormyprogramlacks the execute (x) permission for your user, its group, or others. - Example:
ls -l myscript.shshows-rw-r--r--(noxfor anyone).
- You run
-
Writing or modifying a file:
- You try
echo "hello" > log.txt,nano config.ini,mv old.txt new.txt, orrm temp.fileand get the error. - Cause: You lack write (
w) permission onlog.txt,config.ini,old.txt,temp.file, or, critically, on the directory containing these files. Formvandrm, you often need write permission on the parent directory to create/delete directory entries. - Example:
ls -l log.txtshows-r--r--r--(nowfor anyone).ls -ld /path/to/directoryshowsdr-xr-xr-x(nowfor directory).
- You try
-
Reading a file:
- You attempt
cat secrets.conforless /var/log/app.logand get the error. - Cause: You lack read (
r) permission onsecrets.conforapp.log. - Example:
ls -l secrets.confshows---x--x--x(only execute, no read).
- You attempt
-
Changing directory (
cd) or listing contents (ls) of a directory:- You try
cd /some/restricted/dirorls /some/restricted/dirand get the error. - Cause: For
cd, you need execute (x) permission on the directory. Forls, you need read (r) permission on the directory. Often, a "Permission denied" oncdindicates missingx, and onlsindicates missingr. - Example:
ls -ld /some/restricted/dirshowsdrwx--x--x(cannotlsdue to norfor group/others, but cancdif you know sub-paths). Ord--x--x--x(cannotlsorcdif not owner).
- You try
-
Filesystem security layers (SELinux/AppArmor):
- Less common for basic users but crucial in hardened environments. Even if standard
chmod/chownsettings seem correct, these security frameworks can override and deny access based on policy. - Cause: A security context mismatch or a policy preventing the operation.
- Example: Even with
rwxrwxrwx, SELinux might block an Apache process from writing to a user's home directory if its context dictates otherwise.
- Less common for basic users but crucial in hardened environments. Even if standard
Step-by-Step Fix
When faced with "Permission denied," don't panic. Systematically diagnose the problem using these steps.
Step 1: Identify the exact resource and operation failing
Note the full path to the file or directory and the command you were trying to run. This sounds basic, but sometimes the error message can be misleading, or the real problem is a parent directory.
Step 2: Check the current permissions and ownership
Use the ls -l command, which provides detailed information about files and directories. For directories, use ls -ld to see the directory's permissions, not its contents.
ls -l /path/to/problematic_file_or_directory
# Example output: -rw-r--r-- 1 owner group 1024 Jan 1 10:00 problematic_file
# Example output for directory: drwxr-xr-x 2 owner group 4096 Jan 1 10:00 problematic_directory
Understanding the output:
* The first character (- or d) indicates file type (- for file, d for directory).
* The next nine characters are the permissions: three sets of rwx for owner, group, and others, respectively.
* r (read), w (write), x (execute). A - means the permission is absent.
* The numbers 1 or 2 after permissions refer to the number of hard links.
* owner: The user who owns the file.
* group: The group that owns the file.
* 1024 or 4096: File size in bytes.
Also, determine your current user and its groups:
id
# Example output: uid=1000(ingrid) gid=1000(ingrid) groups=1000(ingrid),4(adm),24(cdrom),27(sudo)
Compare your user/group with the file's owner/group. This will tell you which permission set (owner, group, or other) applies to your action.
Step 3: Determine the required permission
Based on the operation you're performing:
* Executing: Need x permission.
* Reading: Need r permission.
* Writing/modifying/deleting: Need w permission (on the file for modification, on the parent directory for creation/deletion).
* Navigating a directory: Need x permission on the directory.
* Listing directory contents: Need r permission on the directory.
Step 4: Change permissions (chmod)
If the permissions are too restrictive, use chmod to modify them. You can use symbolic mode (u+x, go-w) or octal mode (755, 644). In my experience, symbolic mode is clearer for specific adjustments, while octal is faster for standard patterns.
- To make a script executable (for the owner):
bash chmod u+x /path/to/script.sh # Or for owner, group, and others: chmod a+x /path/to/script.sh - To grant read/write to the owner, read to group and others (common for config files):
bash chmod 644 /path/to/config.ini
(6 =rw-, 4 =r--, 4 =r--) -
To grant read/write/execute to owner, read/execute to group and others (common for directories):
bash chmod 755 /path/to/directory
(7 =rwx, 5 =r-x, 5 =r-x)Caution: Avoid
chmod 777(rwxrwxrwx) unless absolutely necessary and you understand the security implications. It grants full access to everyone and is rarely the correct solution in production.
Step 5: Change ownership (chown)
If the file is owned by root or another user/group, and you need to access it as your current user, you might need to change its ownership. This often requires sudo.
- Change owner:
bash sudo chown your_user /path/to/file - Change owner and group:
bash sudo chown your_user:your_group /path/to/file - Recursive change for directories:
bash sudo chown -R your_user:your_group /path/to/directory
Step 6: Retry the operation
After adjusting permissions or ownership, attempt the original command again.
Step 7: Advanced checks (SELinux/AppArmor)
If permissions and ownership seem correct but you still get "Permission denied," consider if SELinux or AppArmor are active and enforcing policies.
* SELinux: Check its status with sestatus. If enabled, you might need to use chcon to adjust the security context or investigate SELinux logs (audit.log) for denials.
* AppArmor: Check its status with aa-status. Denials might appear in system logs (dmesg, syslog).
These are typically system-level concerns and might require administrator privileges and deeper understanding.
Code Examples
Here are some concise, copy-paste ready code examples for common permission issues.
Checking permissions and ownership:
# Check a file's details
ls -l my_script.sh
# Check a directory's details (not its contents)
ls -ld /var/log/myapp
Making a script executable:
# Add execute permission for the owner
chmod u+x my_script.sh
# Add execute permission for all users
chmod a+x my_script.sh
# Set common executable permissions for owner/group/others (rwxr-xr-x)
chmod 755 my_script.sh
Setting read/write permissions for files:
# Owner can read/write, group/others can only read (rw-r--r--)
chmod 644 config.yaml
# Owner can read/write, group can read, others no access (rw-r-----)
chmod 640 sensitive.log
Setting permissions for directories:
# Owner rwx, group r-x, others r-x (rwxr-xr-x) - common for directories
chmod 755 my_project_dir
# Owner rwx, group rwx, others no access (rwxrwx---) - for shared dirs with a specific group
chmod 770 /srv/shared_data
Changing ownership (requires sudo):
# Change owner of a file to 'myuser'
sudo chown myuser my_document.txt
# Change owner and group of a file to 'myuser' and 'mygroup'
sudo chown myuser:mygroup another_file.txt
# Recursively change owner and group of a directory and its contents
sudo chown -R myuser:mygroup /path/to/my_app_data
Identifying your current user and groups:
# Show current user ID and groups
id
# Show just your username
whoami
Environment-Specific Notes
The "Permission denied" error often manifests differently or requires specific considerations depending on your environment.
Cloud Environments (AWS EC2, GCP Compute Engine, Azure VMs)
- SSH Key Permissions: When connecting to cloud instances, your SSH private key must have very restrictive permissions (
chmod 400). If not, SSH will deny access with a similar-sounding error, though it's not abashpermission error on a file you're trying to execute.
bash chmod 400 my-key.pem - Default Users: Cloud providers often provision VMs with a default user (e.g.,
ec2-useron Amazon Linux,ubuntuon Ubuntu AMIs,gclouduseron GCP). These users typically havesudoaccess, but their home directories and created files will be owned by them, notroot. Operations that requirerootaccess (like writing to/etcor/var/logfor system services) will needsudo. - IAM Roles: While not directly file permissions, cloud IAM roles control what your instance itself can do. If an application running on your instance needs to write to an S3 bucket or access a database, and you get an access denied, it's often an IAM policy issue, not a local file permission one. However, if the application needs to write a local log file, that's back to
chmod/chown. - Ephemeral Storage: Be aware of ephemeral storage (like instance store volumes on EC2). Permissions set there might not persist across reboots, requiring a re-application during startup scripts.
Docker Containers
Docker introduces a layer of abstraction that can complicate permissions. I've debugged countless "Permission denied" errors within containers.
- Container User vs. Host User: By default, processes inside a Docker container run as
root(UID 0) unless specified otherwise (USERinstruction in Dockerfile ordocker run -u). If you're mounting volumes from the host, the permissions of files on the host are crucial. A file owned byyouruser:yourgroup(UID 1000:GID 1000) on the host, when mounted into a container running asroot, will often still show as owned by UID 1000:GID 1000 inside the container. If the container process is then switched to a user with a different UID, it might not have access. - Volume Mounts: Permissions on the host machine for the source directory of a bind mount directly affect what the container can do. If
/host/dataisdrwxr-xr-xand owned byroot, and you mount it as/app/datain a container, a non-root user inside the container likely won't be able to write to/app/data.- Fix: Ensure the host directory's permissions and ownership are compatible with the user the container process runs as. Sometimes
chown -Ron the host directory or evenchmod -R 777(with caution!) is necessary, or you mightchownwithin the container duringdocker buildordocker-entrypoint.sh.
- Fix: Ensure the host directory's permissions and ownership are compatible with the user the container process runs as. Sometimes
USERInstruction: UsingUSER <username_or_uid>in your Dockerfile is good practice for security, but it means that user must have all necessary permissions inside the container for the application to run.docker cp: Permissions of files copied into a container usingdocker cpwill inherit theumaskof the container's environment and the user executing thecpcommand within the container context (oftenroot).
Local Development Environments
sudoOveruse: Developers sometimes get into the habit of prefixing every command withsudoto bypass permission issues. This often leads to files being created or modified byroot, which then causes "Permission denied" when the developer tries to access them later as their regular user.- Shared Drives (NFS, SMB): If you're working on files located on network-attached storage, permissions can be governed by the network file system itself, not just local Linux permissions. User ID mapping (
uid/gid mapping) between your local machine and the server providing the share is critical here. umaskSettings: Yourumask(user file-creation mask) dictates the default permissions for new files and directories you create. A restrictiveumask(e.g.,077) can mean all new files are only accessible by you, leading to issues if other local processes or users need access. Check yourumaskwithumask. You can temporarily set it withumask 022for standard644/755defaults.
Frequently Asked Questions
Q: When should I use chmod 777?
A: Almost never. chmod 777 grants read, write, and execute permissions to everyone (owner, group, and others). While it will certainly fix a permission denied error, it's a significant security risk, especially in production environments, as any user or process could then modify or execute that file. Use it only for temporary debugging if you understand the risks, and revert immediately.
Q: What's the difference between rwx and octal permissions (e.g., 755)?
A: They are two ways to represent the same permissions. rwx is symbolic (read, write, execute), while octal is numeric. Each permission (r, w, x) has a value: r=4, w=2, x=1. Summing these for each of the three sets (owner, group, others) gives the octal number. For example, rwx = 4+2+1 = 7; r-x = 4+0+1 = 5. So, rwxr-xr-x is 755.
Q: Can sudo fix any "Permission denied" error?
A: sudo allows you to run a command as root (or another specified user), which typically has permission to do anything. So, sudo can usually bypass a permission denied error for that specific command execution. However, it doesn't change the underlying file permissions or ownership. If a non-root application needs to access that file later, it will still face the same permission issue unless you explicitly use sudo chown or sudo chmod to fix the permissions permanently for the non-root user.
Q: Why do I get "Permission denied" when trying to cd into a directory, even if ls shows I have read access?
A: To cd (change directory) into a directory, you need the "execute" (x) permission on that directory, not just read (r). The execute permission on a directory allows you to traverse it and access files within it. Read permission on a directory only allows you to list its contents (ls). You might see drwxr----- for a directory; you can cd if you're the owner, but ls will fail for "others" even if r was granted, because x is missing.
Q: Does sudo change the ownership of files I create or modify?
A: When you run a command with sudo, that command is executed as root. If that command creates a new file, the file will typically be owned by root. If it modifies an existing file, the ownership of the file generally remains unchanged unless the command explicitly changes it (e.g., sudo chown). Be mindful of this; frequently creating files with sudo can lead to a directory full of root-owned files that your regular user can't modify.
Related Errors
(none)