How To solve Permission Denied Error On Ubuntu / Debian Linux

As a user of Ubuntu or Debian-based Linux distributions, you may have encountered the frustrating “Permission Denied” error while accessing specific files and directories. This error is typically caused by inadequate file permissions or ownership settings, preventing you from accessing the required resources. This article will explore three methods to resolve the “Permission Denied” error on Ubuntu and Debian systems.

These methods include using the sudo command for temporarily elevated privileges, setting the right system permissions with chmod, and changing the file ownership using chown. Following the step-by-step guide, you can quickly and effectively resolve permission issues and regain access to your desired files or directories.

Steps to fix the permission denied error on Ubuntu or Debian Linux

Here, let’s review the steps to fix the permission denied error on our Ubuntu or Debian-based systems.

Method 1: Use the Sudo Command

Some files and folders are owned by the root and can be accessed only by the root user or with the sudo command. Changing the permissions or ownership of these files is not advisable. However, if needed, they can be modified with superuser permissions. Examples of such files are :

To access such files, we need to use sudo or be a root user as such :

$ whoami
root
$ cat /etc/shadow
< Contents >

Or,

$ whoami 
user
$ id
uid=1000(user) gid=1000(user) groups=1000(user),27(sudo)
$ sudo cat /etc/shadow
< Contents >

Method 2: Setting the Right System Permissions

This is the most common way of fixing this operation. Often, we lack the necessary permissions to access a file. To check our available permissions, we can use the ls command:

$ ls -l

Let’s assume that this results in the following output :

$ ls -l
total 4
-rw------- 1 user user 20 Feb 12 11:36 Test

We can see that only the owner user has read and write permissions. Thus, if another user tries to read this file, they will get an error as such:

$ whoami
user1
$ cat /home/user/Test
cat: /home/user/Test: Permission denied

Hence, to fix this, we would use the chmod command. We can change file permission to grant read access to everyone (the owner, the group members, and others) with the following command :

$ chmod +r Test

Checking the permissions on the file now should yield something like the following :

$ ls -l 
total 4
-rw-r--r-- 1 user user 20 Feb 12 11:36 Test

The same can be done with write and execute permissions. If we want to limit the access to the users of the same group, we can use the following command :

$ chmod g+r Test

This would allow the users in the same group as the owner to access the file, and the permissions would look something like this :

$ ls -l
total 4
-rw-r----- 1 user user 20 Feb 12 11:36 Test

Now, we can finally read the file as the other user.

$ whoami
user1
$ cat /home/user/Test
This Is A Test File

NOTE: If the root user owns the file and our current user is in the sudoer’s group, we can change the file’s permissions by prefixing our commands with sudo. To know more about the usage of the chmod command, check out this module!

Method 3: Change Ownership Of The File

Although less conventional, the last method on our list can help fix the Permission denied error. Instead of changing the file’s permissions, we will change its ownership here. For this, we would need the chown command. First, let us check the ownership of the file using the following:

$ ls -l
total 4
-rw------- 1 user user 20 Feb 12 13:27 Test

As we can see, the file belongs to the user labeled as ‘user‘ and the group by the same name. Also, notice how only the owner has read and write permissions (these can be changed, as discussed previously). However, at present, we cannot access the file. But we’ll change the ownership (and group) of the file with the following:

$ sudo chown user1:mygroup /home/user/Test

If we check the details on our file now, we will find it to be changed to :

-rw------- 1 user1 my-group   20 Feb 12 13:27 Test

As we can see, the permissions on the files are preserved, but the groups that can access the file are changed. Since the owner has read permissions (if that’s not the case, we can always use chmod), we can now read the file’s contents:

$ cat /home/user/Test
This Is A Test File

Conclusion: Fixing the Permission Denied error in Linux

It is important for those who work with Linux to understand and troubleshoot Permission errors on Ubuntu and Debian-based Linux operating systems.

We have discussed three effective methods to address this issue: utilizing the sudo command, adjusting system permissions, and modifying file ownership. By improving these techniques, you can improve your experience working with Linux systems while reducing the likelihood of accidentally exposing sensitive information.

It is advisable to exercise caution when changing permissions and file ownership, and consult the official documentation or contact an expert when uncertain. You can also execute the files once you have set the proper permissions to the file using the methods we have discussed above.

How can I resolve Permission Denied errors on Ubuntu / Debian Linux?

To solve Permission Denied errors on Ubuntu / Debian Linux, you can try troubleshooting the issue by checking the Linux permissions, ensuring the user attempting to access the file or directory has the necessary permissions, and identifying any symbolic links that may be causing the error. It is also important to confirm that there is enough disk space available and that the environment variables are set correctly.

What are some common scenarios leading to Permission Denied errors on Linux servers?

Some common scenarios leading to Permission Denied errors on Linux servers include insufficient user permissions, running a command as a different user, attempting to access protected directories without appropriate permissions, and encountering symbolic links that point to non-existent files or directories.

How can I identify which user is attempting to access the file or directory that is throwing a Permission Denied error?

To identify the user attempting to access the file or directory that is causing the Permission Denied error, you can check the server logs for relevant information. Look for entries that indicate the username or user ID associated with the failed access attempt.

What steps can I take to resolve Permission Denied errors when running a specific command or script?

If you encounter Permission Denied errors when running a specific command or script, you can try creating a new user with the necessary permissions, ensuring the command or script is executable, and confirming that the path to the file is correct.

I am getting a Permission Denied error when trying to access a specific directory. How can I investigate and fix this issue?

When encountering a Permission Denied error while trying to access a specific directory, you can check the permissions of the directory using the ‘ls -l’ command, verify that the user has the required permissions to access the directory, and look for any additional security measures such as SELinux or AppArmor that may be restricting access.

What are some common tools or commands that can help me troubleshoot and solve Permission Denied errors on Ubuntu / Debian Linux?

Some common tools and commands that can be helpful in troubleshooting and solving Permission Denied errors on Ubuntu / Debian Linux include ‘chmod’ to change file permissions, ‘chown’ to change file ownership, ‘sudo’ to run commands with superuser privileges, and ‘ls -l’ to list detailed file permissions.