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:
[root@rhel ~]# su - manmohan Last login: Wed Jan 24 14:34:36 UTC 2018 on 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.
[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:
[root@rhel tmp]# ls -l myshell.sh -rw-r--r-- 1 root root 27 Jan 25 00:31 myshell.sh [root@rhel tmp]# chmod u+x myshell.sh [root@rhel tmp]# ls -l myshell.sh -rwxr--r-- 1 root root 27 Jan 25 00:31 myshell.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.
[root@rhel tmp]# cat myshell.sh echo "My name is Manmohan" [root@rhel tmp]# ./myshell.sh My name is Manmohan [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:
[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.
[root@rhel tmp]# ls -ltr total 4 drwx------ 2 root root 4096 Jan 25 00:48 myfolder [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:
[root@rhel tmp]# chmod o+rx myfolder/ [root@rhel tmp]# ls -lt total 4 drwx---r-x 2 root root 4096 Jan 25 00:48 myfolder [root@rhel tmp]#
Now this time when normal user manmohan try to list directory he will not get the permission denied error.
[manmohan@rhel tmp]$ ls -lt myfolder/ total 0 [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:
[root@rhel tmp]# chmod o+rwx myfolder/ [root@rhel tmp]# ls -lt total 4 drwx---rwx 2 root root 4096 Jan 25 00:48 myfolder [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.
[root@rhel tmp]# chown manmohan:manmohan myfolder/ [root@rhel tmp]# ls -l total 4 drwx---rwx 2 manmohan manmohan 4096 Jan 25 00:48 myfolder [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:
[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.
Download Free book
Get your free copy of Linux command line Cheat Sheet!!!!
Leave a Reply