6

I have created a shell script to start a server program.

startup.sh  start

When above command will executes, it will try starts the server as adminuser. To achieve this my script has written like this.

SUBIT="su - adminuser -c "
SERVER_BOX_COMMAND_A="Server"

##############
# Function to start cluster

function start(){

   $SUBIT "$SERVER_BOX_COMMAND_A"

}

When i execute the command it asks for password. Is there any other way to do this so, it will not ask for password.

I have seen this behavior in Jboss startup script provided on jboss. That script changes the user to jboss and then starts the jboss server. I wanted my script to behave same way.

0

4 Answers 4

1

Unless I am missing something what you ask is not possible.

The jboss script is designed to

  1. Start as root
  2. Switch to jboss user which has less privilages

But what you ask is

  1. Start as a non-privilaged user
  2. Switch to admin/root which has more privilages.

This is why you will always need a password no matter the command you try.

Is there any specific reason that your server needs elevated privilages?

2
  • you mean to say if i run my script from root, it will work as I like it to run?
    – vijay.shad
    Mar 30, 2010 at 12:28
  • What he describes is exactly what is given by the setuid bit.
    – Kevin M
    Mar 30, 2010 at 14:58
3

If you are root, just use sudo:

$1 sudo -u adminuser startup.sh start
4
  • Hi unwind, Can you please describe the command? I think it will su to adminuser and then call the script. Am i right?
    – vijay.shad
    Mar 30, 2010 at 11:41
  • sudo, not su (though you can use su too, as su -c, but these are different commands with different purposes).
    – wRAR
    Mar 30, 2010 at 11:47
  • Above command still asks for the password? :(
    – vijay.shad
    Mar 30, 2010 at 12:02
  • @vijay.shad: The man page I linked (click the command name in my answer) provides plenty of explanation. You really shouldn't be getting a password prompt if you're running sudo as root. Verify that you are.
    – unwind
    Mar 30, 2010 at 12:18
1

Like JBoss, you could check to see if you are the target user before running your command.

TARGET_USER='adminuser'
SUBIT="su - adminuser -c "
SERVER_BOX_COMMAND_A="Server"

##############
# Function to start cluster

function start(){

   if [[ `whoami` == "$TARGET_USER" ]]
   then
      $SERVER_BOX_COMMAND_A
   else
      $SUBIT "$SERVER_BOX_COMMAND_A"
   fi
}
0

You could always use the setuid bit. Make the owner of the file the user you want to run the script as and then make your script setuid.

Note that this can be a security risk so make sure that user has only the permissions needed. After all, an unprivileged user can run that script, right?

EDIT: Don't make scripts setuid. Your jboss example is a program that drops privileges, not a script.

3

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .