0

I would like to know if it is possible to restart the Linux server using PHP script? In related to changing IP Address from Static to DHCP, I need to reboot the system so that it will take effect.

i tried this code:

system("/usr/bin/reboot");

error message is :

reboot: must be useruser

here'e the another:

system('/etc/init.d/network restart');

the error is:

Shutting down loopback interface: [FAILED] Bringing up loopback interface: [FAILED]

Hope you can help me in this.

Thank you!

Regards to all.

2
  • 1
    Your trying this on shared hosting? Commented Jul 11, 2011 at 8:02
  • I am working in local server not on shared hosting.. Commented Jul 11, 2011 at 8:05

5 Answers 5

3

You can restart it if the program-users-context of your interpreter, webserver has the rights to execute these commands. A webserver or php interpreter should not be run as root. You may use sudo, sudoers in order to escalate privileges in these both cases.

Sign up to request clarification or add additional context in comments.

2 Comments

thank you, can you provide me a sample script? because I am new in Linux command - btw I am using CentOS distro. thank you again.
look here for an example to use sudo mannyvellon.sys-con.com/node/851342/mobile
3

You will need to use sudo like this:

system("sudo /usr/bin/reboot");

in your /etc/sudoers add the following:

apache ALL=(ALL) NOPASSWD: /usr/bin/reboot

Where apache is the username under which the PHP script runs.

Be aware of the security impication of doing this - anyone with access to PHP scripts on the server to reboot the server.

6 Comments

Where apache is the username under which the PHP script runs. - how can I know this?
use ps -df | grep apache to find the apache processes. The username should be in the first column.
i tried this, and my I am "apache" user in first column... but when I process this command system("sudo /usr/bin/reboot"), this is the message: "sudo: no valid sudoers sources found, quitting "
Did you edit your sudoers file as shown in the answer? Try typeing sudo ls into a shell prompt and see whether you get any additional error messages.
yes, I edited the sudoers and add the line "apache All=(ALL) NOPASSWD: /usr/bin/reboot... when i type in prompt sudo ls: same error message I recieved during the execution in the web page. "syntax error near line 56, 58, 59....
|
1

In related to changing IP Address from Static to DHCP, I need to reboot the system

No you don't. This is not Microsoft Windows. But the command for remapping the network interfaces varies from distribution to distribution - and you don't say which this is. Similarly, access the reboot, shutdown, init and telinit commands varies by distribution.

I am working in local server

So why not just do it via ssh or at the console?

1 Comment

thank you...I am a newbie in Linux and I am not yet acquainted with other functionality of it.
0

Since you have stated that you're a newbie to Linux, I feel that it's worth pointing out that it's much much less common to need to reboot a Linux box compared to a Windows one.

You shouldn't need to reboot even after updating core software packages. Even if something crashes badly, you can ususally recover without a reboot.

You haven't stated why you'd want to be doing a reboot, but rebooting the whole box really should be an absolute last resort. In fact, rebooting simply to clear an issue is consdered very bad practice for a Linux administrator because it tends to wipe out evidence of what caused the problem, and does nothing to prevent the problem from recurring.

On Linux, most issues that would require a Windows box to be rebooted only require the individual program or service to be restarted.

Finally, a note on security: Doing major system operations such as this via a PHP program is bad security practice because it exposes root level functionality to non-root users. I assume (well, I hope!) that you're planning to lock down access to this PHP page, but even the best secured web page should not be considered secure enough to be running root-level operations.

In short, my advice is that you shouldn't do this. If you must do it, @qbert220's answer should work, but please don't do it.

[EDIT] With specific regard to changing the IP address from DHCP to static, this should not require a server reboot in Linux. You simply need to restart the networking interface.

Once you've changed the config, something like this should be enough to restart your network interface with the new IP address in place:

sudo /etc/init.d/networking restart

You haven't specified what variety of Linux you're using, but here's a link to a page which details how to do it from the commandline in Ubuntu.

It does require root priviledges though, so you would need to use sudo to achieve it and to add your web user to the sudoers list, which as I said before is really not great from a security point of view.

2 Comments

thank you for your advice. But my project is to create a web page wherein I can change the IP address of our server, from DHCP to Static IP or vice versa... how can it take effect without restarting the server? how can I be a root user? every time I created a new file through the PHP script, it appears "apache" as user of the file. Any idea?
@hmbarit - this should be possible to do just by restarting the network interface, rather than rebooting the whole server. Possibly you may need to restart Apache too.
0

Script must be set to run as root:

reboot.php

<?php
   exec("reboot -d -f -i");
?>

Meke it a root script:

chown root.root reboot.php
chmod 700 reboot.php

But why do this with php? Just make a script in sh like so:

#! /bin/sh 
PATH=/sbin:/usr/sbin:/bin:/usr/bin
reboot -d -f -i

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.