Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I have a web application that runs a program which needs X. I'm using xvfb to launch it; I want to run it as another user.

I could probably do sudo -u username -p password my command. However, I'm not feeling too good about storing the users password in plain text.

Is there a "smarter" way of doing this?

share|improve this question

4 Answers 4

up vote 4 down vote accepted

Use suEXEC which allows a script to be run as the owner of the file, rather than the webserver user.

share|improve this answer
    
This is what I ended up using. Not really what I wanted though, but close enough. –  vise Jun 19 '10 at 21:16

The way I've seen it done is to give the Apache user sudo privilege for just that one very specific command (apache2ctrl restart in our case). You don't need to use passwords, if you're storing it in plaintext somewhere that Apache user can read then you may as well not have one.

Unless of course you need to run multiple different commands through Sudo as the Apache user. That could get awkward if you're having to add sudo privileges for each of the commands.

share|improve this answer

If you need to do this outside of Apache at any point, here is a more general solution.

To allow one user to repeatedly run a specific command as another user you could create a shell script owned by that user with the setuid bit enabled that simply runs the command you need. Make sure you only make the script executable by trusted users by setting the group ownership to the apache group (make sure no other users are in this group) and disabling execute permission for all others besides the apache group and your user.

An example:

# Replace $MYCOMMAND, $USERNAME, $GROUPNAME, and $MYSCRIPTNAME below
# In this case $GROUPNAME should probably be apache
su
touch $MYSCRIPTNAME
chown $USERNAME:$GROUPNAME $MYSCRIPTNAME
chmod 4750 $MYSCRIPTNAME # setuid, read/execute for group, none for others
echo '#!/bin/sh' >> $MYSCRIPTNAME
echo "sudo -u $USERNAME $MYCOMMAND" >> $MYSCRIPTNAME
share|improve this answer

A bit late to answer now, perhaps, but I feel that the NOPASSWD feature of sudo need to be mentioned.

For instance, in /etc/sudoers (assuming apache is the user running the web server):

User_Alias WWW = apache

WWW ALL =(username) NOPASSWD: my_command

And invoke sudo -u username my_command. No need to store the password, and you only allow the web server to execute my_command as username.

The user username can still keep its password.

share|improve this answer

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.