Jump to content

root commands as user


Recommended Posts

Hey - I read somewhere that you can do this:

su -c "guarddog"

and guarddog will be run as root. Assuming I remembered that correctly, is it possible to have

su -c "sh myscript.sh"

to have all the commands in the script executed as root?

 

TIA

Link to comment
Share on other sites

Worth remembering is that <myscript> will pass its inheritance to anything else it starts. So if the scruipt is owned by (say apache) and someone added a line the script would also pass root to the line ...

 

It's like setting a suid bit. Or running a rsh ...

 

Not actually important (in this case) , just a security comment...

Link to comment
Share on other sites

OK then New (related) question:

 

How can I make a "function" in the script so that I can run sh myscript.sh as user and it'll mosey along until it calles a different part of the script meant for root, then whether or not it was successful do something else (and you'd know if it was successful based on what it returned, hence why I said function and not procedure)

 

For Example, an install script that would look like this

 

Pseudo!!



mkdir -a ~/.q3a/baseq3 #was is -a to create the necessary parent dirs?

cp /mnt/cdrom/games/quake3/*.cfg ~/.q3a/baseq3



if [[ `call rootstuff` ]] is successful; then

  echo Everything Worked! Press ESC to exit or any other key to play!

  if ESC pressed

       exit

  else

       quake3



#rootstuff:

#as root do this

mkdir -a /usr/local/games/quake3/baseq3 #was it a ??

sh /mnt/cdrom/games/linuxq3a-1.32.x86.run

ln -s /the_void/games/Quake III Arena/baseq3/* /usr/local/games/quake3/baseq3/

return whether or not successful

Link to comment
Share on other sites

Not tested, but probably this should be OK:

 

rootstuff () {

         mkdir -a /usr/local/games/quake3/baseq3 &&

         sh /mnt/cdrom/games/linuxq3a-1.32.x86.run &&

         ln -s /the_void/games/Quake III Arena/baseq3/* /usr/local/games/quake3/baseq3/ ||

         return 1 # probably this part won't be necessary since if one of the above

                  # commands breaks the return status will be >0

}



if su -c rootstuff #here you'll have to enter root's passwd

then

         echo Everything Worked! Press ESC to exit or any other key to play!

         ...

fi

Link to comment
Share on other sites

My example won't work. I wrote it w/o thinking too much. I guess that I was expecting that "su -c rootstuff" would have expanded the function before starting the subshell <--- stupid of me. Obviously that is not true since su will first start a root shell and then it will execute the commands.

 

This other example works fine:

 

rootstuff () {

    su -c '

            mkdir -a /usr/local/games/quake3/baseq3 &&

            sh /mnt/cdrom/games/linuxq3a-1.32.x86.run &&

            ln -s /the_void/games/Quake III Arena/baseq3/* /usr/local/games/quake3/baseq3/ 

    '

}



if rootstuff 

then 

    echo 'Everything Worked! Press ESC to exit or any other key to play!'

    ...

else

    echo 'Something went wrong!!!'

fi

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...