0

I want to overwrite my "/etc/network/interfaces" file with this PHP command

exec('sudo echo -e "# Include files from /etc/network/interfaces.d:\nsource-directory /etc/network/interfaces.dauto eth0\n\nauto eth0\niface eth0 inet static\naddress \nnetmask \ngateway \n\n" > /etc/network/interfaces');

I can see in my "/var/log/apache2/error.log" the log : sh: 1: cannot create /etc/network/interfaces: Permission denied

How can I allow PHP to overwrite this file?

dida110
  • 111
  • 1
  • 9
  • For do that you does to edit the permission file with the root user, but it's a real bad idea i think – Inazo Aug 27 '18 at 09:24
  • 1
    if you really wanted to do that, make a bash file that can delete the file you want, chmod it and then execute the file from your php script. (It is better to build a program using c++ for this sort of stuff) – kks21199 Aug 27 '18 at 09:27
  • You can try to do that using the shell_exec function, as described in their documentation: http://php.net/manual/pl/function.shell-exec.php – Rafael Aug 27 '18 at 09:30
  • Thanks but I need to do it with PHP, security is not really important in this case. If I make this bash file deleting the interfaces file would work but I still can't create a new with my exec command? – dida110 Aug 27 '18 at 09:32
  • You need to run a bash script, or a series of commands within php with sudo. [Here is an example](https://stackoverflow.com/questions/3173201/sudo-in-php-exec) – Cemal Aug 27 '18 at 10:02

1 Answers1

0

Solution using a binary wrapper (with suid bit).

You can take help from this Solution using a binary wrapper.

sharif779
  • 174
  • 2
  • 10