Click here to Skip to main content
15,882,114 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I try to reboot my Ubuntu 16.04 through PHP.
I installed SSH2 extension, write code and it have a problem:
HTML
SSH connection: [OK]
Athentication: [OK]
Shell: [OK]
reboot -i
Welcome to Ubuntu 16.04 LTS (GNU/Linux 4.4.0-66-generic x86_64)

* Documentation: https://help.ubuntu.com/

332 packages can be updated.
4 updates are security updates.

Last login: Sun Mar 12 18:50:57 2017 from 192.168.80.137
]0;lam@lam: ~lam@lam:~$ reboot 
Failed to set wall message, ignoring: Interactive authentication required.
Failed to reboot system via logind: Interactive authentication required.
Failed to start reboot.target: Interactive authentication required.
See system logs and 'systemctl status reboot.target' for details.
Failed to open /dev/initctl: Permission denied
Failed to talk to init daemon. 

Then I use command line:
HTML
#chmod 777 /dev/initctl

It's work, Ubuntu reboot through PHP!!!!.
But after reboot, I try php code I gain, I show similar error above. I must #chmod 777 /dev/initctl again to keep it's work.
Please help me.
P/s: here my php code
PHP
<?php
        echo "SSH connection: ";
        if (!($resource=@ssh2_connect("192.168.80.137"))) {
                echo "[FAILED]<br />";
                exit(1);
        }
        echo "[OK]<br />";

        echo "Athentication: ";
        if (!@ssh2_auth_password($resource,"lam","root")) {
                echo "[FAILED]<br />";
                exit(1);
        }
        echo "[OK]<br />";

        echo "Shell: ";
        if (!($stdio = @ssh2_shell($resource,"xterm"))) {
                echo "[FAILED]<br />";
                exit(1);
        }
        echo "[OK]<br />";
		//$permission = "chmod 777 /dev/initctl \n";
		//fwrite($stdio,$permission);
        $command = "reboot\n";
        fwrite($stdio,$command);

        sleep(1);

        while($line = fgets($stdio)) {
                flush();
                echo $line."<br />";
        }

        fclose($stdio);
?>


What I have tried:

Reboot my Ubuntu 16.04 through PHP.
Posted
Comments
Peter_in_2780 13-Mar-17 3:19am    
/dev/initctl is a pipe, recreated every time you reboot. So your chmod doesn't persist.
To reboot my Ubuntu 16.04LTS server remotely, I ssh in and "sudo reboot". Incidentally I use strong authentication. No userid/password login is allowed.
Member 10390715 13-Mar-17 3:36am    
Thanks you. And this is my solve. I enable SSH by root user.
open sshd configuration file /etc/ssh/sshd_config and change line:
FROM:
PermitRootLogin prohibit-password
TO:
PermitRootLogin yes
And I don't need use "sudo"
Albert Holguin 13-Mar-17 10:21am    
FYI... this is considered bad for security, hence the feature being disabled by default.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900