Score:0

Bash Script Permission Denied when tying to run

in flag

I am aware that there are similar posts but I have followed what they have done but continue to get the same problem of permission denied when trying to execute my bash script.

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

hr flag
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`?
Joe_Informatics avatar
in flag
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 avatar
in flag
I just gave permission to all users to execute chmod uga+rwx file.sh and it still gives me the permission denied error....
hr flag
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)
Joe_Informatics avatar
in flag
`/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?
Score:4
hr flag

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.

Joe_Informatics avatar
in flag
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
hr flag
@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](https://unix.stackexchange.com/questions/490402/executing-a-bash-script-or-a-c-binary-on-a-file-system-with-noexec-option)
Joe_Informatics avatar
in flag
yeah this is definitely new to me, but nice to know the root of the problem. Will take a look at url provided :D
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.