php script running as sudo

Asked

Viewed 953 times

0

I’ve already created a script PHP that creates a folder inside my directory home but I want to know now how to change your rights (rwx) since the folder created has the daemon and the rest of the script (chown) does not work. Please detail as I do not have much experience with Linux.

  • What did you try to do? What error did you make? If you put this part of the script it’s easier for someone to help you.

  • Welcome to Stack Overflow! Please explain the problem better, and if possible include a example of code that reproduces what is happening, because your question is too wide. See Help Center How to Ask.

  • You should explain whether you are running your PHP on the terminal or on the webserver

3 answers

1

first - I don’t recommend running scripts by php with sudo, because as you said yourself, you don’t have much knowledge of linux, and that would be a huge loophole not only in your application but in your entire S.O..

2º - You could create a folder with global permission for everyone to access, so I believe employee for you mkdir("directorname", 0777);

More information about linux permissions in the link below

http://www.infowester.com/linuxpermissoes.php

3º - You can execute any command in the operating system with the exec or shell_exec

Some more permissions data below:

--------- 000

r-------- - 400

r-r-r-- 444

rw-------- 600

rw-r-r--- 644

rw-rw-rw- 666

rwx------- 700

rwxr-x--- 750

rwxr-Xr-x 755

rwxrwxrwx 777

0

  1. Create your PHP script without caring about the user you run, using chmod to change directory permissions.
  2. Run your script via command line: sudo php meuscript.php

There are many "pores" using this approach, but if you are root and want a script to help you administer the system, OK!

However, possibly bash will be the best language to achieve your goal.

  • With the support of the people here at the forum and some more researches I was able to notice the error that was commenting and solved the problems so far, I say this because the project continues and maybe will have new doubts. Just for the record I found in Yast the Sudoers editor and I was able to insert the lines that gave permission for the php script to run without asking for password and with sudo rights, besides of course writing these lines correctly in php. Grateful to all.

  • Did some of these answers help you the most? Mark it as an accepted answer.

0

You can already create the folder with permissions for everyone to access:

mkdir("pasta", 0777);

To run any linux command in PHP as an admin, you must create a root password file and pass it to sudo. Assuming you have saved this file to "/var/www/meuprojeto" under the name "password.secret":

exec('sudo -u root -S chown "usuario" "pasta" < /var/www/meuprojeto/senha.secret');

Browser other questions tagged

You are not signed in. Login or sign up in order to post.