1

I am running a script as a root that creates a user using useradd and passwd. Then am doing some checks/installs before running some other commands that need to be run as the newly created user.

So I have the main script that creates the user:

main.sh:

#!/bin/bash
useradd testuser
passwd testuser

yum -y install git
chmod 777 testuser.sh
su testuser -c ./testuserscript.sh

testuserscript.sh:

#!/bin/bash
git clone git://github.com/schacon/grit.git

When I run ./main.sh: I get the the following error:

bash: ./testuserscript.sh: Permission denied

When I do ll I get the following:

-rwxrwxrwx. 1 root  root  728 Jun 24 11:02 testuserscript.sh

Am I running the wrong command to run the testscript as the testuser?

Blue Ice
  • 7,888
  • 6
  • 32
  • 52
as3rdaccount
  • 3,711
  • 12
  • 42
  • 62

1 Answers1

0

Permission denied can happen when:

  1. The script doesn't have execute permissions for the user which invokes it
  2. The path after the hash-bang (#!) isn't correct or doesn't point to an executable (or the user doesn't have permissions to execute this script).
  3. The user doesn't have execute permission for all the folders leading to the script.

In your code, you're setting chmod 777 to testuser.sh but then you try to execute testuserscript.sh

I don't expect su to change the folder but maybe try absolute paths. Also check the default shell of the newly created used. To circumvent all this (and a couple of other problems), try:

sudo -u testuser bash /tmp/testuserscript.sh

You can later use variables to define in which folder all the scripts are.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820