Restarting a Linux server through a web browser (PHP)

After opening and reading each result on Google, I decided it was time to make my own thread. I am sorry that I need to ask a question that has already been asked before, I can’t stress it enough, but I have no other choice, since no other question asked has helped me achieve my goal.

I am trying to configure a tool to reboot / execute other system functions through a web interface supported by HTML (for buttons / text) and PHP (for performing the above functions).

I can't get this to work. I read that I need to add the user to the sudoers file, and I tried. I am running Nginx on my server, how to add a user to sudoers in my case?

In addition, I know about security threats.

The following is the following:

HTML (index.html):

<body> <h3>Restart</h3> <p> <form action="restart.php" method="get"> <input type="submit" value="Press me."> </form> </p> </body> 

PHP (restart.php):

 <?php echo "This is a test"; echo "<br>"; echo "<br>"; echo shell_exec('ifconfig'); echo "<br>"; echo "<br>"; echo "Restarting server..."; exec ('/usr/bin/sudo /etc/init.d/portmap restart'); shell_exec("/sbin/reboot"); exec("/sbin/reboot"); system("/sbin/reboot"); ?> 

Keep in mind that here, I only have so many things that they are trying to accomplish, so I make sure to hit the target when one of them works, if that makes sense. IFConfig is just a test to make sure that it is truly capable of performing.

Sudoers:

 # User privilege specification root ALL=(ALL:ALL) ALL www-data reboot = NOPASSWD: /sbin/reboot 

All this on Ubuntu 14.04 LEMP.

+6
source share
3 answers

You can do this by editing the sudoers file:

Sudoers:

 www-data ALL=(root) NOPASSWD: /sbin/reboot 

first ALL for the host name if the host name is not rebooting . I advise you to keep EVERYTHING as it will work on any hostname . That is why it does not work on your server.

restart.php

 exec('sudo /sbin/reboot'); 

Or without editing the sudoers file.

  • First create a file in which you are going to store the root password.

~ / password:

 myrootpassword 
  • Run any command you want as root from the php file (do not forget to specify the file that stores your password)

phpfile.php:

 exec('sudo -u root -S /sbin/reboot < ~/password'); 
+3
source
  www-data reboot = NOPASSWD: /sbin/reboot 

this means that you do not need a password when starting sudo, and not that the command runs as sudo ever executed by this user.

The answer is to use sudo /sbin/reboot as a command

0
source

You can use puty. I think using php is not a good plan.

-4
source

Source: https://habr.com/ru/post/983663/


All Articles