-1

I want to start a virtual machine through PHP, but i've had no luck. Here's what i've got.

<?php
if(isset($_POST['btn_start'])){
  echo shell_exec('whoami');
  echo exec('virsh start winagain');
}
?>

<!DOCTYPE HTML>
<html>
<head>
  <title>Manage VPS</title>
</head>
<body>
<form method="POST" action="vps.php">
  <input type="submit" name="btn_start" value="Start">
</form>
</body>
</html>

I get the error:

error: failed to connect to the hypervisor
error: no valid connection
error: Failed to connect socket to '/var/run/libvirt/libvirt-sock': Permission denied

So i tried putting this line into my /etc/sudoers file

www-data ALL = NOPASSWD: /var/run/libvirt/libvirt-sock

But it has done nothing.

I'm running out of ideas now, please help.

HBruijn
  • 77,029
  • 24
  • 135
  • 201
  • Welcome to Server Fault. Be sure to take the tour to see how this works and to earn a badge: https://serverfault.stackexchange.com/Tour – SDsolar Apr 29 '17 at 08:07

1 Answers1

1

If you're going to use sudo (which you probably do) to elevate privileges you need to :

  • grant access to the virsh command and not the socket file
  • use the absolute path to the virsh command in /etc/sudoers i.e.
    www-data ALL = NOPASSWD: /path/to/virsh
  • need to call sudo in your exec command
  • need to use the absolute path to virsh with sudo in your exec command i.e.
    shell_exec("/bin/sudo /path/to/virsh start wine again")
HBruijn
  • 77,029
  • 24
  • 135
  • 201
  • Thank you, I had made a temp fix with chmod -R 777 but i know that's not exactly safe... You're a life saver! – Jake Kirby May 01 '17 at 03:19