When working with Linux, one often run into a
Permission denied error. This is probably the most common error one runs into when starting on Linux. In this module we would learn how to fix this error in multiple ways.
Steps to fix the permission denied error on Ubuntu/Debian Linux
Let’s go over the steps to fix the permission denied error on our Ubuntu or Debian based systems here.
Method 1: Use the Sudo Command
There are some files that are owned by root and can be accessed only by the root user or can be accessed with the sudo command. It is not advisable to change the permissions or the ownership of these files. 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
Here 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 that of 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 file is owned by the root user and our current user is in the sudoer’s group then we can ourselves change the permissions of the file 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
The last method on our list, albeit less conventional, can help fix the
Permission denied error. Instead of changing the permissions of the file, here we will change its ownership. For this, we would need the chown command. First let us check the ownership of the file using:
$ 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 also 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 :
$ sudo chown user1:mygroup /home/user/Test
If we check the details on our file now, we would 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
Here, we saw how to solve issues with permissions. The methods described here each have their own applicability depending on the scenarios and each must be used carefully as required.