
Tengo un servidor web que también reproduce radio por Internet. Como usuario de www-data, quiero ejecutar algunos comandos, por ejemplo, hice esto en/etc/sudoersarchivo:
www-data ALL=(ALL) NOPASSWD: /usr/bin/amixer
Y desde PHP puedo manipular el volumen sin usar contraseña mediante:
exec('sudo -u user amixer set Master 3%-');
Y:
exec('sudo -u user amixer set Master 3%+');
Pero ahora quiero poder reiniciar mi propio servicio ejecutando el comando:
exec('sudo -u user service servicename restart');
Entonces lo intenté:
www-data ALL=(ALL) NOPASSWD: /usr/bin/amixer, NOPASSWD: /bin/service
Y esto:
www-data ALL=(ALL) NOPASSWD: /usr/bin/amixer, /bin/service
E incluso esto:
www-data ALL=(ALL) NOPASSWD: /usr/bin/amixer
www-data ALL=(ALL) NOPASSWD: /bin/service
Pero ninguno de ellos parece estar funcionando. Por favor, ayúdame.
Lo siento chicos, mi error. Hice algunos cambios, intenté vincular el formulario /sbin a /bin
Ahora lo he cambiado a:
www-data ALL=(ALL) NOPASSWD: /usr/bin/amixer, NOPASSWD: /usr/sbin/service
¡Y funciona! ¡Gracias! Tema cerrado.
Respuesta1
Cuidado con tu solución: ¡puedes iniciar, detener o reiniciar cualquier servicio de esa manera!
Mejor cree un script de shell que ejecute este comando:
echo "#!/bin/sh' > /usr/bin/amixer_restart
echo "sudo -u user service amixer restart' >> /usr/bin/amixer_restart
Otorgue los permisos adecuados (550 significa que la raíz y el grupo www-data pueden leer y ejecutar, nadie puede escribir)
sudo chown root:www-data /usr/bin/amixer_restart
sudo chmod 550 /usr/bin/amixer_restart
Y permita que Apache haga sudo en este script:
www-data ALL=(ALL) NOPASSWD: /usr/bin/amixer_restart
Respuesta2
Esto es lo que terminé haciendo:
- Instale apache2 ejecutando
sudo apt-get install apache2
- Asegúrese de que Apache tenga permiso para ejecutar scripts cgi ejecutando
sudo a2enmod cgi
- reiniciar apache
sudo service apache2 restart
Verifique que puedo ejecutar scripts bash creando el siguiente script en
/usr/lib/cgi-bin/test.sh
#!/bin/bash # get today's date OUTPUT="$(date)" USR=$(whoami) # headers echo "Content-type: text/plain" echo "" # body echo "Today is $OUTPUT" echo "Current user is $USR"
hacer que el script sea ejecutable
chmod +x /usr/lib/cgi-bin/test.sh
Verifico que puedo ejecutar el script.
curl localhost/cgi-bin/test.sh
Recibo la siguiente respuesta:Today is Wed Sep 6 12:19:34 PDT 2017 Current user is www-data
Como el usuario es www-data, agrego ese usuario como sudoer. Luego modifico el archivo
/etc/sudoers
agregando esta línea al final:www-data ALL=(ALL) NOPASSWD: ALL
Ahora se supone que ese usuario tiene privilegios de root. Luego modifico mi script test.sh como:
#!/bin/bash # get today's date OUTPUT="$(date)" USR=$(sudo whoami)
Entonces debería ver la siguiente respuesta al ejecutar una solicitud de obtención nuevamente
localhost/cgi-bin/test.sh
:Today is Wed Sep 6 12:28:38 PDT 2017 Current user is root