As a user of Ubuntu or Debian-based Linux distributions, you may have encountered the frustrating “Permission Denied” error while accessing specific files or directories. This error is typically caused by inadequate 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/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 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 :
- sudoers file
- systemwide config files
To access such files, we need to use sudo or be a root user as such :
$ whoami root $ cat /etc/shadow < Contents >
$ 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 provide 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
It is important for those who work with Linux to understand and resolve Permission Denied errors on Ubuntu and Debian-based 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. By developing these skills, you can boost your proficiency with Linux, confidently handle Permission Denied errors, and maintain a secure and efficient working environment.