Allow users to shutdown computer from a window manager

Share your wisdom. Not for support questions!

Moderator: How-to Curator

Allow users to shutdown computer from a window manager

Postby mojoman » February 20th, 2011, 10:14 am

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
[YOUR_USERNAME] ALL=SHUTDOWN
[YOUR_USERNAME] ALL=NOPASSWD: SHUTDOWN

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} <>
   [end]


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>
                        <item label="Exit">
                                <action name="Exit"/>
                        </item>
                        <item label="Reboot">
                                <action name="Execute">
                                        <execute>
                                                sudo /sbin/shutdown -r now
                                        </execute>
                                </action>
                        </item>
                        <item label="Shutdown">
                                <action name="Execute">
                                        <execute>
                                                sudo /sbin/shutdown -h now
                                        </execute>
                                </action>
                        </item>
        </menu>


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.
/mojoman

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.
Seriously, Stable is for senior citizens and Sid is for kids. Be a man and run Testing.
User avatar
mojoman
 
Posts: 428
Joined: February 9th, 2011, 12:33 pm
Location: In the neck of the wood

Return to HowTo

Who is online

Users browsing this forum: No registered users and 1 guest

x