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 a webserver that also plays internet radio. As www-data user I want to run some commands, for example I've made this in /etc/sudoers file:

www-data        ALL=(ALL) NOPASSWD: /usr/bin/amixer

And form PHP I can manipulate volume without using password by:

exec('sudo -u user amixer set Master 3%-');

And:

exec('sudo -u user amixer set Master 3%+');

But now I want to be able to restart my own service by runing command:

exec('sudo -u user service servicename restart');

So I tried:

www-data        ALL=(ALL) NOPASSWD: /usr/bin/amixer, NOPASSWD: /bin/service

And this:

www-data        ALL=(ALL) NOPASSWD: /usr/bin/amixer, /bin/service

And even this:

www-data        ALL=(ALL) NOPASSWD: /usr/bin/amixer
www-data        ALL=(ALL) NOPASSWD: /bin/service

But none of them seems to be working. Please help me out.


Sorry guys - my mistake. I've done some changes, tried to link form /sbin to /bin

Now I have changed it to:

www-data        ALL=(ALL) NOPASSWD: /usr/bin/amixer, NOPASSWD: /usr/sbin/service

And it works! Thanks! Topic closed.

share|improve this question

migrated from stackoverflow.com Aug 22 '12 at 16:59

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

1  
service servicename restart doesn't seem to be the same as /bin/service is guess. you could try to put the restart call in a shell script and then allow sudo to call that script. –  twall Aug 22 '12 at 11:32
    
You can find where binaries are using the which command in a shell. In this case which service will tell you where it is - at a guess it'll be in /sbin rather than /bin. –  Mathew Hall Aug 22 '12 at 11:39
    
@Peter if the question is closed, you should click "close" below the question… –  feeela Aug 22 '12 at 12:33
    
Feeela thanks but I see only "share", "edit", "delete" and "flag". Can't see "close" button –  Peter Aug 22 '12 at 12:55
    
@Feela: close votes on own questions require 250+ reputation. –  Jürgen Thelen Aug 22 '12 at 16:58
add comment

1 Answer

Careful with your solution: you can start, stop or restart any service that way!

Better create a shell script that runs this command:

echo "#!/bin/sh' > /usr/bin/amixer_restart
echo "sudo -u user service amixer restart' >> /usr/bin/amixer_restart

Grant adequate permissions (550 mean root and group www-data can read and execute, nobody can write)

sudo chown root:www-data /usr/bin/amixer_restart
sudo chmod 550 /usr/bin/amixer_restart

And allow apache to sudo on this script:

www-data        ALL=(ALL) NOPASSWD: /usr/bin/amixer_restart
share|improve this answer
add comment

Your Answer

 
discard

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