Executing a script as another user still cause some permission trouble
From terminal, using sudo visudo
command, edit sudoers
file, and add the following line exactly as it is:
www-data ALL=(ALL) NOPASSWD: /home/user2/bin/test.sh
See https://help.ubuntu.com/community/RootSudo#Allowing_other_users_to_run_sudo in this sense.
Then, in php use only sudo /home/user2/bin/test.sh
command. For example:
<?php shell_exec('sudo /home/user2/bin/test.sh'); ?>
Related videos on Youtube
Babajaga
Updated on September 18, 2022Comments
-
Babajaga over 1 year
I have to execute this script with PHP. The user
www-data
can't do it, so I modified thesudoers
file:www-data ALL=(user2) NOPASSWD: /home/user2/bin/test.sh
The script is executed in php with this command :
sudo -u user2 /home/user2/bin/test.sh
Here is
test.sh
script:#!/bin/bash #Stopping previous server screen -S minecraft -X stuff "stop^M" #deleting the map file rm -Rf /home/minecraft/Serveur/*/ # deleting the properties file rm -f /home/minecraft/Serveur/server.properties #new server properties cp /home/minecraft/MapsEtServ.prop/Bloody/server.properties /home/minecraft/Serveur/server.properties #new map cp -R /home/minecraft/MapsEtServ.prop/Bloody/'Bloody Mess v1.0' /home/minecraft/Serveur/'Bloody Mess v1.0' #launching the game server screen -dmS minecraft java -Xmx4096M -Xms4096M -jar /home/minecraft/Serveur/minecraft_server.jar nogui
/home/minecraft/Serveur
and/home/minecraft/MapsEtServ.prop
permissions are set to 777.What happens when I execute the script via SSH: everything works fine.
But when I execute it with php: the screen is stopped, the files are paste and the screen is launched. But there is a problem: the game server is not running.
EDIT : The Probleme is solved ! I just added a cd /home/minecraft/Serveur/ at the beginning of the script and everything works just fine :)
-
Babajaga over 10 yearsI just edited it, line 3 it is -Rf not -rf. I works, just tested and I created it by myself.
-
Babajaga over 10 yearsno they are not :) I just add them here to clarify my question. i don't get what you mean by where it work.
-
Radu Rădeanu over 10 yearsIn php, what is the result of this line:
<?php echo shell_exec('whoami'); ?>
-
Babajaga over 10 yearsthe result is www-data. If i execute the script without changing user, I can't acces the screen because it is owned by www-data. Now, the other user is the owner of the screen.
-
Babajaga over 10 yearsSo I modified visudo sorry, my bad
-
-
Babajaga over 10 yearsI did exactly what you said. This is really weird, the server started, the map folder and the properties were cp, but the server completly ignored those two and he generated another map as if there where no map folder in the same folder ...
-
Radu Rădeanu over 10 years@Babajaga In php, what is the result of this line: <?php echo shell_exec('echo $HOME'); ?>
-
Babajaga over 10 yearsNothing, its shows nothing.
-
Babajaga over 10 yearsI can't change the script. I have a lots of other files in the /home/minecraft/Serveur folder
-
Radu Rădeanu over 10 years@Babajaga Ok, don't change it. Then set a home directory for
www-data
user. To do this, edit/etc/passwd
file (withsudo gedit /etc/passwd
for example), findwww-data
user and add/home/minecraft
directory for him. Something like:www-data:x:1002:1002:www-data:/home/minecraft:/bin/sh
. Add only the directory name, nothing else!