Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executing commands in php as root user in arch linux

i am using arch linux. i want to execute the php file which changes the ip of the system. i did

ifconfig eth0 192.168.163.137

in the terminal and it works fine. the same i tried doing with

shell_exec('ifconfig eth0 192.168.163.137');

in a php file and tried opening the page from a remotely located web browser from another pc connected via router. teh page displays nothing and the code also doesnt execute. i guess its the problem with the user executing it.apache is executing it. so i want it to be run by the root.can anyone please guide me to the execution of my code. i even installed sudo and just put

shell_exec('sudo ifconfig......');

it too doesnt execute...please help...thanku..:)

like image 497
Apurv Nerlekar Avatar asked Aug 27 '26 13:08

Apurv Nerlekar


1 Answers

Sudo normally requires an interactive shell to enter your password. That's obviously not going to happen in a PHP script. If you're sure you know what you're doing and you've got your security issues covered, try allowing the Apache user to run sudo without a password, but only for certain commands.

For example, adding the following line in your sudoers file will allow Apache to run sudo without a password, only for the ifconfig command.

apache ALL=NOPASSWD: /sbin/ifconfig

Adjust the path and add any arguments to suit your needs.

Caution:

  • There might still be complications due to the way PHP calls shell commands.
  • Remember that it's very risky to allow the web server to run commands as root!

Probably a better alternative:

  • Write a shell script with the suid bit to make it run as root no matter who calls it.
like image 122
kijin Avatar answered Aug 29 '26 05:08

kijin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!