Files on an operating system usually have permissions, making the file accessible to a limited range of people. For instance, some files on a computer are accessible only to administrators; other files are only accessible to a particular user.
If you try to run a file to which you have no access on a Linux computer, you will see a permission denied
error. In this guide, we’re going to talk about the cause of this error and how you can solve the error. Let’s begin.
Bash permission denied
The Bash permission denied
error happens when you try to run a file which you do not have permission to run. This may happen if a file can only be executed by a particular user or a group of which you are not a member.
On a Linux operating system, there are three types of permissions:
- Read
- Write
- Execute
You can have permission to read and write a file without having execution privileges. Thus, if you encounter a Bash permission denied
be sure to check whether you are allowed to run the file. You can check if you have permissions over a file by using the following command:
ls -la
This command will give you information about file permissions. We discuss the output of this command in our The Solution
section later in the article. Let’s look at an example scenario featuring the permission denied
error, with a corresponding solution.
An Example Scenario
We have a file called example.sh. We can see this file by running the ls command. The ls command returns the following:
example.sh example2.sh
We want to run our example.sh file. To do so, we can use the ./ notation:
./example.sh
This command lets us run the example.sh file which is present in our ./ directory (the directory we are presently viewing). Let’s see what happens when we try to run the file:
bash: ./test.sh: Permission denied
Our command returns an error.
The Solution
Our Bash shell is telling us that we do not have permissions to run our file. We can check what permissions we have by running the ls -la command:
-rw-r----- 1 james james 13 Feb 16 12:48 example.sh -rw-r----- 1 james james 13 Feb 16 12:48 example1.sh
We do not have execution privileges over any of our files. If there were an x
after the rw
in the first entry of the output above, we would know we can execute our file. The three characters after the first one represent read, write, and execute privileges for a user. Our group also does not have write or execute permissions.
To solve this issue, we need to give ourselves execution privileges:
chmod u+x example.sh
This command gives our user execution (“x”) privileges over the example.sh file.
We can only run this command if we are allowed to change the privileges of the file. If this file was protected (owned by root, for example), then we would not be able to change this file.
The file is owned by the james
system user so I can alter the file permissions on my james
account. I could also use sudo
to alter the file privileges, although this is not necessary because my user account has the necessary access.
Conclusion
The Bash permission denied
error indicates you are trying to execute a file which you do not have permission to run. To fix this issue, use the chmod u+x command to give yourself permissions. If you cannot use this command, you may need to contact your system administrator to get access to a file.
Do you want to learn more about Bash? Check out our How to Learn the Command Line guide. This guide comes with top tips on how to learn Bash. You will also find a list of resources to help you accelerate your learning journey.
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.