List of content you will read in this article:
In Linux operating system, you cannot execute any command without proper permission. Every file and directory has some permission or privilege (read, write, or execute) associated with them. If you are not authorized to access the file or directory, executing any command on that will result as a “permission denied” error in Linux. This prevalent common can only be resolved by getting the proper access to that file and directory. In this article, we will help you with how to fix the permission denied errors in Linux and what type of error is this with the help of various Linux commands.
What is Linux Permission Denied Error?
This type of error will occur whenever you run a command for which you do not have the execute permission. Similarly, you cannot perform read or write action if you do not have read or write permission for any file or directory. These Linux permissions are the primary reason behind the security of Linux, as they will help in protecting the data from unauthorized access.
Linux system has three types of permissions
1. read permission
2. write permission
3. execute permission
So, if you want to solve a Linux permission denied error, you can check your privileges for the specific file or folder using the following command.
This command will display the long listing of all files and folders along with the permission, as shown below.
As shown below, we have created a shell script “hello.sh” without the execute permission. On executing “hello.sh”, you will get a “permission denied” error.
How To Fix Permission Denied Error in Linux?
For solving this error, you need to add the correct permissions to the file to execute. However, you need to be a “root” user or have sudo access for changing the permission. For changing the permission, Linux offers a chmod command. The chmod stands for change mod. This command has a simple syntax, as shown below.
chmod flags permissions filename
- Flags are the additional options that users can set.
- Permissions can either be read, write or execute in the file. You can represent them in symbolic form (r, w, and x) or octal numbers.
- The Filename specifies the file’s name for changing the permissions.
Representation of permissions
Below is the symbolic and octal representation of the user’s permissions while executing the “chmod” command. First, we will understand the representation before using it.
chmod u=rwx,g=r,o=r file
- r specifies the read permissions
- w specifies the write permissions
- x specifies the execute permissions
- Octal representation-
chmod 744 file
- 4 specifies the read permissions
- 2 specifies the write permissions
- 1 specifies the execute permissions
- 0 means no permissions issued.
How to Solve Bash Permission Denied?
Now, we are aware of the error, as shown below.
Giving the appropriate permission to the user will solve the problem. Thus, we are giving the execute permission to the user to run the “hello.sh” shell script. Execute the below command to provide execute permission.
chmod +x hello.sh
Now, we can see the change in the permission of the “hello.sh” script file. The above command provides the execute permission to the file. As you can see, the root user can make the required changes. If we execute the shell script, we should not get the error. Let’s try by running the below command.
After executing the “hello.sh”, we get the output that displays the “hello.” Changing permission has solved the problem of bash permission denied.
If you are a regular Linux user, you might have faced the “permission denied” error while executing various commands. This might be due to the incorrect privileges to run that command. Only a root user or user with sudo access can change the permissions for the file or directory you want to access or execute. If you are the correct user to make the required permission changes, you can run the “chmod” command and add the desired permission.
This is all about how you can solve/fix permission denied errors in Linux with the help of the above-listed commands/methods. If you think there are other alternatives to achieve the goal, you can put them down via the comment box. Also, if you want to run these above-listed commands with a Linux server, you can buy them from here.
People also read: