Ubuntu – Why do we need to be root in terminal for shutdown and restart

privilegesrestartshutdownsudo

When we install/remove/update packages or make any changes which require administrative privileges we are prompted for the password of admin user who has the sudo privileges – this happens both via GUI and terminal.

prompt via gui

However, if we try to shutdown and restart via terminal, it complains that we need to be root:

$ reboot
reboot: Need to be root

$ shutdown now
shutdown: Need to be root

But we are never asked for a password when we perform these actions via the cog-wheel at top right.

cog-wheel menu

Why is there this discrepancy?

Best Answer

The shutdown on the cog-wheel checks if you are allowed to shutdown the machine. This is done via PolicyKit. In case of shutdown this statement in the file /usr/share/polkit-1/actions/org.freedesktop.consolekit.policy is checked:

<action id="org.freedesktop.consolekit.system.stop">
  <description>Stop the system</description>
  <message>System policy prevents stopping the system</message>
  <defaults>
    <allow_inactive>no</allow_inactive>
    <allow_active>yes</allow_active>
  </defaults>
</action>

The PolicyKit triggers a dbus-send command. In case of shutdown it would be:

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

There is a daemon running in the background with root-Privileges that invokes the shutdown command for you.

When you want to be able to shutdown the machine "the old way" via command line (shutdown, reboot, halt, ...), then you need to add the suid-Bit to those commands. But be aware, everyone on your system, that has access to the shell could then shutdown your machine.