How to Resolve the “Permission Denied” Error in Linux

Published on January 19, 2024

With Linux, permissions define how to read, write, or execute a file. The Linux file system allows editing of the file permissions for different users and groups to restrict access. Therefore, when you encounter the “permission denied” error, it signals that you are trying to read, write, or execute a given file but don’t have the needed permissions. Unless you are root, you will often encounter the permission denied error. This post discusses two scenarios where the error arises and gives solutions to work around it.

Scenario 1: Resolving the “Permission Denied” Error for Updates and Install Actions

A Linux system can have numerous users sharing the same resources. In most instances, administrative tasks such as updating the system or installing new packages are a privilege best left for the admin to avoid messing with the system. Thus, when you try to run an update or install command without root privileges, you will get the “permission denied” error. Here are two examples demonstrating the same.

Example 1: Trying to Update the Repository

The apt update command lets you update your system’s repository. However, executing it and getting the error below signals that you are unauthorized to perform the action. You will face a lock limiting access to the required files for the update.

Example 2: Trying to Install a Package

The install privilege is limited depending on your permissions to avoid messing up the system. Therefore, trying to install a package, as we’ve done below, will also bring the “permission denied” error.

If you have any of the above instances, there are two ways to resolve the error in Linux.

Solution 1: Use sudo

The “sudo” command gives you administrative privilege and lets you run any command as the root. However, you must have the root password and enter it when prompted.

Returning to the earlier update command, we can add the sudo when running it. This time, you will get prompted to enter the password, and your update command will execute successfully.

Solution 2: Switch to Root

Instead of having to type sudo whenever you want to run administrative tasks, switching to root is the better option. For that, run the sudo su command and escalate your privilege to root.

Once you are root, you can run any command and won’t face any “permission denied” error or get prompted to enter your password as confirmation.

Scenario 2: Resolving the “Permission Denied” Error When Working with Files and Folders

Example 1: Working with Files

If we have the text file below, we can get the “permission denied” error when trying to read its contents.

Such a case implies that we don’t have the read permissions to that file. There are a few steps to follow to fix such a case quickly.

Step 1: Check the File Permissions

Use the ls -l command to long list the target file to allow you to check what file permissions exist on it. Based on the below output, we don’t have the read (r) permission for the user, group, and others categories.

Step 2: chmod the Permission

To fix this error, adding the read permission will do the trick. Use the chmod command to specify the permission to add and the target file.

Step 3: Recheck the File Permissions

Rerun the ls -l command and verify that you now have the read permissions. With that, you can now read the file contents using the cat command and won’t get the “permission denied” error again.

Sometimes, you can also get the “permission denied” error in Linux when writing to a file.

Such a case signals that you don’t have the write permissions to that file. Running the ls -l command verifies the current permissions. We see that the user category doesn’t have the write (w) permission.

To resolve such a case, use the chmod command and give the +w permission to the user category. With that, you will resolve the error and manage to write to the file.

Example 2: Working with Folders

Even folders have permissions; unless you have the correct ones, depending on the task you want to perform, you can face the “permission denied” error.

For this example, we’ve tried to read the folder’s contents using the ls command but got an error.

Similarly, trying to write to the folder by creating a file using the touch command also throws the “permission denied” error.

Such a case shows that we lack read and write permissions. We can verify this by running the ls -ld command.

Based on the above output, the user lacks the rw (read and write) permissions while the group lacks the r and the others lack the rw permissions.

Suppose we want to ensure everyone can read the contents of the folder. We will chmod +r for the folder. For the write permission, we can specify to give it to the user using the chmod u+w command.

The last step is to attempt to create a file inside the folder and read the folder’s contents to see if we managed to resolve the “permission denied” error, as shown below.

That’s how to resolve the “permission denied” error in Linux for different scenarios.

Conclusion

The “permission denied” error signals that you lack permission to perform a given task in Linux. These permissions vary based on the ownership or the mode assigned to the particular file. We’ve seen different scenarios where you can encounter the “permission denied” error in Linux and given the steps to resolve the error.

New to LinuxMeta? Get Started Now! 

Instantly Deploy Linux & Windows KVM VPS at a Cheap Price