C ++,linux,没有root的关机命令

问题描述 投票:-2回答:2

是否有可能制作一个仅以用户身份运行而不使用root特权的关机脚本?

我找到了此代码,但似乎无能为力:

#include <unistd.h>
#include <sys/reboot.h>

int main () {
    sync();    // If reboot() not preceded by a sync(), data will be lost.
    setuid(0); // set uid to root, the running uid must already have the
               // appropriate permissions to do this.
    reboot(RB_AUTOBOOT); // note, this reboots the system, it's not as
    return(0);           // graceful as asking the init system to reboot.
}

系统信息:

Linux hosek 4.15.0-48-generic #51-Ubuntu SMP Wed Apr 3 08:28:49 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
c++ linux root shutdown
2个回答
0
投票

具有systemd并具有适当权限的桌面系统可以从控制台连接到systemd:

systemctl poweroff

-2
投票

首先,不要这样做。无论您认为要执行多少操作,都不需要。认真。这样做主要是在搞乱系统安全性。即使是非常了解它们的人,也很难正确地找到这些系统。所以,不要这样做。如果您需要授予某人关闭系统的权限,请找出如何配置sudo以允许它。不要沿着你走的路走。

© www.soinside.com 2019 - 2024. All rights reserved.