Page 1 of 1

Allow users to shutdown computer from a window manager

PostPosted: February 20th, 2011, 10:14 am
by mojoman
I use window managers fairly much (fluxbox and openbox mostly) and one thing that I really want in my menu is the option to shutdown and reboot the computer, not just to log out or restart whatever window manager I use. The power to shutdown or reboot is normally reserved for root (gnome and kde users does it since gdm and kdm can do it) but window managers are controlled by the user, and thus lacks the powers that the user lacks. (I might be wrong but I seem to recall that a user on Xfce4 with gdm do not have the power to shutdown the computer from Xfce4 either, thus also requiring this little trick.)

This is where sudo comes into play. Sudo is used to allow users to temporarily assume root powers, either full out or limited to certain commands, with or without requiring a password. Of course, only an idiot would allow a user to assume full root powers without a password, and I personally fail to see the point in with sudo allowing full root powers with a password. Why not use su instead? Anyway, to give the user the power to shutdown or reboot the computer and this without requiring password is one good, legitimate use of sudo.

Who gets to do what with sudo is governed by the contents of the file sudoer, and to edit it you need to use the command visudo:
Code: Select all
# visudo -f /etc/sudoers

In the file add these lines:
Code: Select all
# Cmnd alias specification
Cmnd_Alias      SHUTDOWN = /sbin/shutdown

# User privilege specification

Save and exit and you should be able to shutdown the computer without further ado. (The reason that you need to add the full path [/sbin/shutdown] is because /sbin is not in your users path, or shouldn’t be at any rate).

Try it out to see that it works (just a simulation):
Code: Select all
sudo /sbin/shutdown -k now

As an example, in my fluxbox menu I got a submenu called Quit. That part of the menu looks like this:

Code: Select all
   [submenu] (Quit)
      [exit] (Log out)
      [exec] (Reboot) {sudo /sbin/shutdown -r now} <>
      [exec] (Shutdown) {sudo /sbin/shutdown -h now} <>

Now, the 'Log out' part is the standard from the fluxbox menu so there's no need to define it. The commands for Reboot and Shutdown are the shutdown command that my user can do without password, with the option to restart and halt.

That part of my openbox menu look like this:
Code: Select all
        <menu id="root-menu-21144" label="Exit">
                        <item label="Restart">
                                <action name="Restart"/>
                        <item label="Exit">
                                <action name="Exit"/>
                        <item label="Reboot">
                                <action name="Execute">
                                                sudo /sbin/shutdown -r now
                        <item label="Shutdown">
                                <action name="Execute">
                                                sudo /sbin/shutdown -h now

To give credit where credit is due this is where I originally got the info.

If you have questions, comments, praise or complaints this is the thread for it.

Hope you find it useful.

EDIT: Xfce 4.4 (i.e. Lenny and after) shouldn't have any problems in this regard but version 4.2 had a very similar problem. However, if you're running Xfce and have these problems, have a look here and here.