"Permission denied" is a common error message encountered when using Linux, macOS, or other Unix-like systems, indicating that the current user doesn't have sufficient permissions to perform the requested operation on the target resource.

If you're using Windows, simply run the operation with administrator privileges.

Common Causes

Cause Explanation
File lacks execute permission When programs or script files don't have execute permissions
No read/write permissions Current user lacks read/write permissions for the file or directory
Attempting to access system resources Non-root user trying to access root-only resources

Solutions

1. Use root privileges to execute commands, such as deleting a file:

sudo rm filename.txt

For system operations:

sudo apt update

2. Change file permissions, for example adding execute permission:

chmod +x pythonscript.py

Changing file permissions requires you to be the file owner or root user.

Or make file readable, writable and executable by all users:

chmod 777 filename

3. Change file ownership:

sudo chown username:groupname filename

Example, changing ownership to current user:

sudo chown $(whoami):$(whoami) example.txt

Linux Permissions

In Linux systems, files have read, write, and execute permissions:

  • Read permission (r): Allows viewing file contents or listing directory contents
  • Write permission (w): Allows modifying file contents or adding/removing files from directory
  • Execute permission (x): Allows executing files or entering directories

Linux permissions apply to three user categories:

The root user can perform all operations.

  • Owner: The file creator or owner
  • Group: Other users in the same group as the owner
  • Others: All other users on the system

Use the ls -l command to view file/directory permissions, owner, and group:

ls -l filename
drwxr-xr-x 1 user group 4096 Feb  6  2022 filename

Here, user represents the file owner, group represents the group;

drwxr-xr-x (10 characters total) shows the file type and permissions for three user categories:

Character Position Meaning
1st character d File type: - for regular file, d for directory; other types exist.
2-4 rwx Owner permissions: read, write, execute
5-7 r-x Group permissions: read, execute
8-10 r-x Other users' permissions: read, execute