Restarting a Linux Server via Web Browser (PHP)

六眼飞鱼酱① 提交于 2021-02-04 17:49:47

问题


After opening and reading every result on Google, I figured it's time to make my own thread somewhere. I am sorry that I need to ask a question that's already been asked before, I cannot stress this enough, but I have no other option as no other question asked has helped me achieve what my goal is.

I'm trying to setup a means of rebooting/doing other system functions through a web interface powered by HTML (for the buttons/text) and PHP (for the execution of the aforementioned functions).

I'm unable to get this to work. I've read that I need to add the web user to the sudoers file, and I've tried. I'm running Nginx on my server, how do I add the user to the sudoers in my case?

Also, I'm aware of the security risks.

The following is what I have so far:

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");
?>

Mind you that here, I only have so many things attempting to execute, so that I make sure I hit the target when one of them works, if that makes sense. The IFConfig is just a test to make sure that it's actually able to execute.

Sudoers:

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

This is all on Ubuntu 14.04 LEMP.


回答1:


You can do this either by editing your sudoers file :

Sudoers:

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

The first ALL is for the hostname if you're hostname is not 'reboot' I advise you to keep ALL as it will work in any hostname. That's why it doesn't seem to work on your server

restart.php

exec('sudo /sbin/reboot');

Or without editing your sudoers file.

  • First create a file where you're gonna store you're root password

~/password :

myrootpassword
  • Second run any command you want while being root from php file (don't forget to specify the file which store your password)

phpfile.php :

exec('sudo -u root -S /sbin/reboot < ~/password');



回答2:


 www-data reboot = NOPASSWD: /sbin/reboot

this means you dont need a password when running sudo, not that the command runs as sudo when ever run by that user.

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




回答3:


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



来源:https://stackoverflow.com/questions/28990441/restarting-a-linux-server-via-web-browser-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!