How To Resolve Permission Denied Error on Linux (Linux Permission Denied)

In the Linux operating system, executing commands requires proper permission. Each file and directory has associated permissions like read, write, or execute. 

Suppose you encounter a chmod permission denied or Linux permission denied error in Linux. In that case, it means your user account lacks the necessary permissions, or you don’t have enough read, write, or execute permissions on a file or directory you want to use. 

This error can be resolved by obtaining the appropriate access to the file or directory. Permissions in Linux delegate access to users and groups for independently reading, writing, or executing files. Just being able to open a file doesn't guarantee editing privileges.

This tutorial demonstrates how to resolve the Linux Permission Denied error on Linux. The solutions provided include adjusting file permissions and accessing protected documents by elevating to the root account. Follow the steps outlined below to edit file permissions and rectify the error.

What Are the Causes of Linux Permission Denied Error? (Root Permission Denied)

The permission denied Linux error occurs when attempting to execute a command without having the necessary execute permission. Similarly, read or write actions are restricted if the respective permissions for files or directories are lacking. Linux permissions are important for its security, safeguarding data from unauthorized access. Below, we have listed common permissions that are applied to a file or directory in Linux:

Read (r): Allows viewing the content of a file or directory. For example, with read permission, one can read text file contents or list directory files.

Write (w): Permits modifying file contents or creating new files within a directory. Without having written permission, changes to files or additions to directories are prohibited.

Execute (x): Specifically for files containing executable code like shell scripts or binary executables. Execute permission enables running these files as programs.

These permissions apply to three user categories:

Owner (u): The user who owns the file or directory.

Group (g): A designated group of users to which the file or directory belongs.

Others (o): All users who are neither the owner nor part of the group.

Besides these basic permissions, Linux offers advanced features like setuid, setgid, and sticky bits, which influence how permissions operate for certain files and directories.

What Are the Common Use Cases of Linux Permission Denied Error?

"Permission Denied" errors in Linux can arise in several situations due to its robust security measures. Understanding these common scenarios is essential for effective troubleshooting and prevention. Here are some typical examples when you encounter permission denied as root errors:

  • Users might encounter "Permission Denied" errors while installing or updating software packages using package managers like apt or yum. This occurs if they lack the required privileges to modify system files or directories where packages are installed.
  • Modifying system configuration files, such as those in the /etc directory, typically necessitates root or sudo privileges. Users may face "Permission Denied" errors when attempting to edit these crucial files without the necessary access.
  • In a networked environment, accessing shared directories or files on remote servers may lead to "Permission Denied" errors if the user doesn't have the necessary permissions granted by the remote system.
  • Permission issues may arise when users try to perform actions within their home directories, such as creating, deleting, or modifying files and directories.
  • Running shell scripts or executable files without the appropriate execute permissions can trigger "Permission Denied" errors. Users must ensure that scripts have the necessary execute permissions.
  • Users may encounter problems when attempting to change the ownership of files or directories if they lack the required permissions for the ownership transfer.

How to Fix Permission Denied Error on Linux? (chmod Permission Denied)

To fix permission denied Linux error, first, verify your privileges for the particular file or folder using the following command:

$ ls -la

The “ls” command will display a detailed listing of all files and folders, including their permissions, as illustrated below.

how to resolve permission denied error on linux (linux permission denied)

The Permission Denied error can occur on both files and directories. Here's an example of what it looks like when attempting to execute a script without the necessary executable permissions for a user account:

how to resolve permission denied error on linux (linux permission denied)

If you receive this type of error, it indicates that your current user account lacks the necessary permissions configured to interact with the file as intended. To resolve this error, you can use the chown or chmod commands to adjust permissions and grant the required access to the file.

Perform the following steps to fix the Linux permission denied error:


Step 1: First, check the permissions assigned to the file using the ls -l command. This command provides detailed information about the owner, group, and permissions for any specified file or directory. Make sure to include the file or directory path as an argument.

$ ls -la test.sh

how to resolve permission denied error on linux (linux permission denied)

The above output shows that both the owner and group possess read and write permissions, while other users have only read permissions. Notably, there are no execute permissions assigned to the file. This lack of execute permissions explains why the error occurs when attempting to execute it.

Step 2: To resolve the Linux permission denied error, you will grant the necessary permissions using the chmod command. For example, assign read, write, and execute permissions to a file using the below command:

$ chmod +rwx test.sh

Step 3: After assigning the appropriate permissions, again run the test script using the below command:

$./test.sh

Now, you will notice the shell script output will execute on your terminal window.

how to resolve permission denied error on linux (linux permission denied)

Resolved Permission Denied Error (chown Permission Denied)

To resolve the permission denied error, take ownership of the file when necessary, which typically grants you more permissions compared to other users. Alternatively, you can adjust permissions as needed. Use the chown command to change the owner of the file to a specific user, such as samreena.

$ chown samreena test.sh

Alternatively, absolute mode, represented by numerical values, can be employed instead of symbolic mode (rwx). Here is an example:

Grant full permissions (read, write, execute) exclusively to the file owner and read permissions to all other users:

$ chmod 744 file-name

Grant full permissions (read, write, execute) to all users:

$ chmod 777 file-name

In this example, the digit 7, representing the owner, signifies full permissions (rwx) granted as:

  • 4 for read;
  • 2 for writing;
  • 1 for execution.

This totals 7, granting the owner full access.

The digit 4, representing the group, indicates read-only permissions (r--) as:

4 for read;

With no write or execute permissions granted to group members.

Similarly, the digit 4 for others also signifies read-only permissions (r--) with:

4 for read;

No write or execute permissions for all other users.

In octal representation, each digit sums up its constituent permissions (4 for read, 2 for write, 1 for execute), while 0 means no permissions are set. The first digit sets permissions for the user, the second for the group, and the third for others.

Troubleshooting Tips for "Permission Denied" Errors:

Identifying the root cause of "Permission Denied" errors is important for resolving them effectively. Here are some troubleshooting tips that will assist you in identifying and addressing the issue:

Check Error Messages: Pay attention to error messages for insights into the permission issue.

Review Log Files: Examine system logs for relevant entries related to permissions.

Verify File Permissions: Use ls -l to ensure permissions are correctly set.

Ownership Confirmation: Confirm file or directory ownership.

Use sudo or su: Consider using sudo or su for elevated privileges.

Check Disk Space: Insufficient disk space can cause permission errors.

Network Access: Ensure network configurations are correctly set for remote file access.

Conclusion 

In this tutorial, we learned how to fix the Permission Denied error on a Linux system. This error occurs when the essential permissions to read, write, or execute a file or directory are lacking. 

Solutions discussed include adjusting file permissions using the chmod command, changing ownership with chown, or utilizing administrator privileges via sudo. It's important to note that the root user account always retains full permissions on any file, irrespective of its configured permissions.

This guide covers various methods to resolve permission-denied errors in Linux using the commands and methods listed above. If you believe there are alternative approaches to achieve the same goal, feel free to share them in the comments section. Additionally, you can consider purchasing a Linux VPS server to practice and test the commands mentioned here.