Executing commands in php as root user in arch linux

I am using arch linux. I want to execute a php file that changes the ip of the system. I did

ifconfig eth0


in the terminal and it works fine. I tried to do the same with

shell_exec('ifconfig eth0');


in a php file and tried to open the page from a remote web browser from another computer connected through a router. nothing is displayed on the page and no code is executed either. I guess the problem is with the user executing it.apache is executing it. so i want it to be run by root.can someone please guide me to execute my code. i even installed sudo and just put

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


it doesn't do either ... please help ... thanku .. :)


source to share

3 answers

Sudo usually requires the interactive shell to enter your password. This obviously won't happen in a PHP script. If you are sure you know what you are doing and you have security issues, try letting the Apache user run sudo without a password, but only for certain commands.

For example, adding the following line to the 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 as per your need.


  • There can still be complications due to the way PHP invokes shell commands.
  • Remember that it is very dangerous to let the web server run commands as root!

Probably the best alternative:

  • Write a shell script with the suid bit so that it runs as root no matter who invokes it.



This feature is disabled when PHP is running in safe mode.

Documentation: http://php.net/manual/en/function.shell-exec.php

So maybe try customizing your file php.ini




Queue the commands and run cron, check them (only allow known good queries) and run them, then note that the queue is full of date and result.

Then your end user can click / wait for update using ajax.



All Articles