Home Terminal Tuts How to fix shell script permission denied error in Linux

How to fix shell script permission denied error in Linux

fix shell script permission denied error in linux

When attempting to run applications in Linux, one of the most typical issues that arise is the “bash:./program name: permission denied error.” This issue happens most frequently when the script you are attempting to run does not have the appropriate execute permissions. This problem may be fixed by either modifying the permissions assigned to the script or assigning the necessary permissions.

Let’s look at the specifics of the error, as well as the possible solutions to it.

If the directory in which you are working does not have the appropriate rights, you may get an error in Linux that says “permission denied” when you try to list the files in the directory or run a shell script inside the directory. Because the Linux operating system prioritizes its users’ safety, the “root” user has unrestricted access to all folders and files, allowing them to make any desired modifications. As a result, it’s possible that other users won’t be able to make those kinds of adjustments.

Fixing the shell script permission denied error in Linux

For illustration purposes, I have a shell script on my system named “fosslinuxscript.sh.” Now, assuming the role of a regular user, we will attempt to run that infernal script by executing the following line of code:

./fosslinuxscript.sh
permission denied error

permission denied error

Note: if you don’t have the file and would like to create one, run the following command on your console:

touch fosslinuxscript.sh
create fosslinuxscript

create fosslinuxscript

This software generates an error because the script “myscript.sh” lacks execute permission. To examine the permissions given to a file, enter the command –l at the command prompt.

ls -l fosslinuxscript.sh
check fosslinuxscript permissions

check fosslinuxscript permissions

The result above indicates that the script has only read and write access but no execute privileges (denoted by x). The file cannot thus be run.

When attempting to list the files in a directory or execute a shell script inside of a directory, Linux may give you an error that reads “permission denied” if the directory in which you are working does not have the proper privileges. The “root” user has full access to all directories and files, enabling them to make any necessary changes. This is possible because the Linux operating system emphasizes the security of its users. Other users might not be able to make those kinds of modifications as a result.

Troubleshooting permission denied error

To overcome this “permission denied” error, just add “x” or “execution” permission to this “fosslinuxscript.sh” file and make it executable.

Check the shell script’s file permissions first.

ls -l fosslinuxscript.sh
check fosslinuxscript permissions

check fosslinuxscript permissions

To modify the settings, check out the next section.

Using the command chmod

The chmod command permits the modification of a file’s permissions using a reference file, numeric mode, or symbolic mode.

Syntax of command chmod:

chmod u+x flags permissions filename
  1. flags: the user has the ability to specify the extra parameters
  2. permissions: This portion of the chmod command is used to establish the permissions for a file. These permissions include “r” for read, “w” for write, and “x” for making it executable.
  3. filename: You will need to give the filename for which you wish to update the permissions.

In contrast, “u+x” makes the script executable for the current Linux user, even if the group owner or other “users” already have permission to run it.

Now to make our script executable, run the command below:

chmod u+x fosslinuxscript.sh
make script executable

make script executable

The “fosslinuxscript.sh” file should be rendered executable upon execution of the preceding chmod command. Execute the “ls” command to verify the modifications made to the shell script’s permissions.

ls -l fosslinuxscript.sh

Employ the cat command to examine the contents of the fosslinuxscript.sh script file.

cat fosslinuxscript.sh
check contents of script

check contents of script

Once you know the contents present in this file, you can proceed and execute the file to check to verify if the files are similar:

./fosslinuxscript.sh
i love fosslinux output

I love fosslinux output

The output indicates that the permission denied error of this “fosslinuxscript.sh” shell script has been successfully resolved.

Conclusion

This article provides a detailed technique for resolving the “permission denied” shell script execution problem. Every Linux user should be familiar with the simple solution for the “permission denied” issue while executing any shell script. The “chmod” tool addresses this problem by modifying the script’s file permissions to make it executable for the current user.

You may also like

1 comment

Anonymouse August 27, 2022 - 4:18 AM

Hi Padwan ..

tzz… chmod ?
man chmod
whatis chmod

ls -al fosslinuxscript.sh
~
sudo chmod 700 fosslinuxscript.sh
~
ls -al fosslinuxscript.sh
….

why “sudo”, because the admin can change the rules of the files
at foreign files..

why Numbers.. because it’s works
knowing is the Force !
not knowing is Magic
you have wrote, you make a howto about permissions, the chmod set up the rules of permissions..

this is a babble/stammering of words and not a howto..

if you make something, then make it right..
and if you wana make right, must you have really known about,
to make it right …

best

Reply

Leave a Comment

fl_logo_v3_footer

FOSS Linux is a leading resource for Linux enthusiasts and professionals alike. With a focus on providing the best Linux tutorials, open-source apps, news, and reviews, FOSS Linux is the go-to source for all things Linux. 


Whether you’re a beginner or an experienced user, FOSS Linux has something for everyone.

Follow Us

Subscribe

©2016-2023 FOSS LINUX

A PART OF VIBRANT LEAF MEDIA COMPANY

ALL RIGHTS RESERVED.

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.