开发者

How can I use sudo to reboot my server using php?

开发者 https://www.devze.com 2022-12-20 15:08 出处:网络
I\'ve tried these: <?php $r = shell_exec(\'sudo -u root -S /etc/init.d/apache2 restart < /home/$user/passfile\');

I've tried these:

<?php
  $r = shell_exec('sudo -u root -S /etc/init.d/apache2 restart < /home/$user/passfile');
  echo $rin;
?>

(password on passfile) DIDNT GET

<?php
  (php$output4=shell_exec('echo "$x" | whoami 1>/dev/null && sudo /etc/init.d/ssh restart');
?>
<html>
开发者_开发技巧  <body>
    <script type="text/javascript">
      x=prompt("entr");
    </script>
  <body>
</html>
<html>
  <form id="form1" name="form1" method="post" action="">
    <input type="submit" name="b1" value="click"  />
  </form>
</html>
<?php
  if(isset($_POST['b1'])) {
    echo "<pre>$output4</pre>";
  }

?>

Please help.


From man sudo on OS X:

-S          The -S (stdin) option causes sudo to read the password from the standard input instead of the
                   terminal device.


You can't invoke sudo this way; it reads it's password directly from the keyboard (via /dev/tty), not from STDIN.

If you want to execute something as a root user, you have to run the script as the root user.


You can't invoke sudo this way, for security reasons. If there is a workaround, please forget about it as quickly as possible. What you are trying to do is a terrible idea. You do not want to restart your server from a web page. You really don't. Yes, I'm sure. Put your keyboard down and walk away slowly.

Maybe if you tell us what you're trying to do, we can help you come up with a better way.


Ensure that require tty is disabled in your sudoers file.

Snippet from /etc/sudoers

#
# Disable "ssh hostname sudo <cmd>", because it will show the password in clear.
#         You have to run "ssh -t hostname sudo <cmd>".
#
#Defaults    requiretty

# Apache suoders entry - allow passwordless login
apache ALL=(ALL) NOPASSWD: ALL
0

精彩评论

暂无评论...
验证码 换一张
取 消