Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question

migrated from stackoverflow.com Mar 30 '10 at 13:57

This question came from our site for professional and enthusiast programmers.

add comment

4 Answers

up vote 1 down vote accepted

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?

share|improve this answer
    
you mean to say if i run my script from root, it will work as I like it to run? –  vijay.shad Mar 30 '10 at 12:28
    
What he describes is exactly what is given by the setuid bit. –  Kevin M Mar 30 '10 at 14:58
add comment

If you are root, just use sudo:

$1 sudo -u adminuser startup.sh start
share|improve this answer
    
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 '10 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 '10 at 11:47
    
Above command still asks for the password? :( –  vijay.shad Mar 30 '10 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 '10 at 12:18
add comment

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
}
share|improve this answer
add comment

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.

share|improve this answer
1  
Most Unix implementations do not allow setuid scripts. See for example tuxation.com/setuid-on-shell-scripts.html . –  coneslayer Mar 30 '10 at 14:30
    
You should never have a script that's setuid and many systems won't allow it. –  Dennis Williamson Mar 30 '10 at 14:36
    
Aah yes--setuid is for programs, not scripts. –  Broam Mar 30 '10 at 14:58
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.