5

I can't run my Bash script, execution permission is always denied.

I modified my files with command for example:

chmod u+x bash_script1.sh 

and obtained the following after:

-rwxr--r-- 1 user group 1947 Jun 18 16:04 bash_script1.sh
-rwxr--r-- 1 user group 2430 Jun 18 15:59 bash_script2.sh
-rw-r--r-- 1 user group    1 Jun 18 10:57 runs.txt

However, I continue to get the following error when running from command line:

Comand:

./bash_script1.sh

Error:

-sh: ./bash_script1.sh: Permission denied

When I run the following command below, I don't get the error but I don't want to use bash to run my script:

bash bash_script1.sh

#!/usr/bin/env bash is placed at my heading.

Joe_Informatics
  • 53
  • 1
  • 1
  • 4
  • 1
    You only gave execute permission to user - are you actually logged in as user when you try this? what is the output of the id command? what is the exact command that produces the error -sh: ./bash_script1.sh: Permission denied? – steeldriver Jun 18 '21 at 14:19
  • Yes, it is exactly that error that I get when trying to run ./file_script1.sh in command line. The output is as follows: uid=19979(user) gid=867800079(group) groups=867800079(group),11226(research),11765(omics) – Joe_Informatics Jun 18 '21 at 14:23
  • I just gave permission to all users to execute chmod uga+rwx file.sh and it still gives me the permission denied error.... – Joe_Informatics Jun 18 '21 at 14:39
  • 1
    OK so the only other cause I can think of is that the filesystem it's stored on is mounted with the noexec option - check with findmnt -T . (run from the directory that contains the script) – steeldriver Jun 18 '21 at 14:40
  • /home /dev/md126p2 xfs rw,noexec,relatime,attr2,inode64,noquota this is what I get after. How can I change this? or do I have to ask my sysadmin? – Joe_Informatics Jun 18 '21 at 14:54

2 Answers2

8

As we can see from the output of findmnt -T ., the filesystem on which the script is stored is mounted with the noexec option. This prevents scripts (as well as binary executables) from being executed directly, whereas bash bash_script1.sh still works because the bash executable is stored elsewhere and only needs to read the script.

To modify the behavior temporarily you can use the mount command:

sudo mount -o remount,exec /home

To make the change persistent, you will need to modify your /etc/fstab file and remove the noexec mount option for this block device.

steeldriver
  • 143,099
  • this is all definitely new to me, so I will have to look into it deeper for sure. unfortunately I do not have sudo rights thus will have to reach out to my sysadmin for more guidance. Thank you so much at least for the clarification :D – Joe_Informatics Jun 18 '21 at 15:09
  • 1
    @Joe_Informatics you're welcome - tbh I was slightly surprised that the noexec option has this effect on scripts (not only binary executables) - the mount man page just says noexec Do not permit direct execution of any binaries on the mounted filesystem.. But I confirmed the behavior on my own system, and it's discussed in more detail here for example Executing a bash script or a c binary on a file system with noexec option – steeldriver Jun 18 '21 at 15:20
  • yeah this is definitely new to me, but nice to know the root of the problem. Will take a look at url provided :D – Joe_Informatics Jun 21 '21 at 12:17
0

In my case, binaries were executed but no script whatsoever, whether bash or c-shell script The culprit were these mount options in /etc/fstab, for the partition where the scripts are residing:

user,data-ordered

(somehow generated by default during installation).

The remedy has been to replace these options by:

acl,user_xattr

(found on a different machine where everything worked fine - man mount will have the explanation).