While running your codes on the Linux terminal, you may encounter the “bash: ./program_name: permission denied”, in short, the permission denied error.
This tutorial explains the Permission denied error, the reason behind getting such errors, and how to fix shell script permission denied error in Linux.
The shell script permission denied error occurs when the shell script you’re trying to run doesn’t have the permissions to execute. Linux tells you about the problem by showing bash: ./program_name: permission denied on your Linux terminal.
Linux and other such OSs are very much concerned about its’ security. Only the users with “Sudo” privileges or “root” has full access to all of the files and directories for making required changes. It doesn’t allow a normal user to make changes. So, the execution of a script stops.
Now, let’s consider this example given below.
./samplescript.sh
In the above example, the “samplescript.sh” is a shell script. And we tried to execute this shell script.
bash: ./samplescript.sh: Permission denied
This is the script execution output returned by Linux. It shows the user can’t execute the script as there’s the “permission denied error”.
To fix the permission denied error in Linux, one needs to change the file permission of the script. Use the “chmod” (change mode) command for this purpose. But before that, check the file permission.
For checking the file permission, run the following command.
ls -l samplescript.sh
Now, we’ll discuss the solution using “chmod”.
The “chmod” command allows a normal user to change the file permission with the help of a numeric, reference file, or symbolic mode.
Syntax of chmod is:
chmod flags permissions filename
As you can see, the chmod command is made with three segments, such as:
For instance,
chmod u=rwx,g=r,o=r file
In the above example, “x” gives execute permissions, “w” is for the write, and “r” gives read permission.
Now, let’s change the permissions of the file “samplescript.sh” using the command below.
Method 1
Type the command given below.
chmod u+x samplescript.sh
In the command above, “u+x” makes the shell script executable for the file’s owner. This chmod command execution turns the “samplescript.sh” into the desired executable format. The access mode is changed to execute (as “x” denotes execute).
Method 2
This is another way of giving file permission. So, type the following command.
sudo chmod +x samplescript.sh
Here, the chmod command gives execute permission to anybody because no reference is specified.
Chmod references include:
After this, execute the command below:
ls -l samplescript.sh
The “ls” command above confirms the modifications made through the permissions of the shell script. Then, use the following command.
cat samplescript.sh
This “cat” command enables you to view the script file’s content.
And finally, you’re good to go with the shell script execution. So, type the command below in your Linux terminal.
./samplescript.sh
If your script runs, then congrats! You’ve succeeded in fixing the shell script permission denied error.
This is the conclusion of today’s tutorial on how to fix shell script permission denied error in Linux. As a Linux user, the quick fix of permission denied error will be of immense help for you because now you've learned how to resolve "bash: ./program_name: permission denied".