35

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

amy@amy:~$ shutdown now
shutdown: Need to be root

and

amy@amy:~$ 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.

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
amyassin
  • 1,361

2 Answers2

33

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

Michael Mrozek
  • 93,103
  • 40
  • 240
  • 233
  • is it secure?? 'you could have it do whatever you want.' – amyassin Aug 10 '11 at 17:52
  • 7
    To get acpid to do something different than it already does, you'd have to be root, so you'd already have permission to do insecure things. It is also possible to use MAC systems like SELinux to protect acpid, restricting the things they are allowed to do, even while they run as root. – Warren Young Aug 10 '11 at 18:00
  • 5
    @amyassin Well, to change what it does you need root privileges; a normal user can't tell it what to do. But acpid is configurable, it can run different scripts depending on the hardware events it sees (for example, I have acpid lock my computer when I hit the power button) – Michael Mrozek Aug 10 '11 at 18:00
  • 9
    @amyassen If someone has physical access to your machine then it's too late to worry about security -- if the power button doesn't trigger a graceful shutdown they can just pull the power cord and cause an ungraceful shutdown. – Shadur-don't-feed-the-AI Aug 10 '11 at 20:23
  • @Shadur I mean if some malicious software could crack in somehow,, this can be a weak point... Though can't think how that malicious software can get in... Just wondered... – amyassin Aug 10 '11 at 21:18
  • 3
    Said 'malicious software' would have to get root ownership and SUID permissions in order to tell init to make with the shutdown -- and again, by the time it gets to that point you have bigger problems to worry about than a possible shutdown. – Shadur-don't-feed-the-AI Aug 11 '11 at 07:11
  • This is a great answer, but most modern desktop environments actually use dbus rather than invoking a shutdown themselves. – Chris Down Dec 24 '12 at 11:46
11

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
Chris Down
  • 125,559
  • 25
  • 270
  • 266