Linux – How to Set Shutdown Permissions

linuxpermissions

Suppose you install a desktop environment, say ubuntu or debian. You can shutdown the system by clicking a button somewhere on your system menu as a normal user. You don't need to switch to superuser to accomplish that.

However in the same desktop environment, if I open a terminal (say gnome-terminal) as a normal user, and type

shutdown -h now

I would be prompted by

shutdown: need to be root

The only way to shutdown is to prepend the command with a sudo.

Can anyone explain why this is so?

Thanks
KC

Best Answer

The question asked by K.Chen is: why do I need sudo privileges when I do it from CLI, ahile I do not need such privileges when I do it from the GUI.

The first part of the answer is that people who design Desktop Environments, like Gnome, KDE, Xfce, Mate, Cinnamon, ... try to simplify the work of their users, and they configure shutting down and rebooting without requiring sudo credentials. This, incidentally, implies that there must be a shutdown sequence which does not involve the program shutdown, which does require sudo privileges (no way around that).

I do not know in detail how each DE does it, but I know that there is a gentle way to bring down, or restart/shutdown/hibernate your system, which does not require root privileges. You can find the original post in an Arch Linux Forum post. In essence, it amounts to issuing hese commands:

halt

 #!/bin/bash
 dbus-send --system --print-reply --dest="org.freedesktop.ConsoleKit"/org/freedesktop/ConsoleKit/Manager org.freedesktop.ConsoleKit.Manager.Stop

reboot

 #!/bin/bash
 dbus-send --system --print-reply --dest="org.freedesktop.ConsoleKit"  /org/freedesktop/ConsoleKit/Manager org.freedesktop.ConsoleKit.Manager.Restart

dbus-suspend

 #!/bin/bash
 dbus-send --system --print-reply --dest="org.freedesktop.UPower" /org/freedesktop/UPower org.freedesktop.UPower.Suspend

hibernate

 #!/bin/bash
 dbus-send --system --print-reply --dest="org.freedesktop.UPower" /org/freedesktop/UPower org.freedesktop.UPower.Hibernate 

My guess is that the GUI buttons use roughly these commands. To be certain one should look into the code, but I believe this is a safe bet.

Related Question