简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM