Marcin
Marcin

Reputation: 5589

Unable to execute bash scripts even as root?

I have a weird problem, I cant execute bash script even as basic as:

#!/bin/bash
echo "me"

I am saving it as a test.sh and then do chmod 755 test.sh and once run ./test.sh getting:

bash: ./test.sh: Permission denied

Any ideas what could be causing this?

Upvotes: 22

Views: 54309

Answers (9)

sebix
sebix

Reputation: 3239

For filesystems which are mounted with the noexec by default, for example NFS, explicitly adding exec at the end helps, even when options provided earlier in the list default imply noexec as well, e.g. the user option.

So if you have one of those options:

  • noexec
  • user

Change them to:

  • exec or
  • user,exec

It is important to place exec at the end. Just removing noexec may help in certain cases, but not in all, if you are using other options like user before.

Upvotes: -1

Craig Doran
Craig Doran

Reputation: 11

In macOS this can occur if a com.apple.quarantine flag exists. If you see a @ suffix on the permissions after running a ls -l on the script's path, execute ls -l@ *script_path* to confirm. Then run a xattred -d com.apple.quarantine *script_path* to remove the quarantine flag.

Upvotes: 1

Abhinav
Abhinav

Reputation: 329

Script needs be executable. Use this:

chmod +x <script-name>

Upvotes: 21

Lan...
Lan...

Reputation: 103

you need use ./test.sh when you in the directory of that file,if you don't,try PATH TO THE SCRIPT.or you can copy it to some directory of /data and chmod it for shell,then do the above steeps.if you still fail,it's ok because i have a same problem,i just did it success for once time.

Upvotes: 0

jceifrig
jceifrig

Reputation: 11

Also, check to see if the directory/filesystem containing the script is nfs-mounted. root won't run scripts from nfs-mounted locations.

Upvotes: 1

Serem
Serem

Reputation: 21

Use chmod +x ./test.sh this should allow you to run it.

Upvotes: 1

DaveOfTheDogs
DaveOfTheDogs

Reputation: 81

Although not directly pertinent to this particular thread; if a file has come form a Windows system there may be a CR/LF at the end of the line. This would affect all lines in the file, including the initial execution line, and would not be visible if you are viewing the file.

$ ./test.sh 
-bash: ./test.sh: /bin/bash^M: bad interpreter: No such file or directory

To see this, you could cat -A the file: $ cat -A ./test.sh #!/bin/bash^M$ echo "me"^M$

To remove, use dos2unix.

Upvotes: 3

themel
themel

Reputation: 8895

That can happen if you have mounted the file system with the "noexec" option. You should remove it.

Upvotes: 27

Stainedart
Stainedart

Reputation: 1979

Try

ls -la

to see the actual rights and ownership of the file. To see if the chmod command actually worked. You might want to change the ownership along with the mod of the file check : http://www.tuxfiles.org/linuxhelp/fileowner.html

Upvotes: 1

Related Questions