在没有 SSH 的情况下远程关闭/重启 Linux 机器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8528392/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 03:40:51  来源:igfitidea点击:

Remotely shutdown/reboot Linux boxes without SSH?

linuxshutdownremote-accessrebootwake-on-lan

提问by jasxun

I need to remotely shutdown and reboot Linux (Ubuntu) machines without logging into them(otherwise simple commands can do the job). The machines are just cheap PCs so there are no special power management hardware installed (though they can wake-on-lan). Is there some sort of "power management server" software that I can install on those boxes, which listens to remote requests for reboot/shutdown and acts accordingly? Of course it would be nice if it requires some authentication (password) in order to respond to the requests.

我需要远程关闭和重新启动 Linux (Ubuntu) 机器而不登录它们(否则简单的命令就可以完成这项工作)。这些机器只是廉价的 PC,因此没有安装特殊的电源管理硬件(尽管它们可以通过网络唤醒)。是否有某种“电源管理服务器”软件可以安装在这些盒子上,它会侦听远程重启/关闭请求并采取相应措施?当然,如果它需要一些身份验证(密码)才能响应请求,那就太好了。

回答by Pablo Castellazzi

A few options:

几个选项:

This tools are not exactly to shutdown machines (but they can do it), they are configuration management frameworks to administer a lots of machines, they can handle configuration changes, package installs and updates, and run all the commands you want, in one machine, in a set of machines, or in the whole network.

这个工具并不完全是关闭机器(但他们可以做到),它们是管理大量机器的配置管理框架,它们可以处理配置更改、包安装和更新,并在一台机器上运行你想要的所有命令,在一组机器中,或者在整个网络中。

回答by Nope

As pointed out by j?rgensen, you can use SYSRQ (http://en.wikipedia.org/wiki/Magic_SysRq_key), an API directly talking to the kernel.

正如 j?rgensen 所指出的,您可以使用 SYSRQ ( http://en.wikipedia.org/wiki/Magic_SysRq_key),这是一个直接与内核对话的 API。

Beware, these are quite hardcore and may harm your hardware. It takes the time of a single UDP packet transfer to reboot. Boom. We only use it on live diskless computers.

当心,这些是非常核心的,可能会损害您的硬件。重新启动需要花费单个 UDP 数据包传输的时间。繁荣。我们只在实时无盘计算机上使用它。

1. xt_SYSRQ (iptables modules, kernel)

1. xt_SYSRQ(iptables 模块,内核)

There is xt_SYSRQ, one of the iptables modules provided by xtables-addons-common : http://manpages.ubuntu.com/manpages/oneiric/man8/xtables-addons.8.html

有 xt_SYSRQ,xtables-addons-common 提供的 iptables 模块之一:http://manpages.ubuntu.com/manpages/oneiric/man8/xtables-addons.8.html

Installing on debian

在 debian 上安装

#!/bin/bash
apt-get install -qq xtables-addons-common iptables
echo -n "yolo" >/sys/module/xt_SYSRQ/parameters/password
iptables -A INPUT -p udp --dport 9 -j SYSRQ

Shotgun reboot

霰弹枪重启

#!/bin/bash
sysrq_key="sub"  # the SysRq key(s), Sync, Unmount, reBoot
password="yolo"
seqno="$(date +%s)"
salt="$(dd bs=12 count=1 if=/dev/urandom 2>/dev/null | openssl enc -base64)"
ipaddr=""
req="$sysrq_key,$seqno,$salt"
req="$req,$(echo -n "$req,$ipaddr,$password" | sha1sum | cut -c1-40)"
echo "$req" | socat stdin udp-sendto:$ipaddr:9

2. sysrqd (tcp 4094 listening daemon, userland)

2. sysrqd(tcp 4094 监听守护进程,userland)

This solution works only if your bricked computer is able to handle TCP connections.

只有当您的砖砌计算机能够处理 TCP 连接时,此解决方案才有效。

Installing on debian

在 debian 上安装

#!/bin/bash
apt-get install -qq sysrqd
echo "yolo" > /etc/sysrqd.secret
service sysrqd restart

Shutgun reboot

关机重启

I made a script, https://gist.github.com/qolund/1470beaa1a63e034025dbut its just a TCP connexion on port 4094. You need to send the password and the commands,

我做了一个脚本,https://gist.github.com/qolund/1470beaa1a63e034025d但它只是端口 4094 上的 TCP 连接。您需要发送密码和命令,

# telnet 172.16.42.180 4094
Trying 172.16.42.180...
Connected to 172.16.42.180.
Escape character is '^]'.
sysrqd password: nope
Go away!
Connection closed by foreign host.
# telnet 172.16.42.180 4094
Trying 172.16.42.180...
Connected to 172.16.42.180.
Escape character is '^]'.
sysrqd password: yolo
sysrq> sub
[..]

The connection isn't properly closed, because the 'b' reboot command is too fast, the computer is already rebooting.

连接未正确关闭,因为“b”重启命令太快,计算机已经在重启。