gpt4 book ai didi

linux - 无需密码即可远程关闭/重启 Linux 机器

转载 作者:IT王子 更新时间:2023-10-29 01:14:34 25 4
gpt4 key购买 nike

我正在编写一个 pyQt 客户端-服务器应用程序,它可以远程重启/关闭 PC。
receivers 正在监听网络中的传入消息,sender 向选定的 receiver 发送重启/关闭消息。

以下部分代码在接收器上运行:

import os

self.currentOS = calling a function to determine the current OS

if self.currentOS == "Win":
os.system("shutdown -r -f -t 1")
elif self.currentOS == "Lin":
os.system("shutdown -r now")

我有 2 个虚拟机充当接收器,一个在 Windows 上,另一个在 Linux 上。

当我向 Windows 接收器发送重新启动消息时,机器重新启动。
当我向 Linux 接收器发送重启消息时,它要求输入密码

Incoming:EXEC_OP_RESTART
[sudo] password for jwalker:

我必须改变什么才能克服这个问题?
shutdown -r now 是唯一的方法吗,还是我可以用另一种方法(更直接地)来做到这一点?

编辑:在 this问题,使用了一个叫做 dbus 的东西,它是在没有密码的情况下完成的,我正在搜索 dbus,作为替代方案。

最佳答案

重启 Linux 机器需要 root 权限。一些桌面环境使用守护进程来解决这个问题......但我建议编辑 sudoers 文件

https://help.ubuntu.com/community/Sudoers一个howto。基本上,您需要允许重新启动命令 - 并且只允许重新启动命令 - 在没有密码的情况下运行。

类似于:

ALL ALL=(ALL) NOPASSWD: /usr/sbin/shutdown

将允许机器上的任何用户在不使用密码的情况下重新启动它。您可能需要在系统命令中的“关机”前加上“sudo”前缀,尽管它看起来像是以某种方式自动调用的。如果这不够安全,您可以创建一个组,让您的程序作为该组运行,然后允许该组重新启动。

编辑:显然这可以用 DBus 完成(注意:我还没有测试过):

dbus-send --system --print-reply --dest="org.freedesktop.Hal" /org/freedesktop/Hal/devices/computer org.freedesktop.Hal.Device.SystemPowerManagement.Restart int32:0

这是可行的,因为 dbus 以 root 身份运行(或具有 root 特权),因此可以接受从非特权进程重新启动的请求并对其执行操作。我仍然认为 sudo 方式更简洁,维护此代码的任何人也是如此。

关于linux - 无需密码即可远程关闭/重启 Linux 机器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8089471/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com