How does the power button shut the computer down without root permission?

When I try to shutdown the computer from a command line or terminal I must have root privileges:

<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="81e0ecf8c1e0ecf8">[email protected]</a>:~$ shutdown now
shutdown: Need to be root

and

<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4d2c20340d2c2034">[email protected]</a>:~$ halt
halt: Need to be root

but when shutting down using the graphical user interface, i.e. shutdown button, or the hardware shutdown button, I’m not asked for the password to do so. What does that shutdown for the graphical interface, and why it doesn’t need the password or root privileges?

I’m using Ubuntu 11.04 Natty.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

The hardware power button triggers an ACPI event that acpid (the ACPI daemon) notices and reacts to; in this case by shutting down the system, although you could have it do whatever you want. The ACPI daemon runs as root, so it has permission to shutdown the system. Desktop environments (e.g. gdm for Gnome) typically run as root as well, so I suspect they work the same way — you don’t have permission to shutdown the system, but you can tell gdm you want it shut down and it can do it on your behalf

Method 2

Michael’s answer correctly discusses system function when using the hardware power switch, but most desktop environments actually use dbus for this purpose rather than doing it themselves. For example, GNOME uses dbus‘s org.freedesktop.Hal.Device.SystemPowerManagement.Shutdown when the shutdown button is clicked. When this is sent, dbus does some checks to determine whether the user sending the message is authorised to perform a shutdown, and if they are, it shuts down the system.

You can emulate this by using dbus-send. For example, to shut down your system using dbus, use something like this:

dbus-send --system --dest=org.freedesktop.Hal /org/freedesktop/Hal/devices/computer org.freedesktop.Hal.Device.SystemPowerManagement.Shutdown


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x