linux服务器在php脚本中重新启动

发布于 2024-11-19 13:05:28 字数 394 浏览 0 评论 0原文

我想知道是否可以使用PHP脚本重新启动Linux服务器?关于将 IP 地址从静态更改为 DHCP,我需要重新启动系统才能生效。

我尝试了这段代码:

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

错误消息是:

重新启动:必须是 useruser

这里是另一个:

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

错误是:

关闭环回接口:[失败] 启动环回接口:[失败]

希望你能在这方面帮助我。

谢谢你!

向大家问好。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

我不会写诗 2024-11-26 13:05:28

如果您的解释器、网络服务器的程序用户上下文有权执行这些命令,您可以重新启动它。网络服务器或 PHP 解释器不应以 root 身份运行。在这两种情况下,您可以使用 sudo, sudoers 来升级权限。

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.

又爬满兰若 2024-11-26 13:05:28

您需要像这样使用 sudo:

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

在 /etc/sudoers 中添加以下内容:

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

其中 apache 是 PHP 脚本运行时使用的用户名。

请注意这样做的安全隐患 - 任何有权访问服务器上 PHP 脚本的人都可以重新启动服务器。

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.

つ可否回来 2024-11-26 13:05:28

与将 IP 地址从静态更改为 DHCP 相关,我需要重新启动系统

不,您不需要。这不是微软Windows。但是重新映射网络接口的命令因发行版而异 - 并且您没有说明这是哪一个。同样,访问reboot、shutdown、init 和telinit 命令也因发行版而异。

我在本地服务器工作

那么为什么不通过 ssh 或在控制台上进行呢?

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?

苦妄 2024-11-26 13:05:28

既然您已经说过您是 Linux 的新手,我觉得值得指出的是,与 Windows 相比,需要重新启动 Linux 机器的情况要少得多。

即使更新核心软件包后,您也不需要重新启动。即使出现严重崩溃,您通常也无需重新启动即可恢复。

您还没有说明为什么要重新启动,但重新启动整个盒子确实应该是绝对的最后手段。事实上,对于 Linux 管理员来说,仅仅为了解决问题而重新启动被认为是非常糟糕的做法,因为它往往会消除导致问题的证据,并且无法防止问题再次出现。

在 Linux 上,大多数需要重新启动 Windows 机器的问题只需要重新启动单个程序或服务。

最后,关于安全性的说明:通过 PHP 程序执行诸如此类的主要系统操作是不好的安全实践,因为它向非 root 用户公开了根级别功能。我假设(好吧,我希望!)您计划锁定对此 PHP 页面的访问,但即使是最安全的网页也不应该被认为足够安全以运行根级操作。

简而言之,我的建议是你不应该这样做。如果您必须这样做,@qbert220的答案应该有效,但请不要这样做。

[编辑]
具体到将 IP 地址从 DHCP 更改为静态,这不需要在 Linux 中重新启动服务器。您只需重新启动网络接口即可。

一旦你改变了配置,类似这样的事情应该足以用新的 IP 地址重新启动你的网络接口:

sudo /etc/init.d/networking restart

你还没有指定你正在使用的 Linux 类型,但这里有一个页面的链接,该页面详细说明了怎么做它从Ubuntu 中的命令行

但它确实需要 root 权限,因此您需要使用 sudo 来实现它并将您的 Web 用户添加到 sudoers 列表中,正如我之前所说,从安全角度来看这确实不太好。

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.

丘比特射中我 2024-11-26 13:05:28

脚本必须设置为以 root 身份运行:

reboot.php

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

将其设置为 root 脚本:

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

但是为什么要使用 php 执行此操作呢?只需在 sh 中编写一个脚本,如下所示:

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文