Get your server issues fixed by our experts for a price starting at just 25 USD/Hour. Click here to register and open a ticket with us now!

Author Topic: How to resolve permission denied Linux error  (Read 1194 times)

0 Members and 1 Guest are viewing this topic.

akhilt

  • Guest
How to resolve permission denied Linux error
« on: June 02, 2018, 08:57:45 am »
How to resolve permission denied Linux error


This article will teach you quickly what is permission denied Linux error. And also what ways you can avoid permission denied error in Linux.


What is permission denied Linux error?

This error comes when you try to list files or try to execute the file inside the directory where you don’t have sufficient permission. Since Linux operating system is very particular about its security aspect.

Example of Permission denied Linux error

Let’s say you are a normal user who is trying to list or trying change the directory inside the /root file-system. Since you do not have sufficient permissions system will respond with permission denied error message as below:
Code: [Select]
[root@server ~]# su - akhil
Last login: Thu May 24 14:34:36 UTC 2018 on pts/0

[akhil@server ~]$ ls -l /root
ls: cannot open directory /root: Permission denied

[akhil@server ~]$ cd /root
-bash: cd: /root: Permission denied

[akhil@server ~]$ id
uid=501(akhil) gid=501(akhil) groups=501(akhil)

[akhil@server ~]$

One way to avoid such error is to switch to root user using su - command. However, this solution is not recommended since it will gain unnecessary access to all the root file system.

How to resolve Permission denied Error

Resolving Permission denied error related to script execution:

Let’s say you have created a shell script for performing any task. but when you try to execute the script you may end with below error due absence of permission denied error.

Code: [Select]
[root@server tmp]# ./myshell.sh
-bash: ./myshell.sh: Permission denied

[root@server tmp]#

Now to avoid such case you need to add execute permission “x” to the file myshell.sh using chmod command as below:

Code: [Select]
[root@server tmp]# ls -l myshell.sh
-rw-r--r-- 1 root root 27 Jan 25 00:31 myshell.sh

[root@server tmp]# chmod u+x myshell.sh

[root@server tmp]# ls -l myshell.sh
-rwxr--r-- 1 root root 27 Jan 25 00:31 myshell.sh

[root@server tmp]#

In the last output, you can see that there is “x” (execution) permission added after chmod command. So next time when you try to execute the shell script, it will execute without any error.

Code: [Select]
[root@server tmp]# cat myshell.sh
echo "My name is Akhil"

[root@server tmp]# ./myshell.sh
My name is Akhil

[root@server tmp]#

Resolving permission denied Linux error while listing or writing to a file

In this type of permission denied error you try to list or write the file in which you do not have sufficient permission to do so as below:

Code: [Select]
[akhil@server tmp]$ cd myfolder/
-bash: cd: myfolder/: Permission denied

[akhil@server tmp]$

If you look at the permissions of the myfolder directory using ls -l command you will come to know about the permissions.

Code: [Select]
[root@server tmp]# ls -ltr
total 4
drwx------ 2 root root 4096 Jan 25 00:48 myfolder

[root@server tmp]# pwd
/tmp

[root@server tmp]#

As per the permission given in above output only owner of the directory who is root can have all permission that is read, write and execute.  So in such case you need to change the permission of the directory to read using below chmod command:

Code: [Select]
[root@server tmp]# chmod o+rx myfolder/

[root@server tmp]# ls -lt
total 4
drwx---r-x 2 root root 4096 Jan 25 00:48 myfolder

[root@server tmp]#

Now this time when normal user akhil try to list directory he will not get the permission denied error.

Code: [Select]
[akhil@server tmp]$ ls -lt myfolder/
total 0

[akhil@server tmp]$ cd myfolder/

[akhil@server myfolder]$

In case you want to have write permission on this directory you need to specify w flag as well in chmod command as below:

Code: [Select]
[root@server tmp]# chmod o+rwx myfolder/

[root@server tmp]# ls -lt
total 4
drwx---rwx 2 root root 4096 Jan 25 00:48 myfolder

[root@server tmp]#

Same is applicable to file level permission as well.

One more way is to changing the ownership of the directory using chown command. Since in our example we are getting error for user manmohan we will change ownership of the directory  myfolder using below command.

Code: [Select]
[root@server tmp]# chown akhil:akhil myfolder/

[root@server tmp]# ls -l
total 4
drwx---rwx 2 akhil akhil 4096 Jan 25 00:48 myfolder

[root@server tmp]#

Since akhil user is now the owner of the directory he can able to do any operation on the directory. In case you want to recursive permission do not forget to add -r while chown command as below:

Code: [Select]
[root@server tmp]# chown -R akhil:akhil myfolder/
Resolving permission denied Linux error for specific user

In above method of changing the permission using chmod is not suitable as per my opinion. Because when you give permission to others, it will be open for all the users within the system. Which is wrong in terms of security perspective.  To resolve this error specific to user you can implement it using access control list or ACL.

Linux user access control list (ACL)

User access control

In UNIX system you always need to deal with security towards the system. File or directory level user access control is one of them.
Mainly the purpose of the user access control list is to provide secure access to the flies and directory within the system. There are only two commands to configure the user access control list in Linux system.

1. getfacl which displays the currently configured access to directory or to file.
2. setfacl which actually set the user access control for to directory or to file.

So let’s jump directly into the demo of “user access control list”

Here we are configuring the user access control for the user manmohan in the directory /etc.

1. Let's check first if akhil user has write user access control for “/etc” directory or not.
2. Log in as akhil cd to /etc.
3. Now try to create directory inside /etc and thesystem responds with.

Code: [Select]
[akhil@server etc]$ mkdir test2
mkdir: cannot create directory `test2': Permission denied

4. Now Let’s configure the user access for akhil user.

5. Fire the setfacl command as below:

Code: [Select]
[root@server etc]# setfacl -R -m u:akhil:wrx /etc/test/
-R give the recursive permission under /etc directory . If you don’t want to give you can ignore this option from the command.

-m stands for modification.

U stands for user again for akhil user with permission wrx.

6. Now verify the user access control for list for /etc directory.

Code: [Select]
[root@akhil etc]# getfacl test
# file: test
# owner: root
# group: root
user::rwx
user:manmohan:rwx
group::r-x
mask::rwx
other::r-x

[root@server etc]#

Yes, all properly set.

7. Now let’s create the files under /etc to test the configuration.

Code: [Select]
[akhil@server test]$ pwd
/etc/test

[akhil@server test]$ touch 1

[akhil@server test]$ ls -ltr
total 0
-rw-rw-r--. 1 akhil akhil 0 Jul 21 20:55 1

[akhil@server test]$ mkdir test2

[akhil@test test]$ cd test2

[akhil@test test2]$ touch test2

[akhil@test test2]$ pwd
/etc/test/test2

[akhil@server test2]$

8. For multiple user fire below command.

Code: [Select]
setfacl -m user:akhil:wrx,u:rahul:rw /etc/test/
9. For removing all permissions from the user with user rahul:

Code: [Select]
setfacl -x user:rahul /etc/test/
Hope the above informations were helpful :D!!
THANK YOU :) :)