Okay so I want to change the file mode of a directory to 777 so I use the line
exec('chmod -R 777' . $dir);
where $dir is the directory path of the directory I wanna change
it doesn't seem to work but I don't get an error for it, also if the user I was executing the script from was a sudo user so I have to enter the password after I enter the command, how would I do this? Would it be something like exec('chmod -R 777' . $dir\n 'password'); ?
Information I get from this site
Execute system commands via PHP
Many a times we need to execute system commands on a Linux system – to delete a directory, or restart a service. However, since Apache does not run with root privileges, it is nearly impossible to use PHP’s exec(), system() or passthru() functions to achieve that.
The solution to this is very simple, specially on Ubuntu. The Apache’s user www-data need to be granted privileges to execute certain applications using sudo.
1.Run the command sudo visudo
2.At the end of the file, add the following
www-data ALL=NOPASSWD: /sbin/iptables, /usr/bin/du
This is assuming that you wish to run iptables and du using super user (root) privileges. However, if you wish to run every application using super user privileges, then add the following instead of what’s above
www-data ALL=NOPASSWD: ALL
3.That’s it, now use exec() in the following manner inside your .php script
exec ("sudo iptables -P FORWARD ACCEPT");
Related
Can I write a shell script which anyone can execute which INTERNALLY switches user (or elevates to root) and then runs those commands without exposing the executing user to root privileges?
Some background: I have a shell script which gets a file from my local network (using smbclient) and then converts and treats said file to a csv ready to import into my MYSQL DB. I can run the file as myself but when I try and execute it thru in PHP exec() or shell_exec() I get permission issues because it's user is www-data.
I've spend a day tring various file permision changes, apending the path environment and even reading up on granting www-data sudo rights (without success) but, rather than all that can I create a file anyone (www-data) can execute and safety perform the commands within (and only the commands within) as my user or sudo?
you can do this by editing /etc/sudoers
sudoedit /etc/sudoers
add this line
Cmnd_Alias YOUR_CMD_ALIAS= /path/yourShellScript
[User's name] ALL=(ALL) NOPASSWD: YOUR_CMD_ALIAS
then execute your code with sudo
sudo /path/yourShellScript
I need to run a cPanel script from the command line via PHP's exec function (other cPanel options are not viable). The command is roughly:
uapi --user=[user] Email suspend_incoming email=[user]%40[domain].[tld]
I was getting the following error:
setuids failed: Attempting to setuid as a normal user with RUID ***
That in turn led me to realize that the user under which PHP runs does not have permission to execute that command which lead me to this thread. However just as you're not supposed to CHMOD files blatantly as 777 I don't want to blindly enable all commands for this user when using sudo from the command line or PHP's exec script.
How do I only allow the uapi command (also multiple specific commands) to be executed by this user when using sudo via an edit to the sudoers file?
You can give a user access to execute a command as root via an entry to sudoers file like so (assuming www-data is the PHP process owner user):
www-data ALL=(root) NOPASSWD: /path/to/uapi
Obtain the path to the uapi file via the command which uapi. You should carefully consider the security implications of this action.
This UNIX stackexchange question has a nice detailed explanation of the sudo entry syntax.
I building one PHP application where I create command line functionality for Linux debian Jessie. All works fne but I need to be able use some commands like root user.
Is there a way to use shell_exec() or similar command to access like root user via PHP?
Idea of this command line is to people who have access to that server can handle with it over internet from any place or device.
Here is image of console:
Executing commands as root via PHP will leave yourself wide open to all sorts of malicious hackery.
Have a look at the "sudo" documentation.
You should be able to set up all the commands you need as "sudo"able scripts. It is much better to write specific scripts with limited functions than to expose the underlying priviledged command.
As in:
exec ('sudo getCurrentUser.sh')
First, you need to add the user that PHP is using to run (most of the time it is www-data) to the sudo group if it is not already assigned.
Then, in your php file:
use sudo -S, so you can pass the password via echo
$exec = "echo your_passwd | /usr/bin/sudo -S your command";
exec($exec,$out,$rcode);
if you have trouble with the paths - use
"bash -lc 'echo your_passwd | /usr/bin/sudo -S your command'"
so you get a new bash that acts like a login shell and has the paths set
Edit your sudoers file
sudo vi /etc/sudoers
Put this line
www-data ALL=(ALL) NOPASSWD: ALL
www-data is the php default user in linux ( replace if necessary )
Use
$output = shell_exec('sudo XXXX');
I have set of linux system commands that requires to be logged into shell first.
How can i do that in php ?
For example:
$output2 = shell_exec('ls -lrt /opt/test 2>&1');
Since /opt/test can be accessed by user/owner XXX only, I need to login with that username.
runuser command could be helpful but i need directions.
Actually I need to run an application via system command but before that user must be logged in to have access to that application.
One way to do it would be to create a shell script containing all of the commands that you need to run as the privileged logged in user, and then run that shell script using sudo.
You will need to change your sudoers file so that the execution of the script can be run by the user that the script is running as.
eg:
Cmnd_Alias HTTP_COMMANDS = /usr/local/my_ls_script
XXXX ALL=(ALL) NOPASSWD: HTTP_COMMANDS
Be careful when doing this though! Make sure that the sudoers line can only run the commands that you specify.
Also, make sure that you switch to the user at least once, and run sudo, otherwise, the webserver will not be able to run sudo for the user.
Additionally, you might need to disable NO_TTY in the sudoers file.
You would change your command to:
$output2 = shell_exec('sudo -u XXXX /usr/local/my_ls_script 2>&1');
I have a script in /var/www/myscript.sh which creates folders and runs the command svn update for my projects. I need to execute this script by calling it in a PHP file in the browser (i.e. Localhost/test.php). I tried using functions shell_exec() and exec() but those did not work. I ran my shell script in terminal with su www-data && ./myscript.sh and it worked. What else am I missing?
<?php
$output = shell_exec("./myscript.sh");
?>
Update 5/4/2011:
I added www-data ALL=(ALL) NOPASSWD:ALL to /etc/sudoers and it works, but this is very insecure. Is there another way to do this?
Several possibilities:
You have safe mode enabled. That way, only exec() is working, and then only on executables in safe_mode_exec_dir
exec and shell_exec are disabled in php.ini
The path to the executable is wrong. If the script is in the same directory as the php file, try exec(dirname(__FILE__) . '/myscript.sh');
You might have disabled the exec privileges, most of the LAMP packages have those disabled. Check your php.ini for this line:
disable_functions = exec
And remove the exec, shell_exec entries if there are there.
Good Luck!
Residuum did provide a correct answer to how you should get shell exec to find your script, but in regards to security, there are a couple of points.
I would imagine you don't want your shell script to be in your web root, as it would be visible to anyone with web access to your server.
I would recommend moving the shell script to outside of the webroot
<?php
$tempFolder = '/tmp';
$webRootFolder = '/var/www';
$scriptName = 'myscript.sh';
$moveCommand = "mv $webRootFolder/$scriptName $tempFolder/$scriptName";
$output = shell_exec($moveCommand);
?>
In regards to the:
i added www-data ALL=(ALL) NOPASSWD:ALL to /etc/sudoers works
You can modify this to only cover the specific commands in your script which require sudo. Otherwise, if none of the commands in your sh script require sudo to execute, you don't need to do this at all anyway.
Try running the script as the apache user (use the su command to switch to the apache user) and if you are not prompted for sudo or given permission denied, etc, it'll be fine.
ie:
sudo su apache (or www-data)
cd /var/www
sh ./myscript
Also... what brought me here was that I wanted to run a multi line shell script using commands that are dynamically generated. I wanted all of my commands to run in the same shell, which won't happen using multiple calls to shell_exec(). The answer to that one is to do it like Jenkins - create your dynamically generated multi line of commands, put it in a variable, save it to a file in a temp folder, execute that file (using shell_exec in() php as Jenkins is Java), then do whatever you want with the output, and delete the temp file
... voila
If you are having a small script that you need to run (I simply needed to copy a file), I found it much easier to call the commands on the PHP script by calling
exec("sudo cp /tmp/testfile1 /var/www/html/testfile2");
and enabling such transaction by editing (or rather adding) a permitting line to the sudoers by first calling sudo visudo and adding the following line to the very end of it
www-data ALL=(ALL) NOPASSWD:/bin/cp /tmp/testfile1 /var/www/html/testfile2
All I wanted to do was to copy a file and I have been having problems with doing so because of the root password problem, and as you mentioned I did NOT want to expose the system to have no password for all root transactions.