Monday, January 29, 2018

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 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:

1

2

3

4

5

6

7

8

9

[root@rhel~]# su - manmohan

Last login:Wed Jan2414:34:36UTC2018on pts/0

[manmohan@rhel~]$ls-l/root

ls:cannot open directory/root:Permission denied

[manmohan@rhel~]$cd/root

-bash:cd:/root:Permission denied

[manmohan@rhel~]$id

uid=501(manmohan)gid=501(manmohan)groups=501(manmohan)

[manmohan@rhel~]$

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.

1

2

3

[root@rhel tmp]# ./myshell.sh

-bash:./myshell.sh:Permission denied

[root@rhel tmp]#

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

1

2

3

4

5

6

[root@rhel tmp]# ls -l myshell.sh

-rw-r--r--1root root27Jan2500:31myshell.sh

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

[root@rhel tmp]# ls -l myshell.sh

-rwxr--r--1root root27Jan2500:31myshell.sh

[root@rhel 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.

1

2

3

4

5

[root@rhel tmp]# cat myshell.sh

echo"My name is Manmohan"

[root@rhel tmp]# ./myshell.sh

My name isManmohan

[root@rhel 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:

1

2

3

[manmohan@rhel tmp]$cd myfolder/

-bash:cd:myfolder/:Permission denied

[manmohan@rhel tmp]$

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

1

2

3

4

5

6

[root@rhel tmp]# ls -ltr

total4

drwx------2root root4096Jan2500:48myfolder

[root@rhel tmp]# pwd

/tmp

[root@rhel 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:

1

2

3

4

5

[root@rhel tmp]# chmod o+rx myfolder/

[root@rhel tmp]# ls -lt

total4

drwx---r-x2root root4096Jan2500:48myfolder

[root@rhel tmp]#

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

1

2

3

4

[manmohan@rhel tmp]$ls-lt myfolder/

total0

[manmohan@rhel tmp]$cd myfolder/

[manmohan@rhel 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:

1

2

3

4

5

[root@rhel tmp]# chmod o+rwx myfolder/

[root@rhel tmp]# ls -lt

total4

drwx---rwx2root root4096Jan2500:48myfolder

[root@rhel 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.

1

2

3

4

5

[root@rhel tmp]# chown manmohan:manmohan myfolder/

[root@rhel tmp]# ls -l

total4

drwx---rwx2manmohan manmohan4096Jan2500:48myfolder

[root@rhel tmp]#

Since
manmohan 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:

1

[root@rhel tmp]# chown -R manmohan:manmohan 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. Follow my article on Access control list ACL for the same.