Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have recently given up my Desktop Environment and am now only using a Window Manager (i3) and X. It all works very well and I do not miss much. Except for one thing: the shutdown button. I can now only shutdown by issuing:

sudo shutdown -Ph now

from a terminal. This works, but I want to create a keybinding in i3 to shutdown by clicking Mod+x. And I am not sure how this can be achieved. If I create a script in say ~/shutdown and refer to this with:

bindsym $mod+x exec ~/shutdown

Then this does not work; I figure because I am running as a regular user and I am sudoing in this script. I would be fine with having a terminal appear and prompting me for the password (although I prefer this to not require a password, if possible), but am not sure how to do this. What is a way to get this done?

Using Arch Linux.

share|improve this question
add comment

3 Answers

up vote 6 down vote accepted

If you use D-Bus sessions and ConsoleKit (which is a default component of most modern desktop systems, so you may already have it installed), a system poweroff approach that is slightly cleaner than sudo shutdown and that does not require any sort of root privilege is:

dbus-send --system --print-reply --dest="org.freedesktop.ConsoleKit" \
 /org/freedesktop/ConsoleKit/Manager org.freedesktop.ConsoleKit.Manager.Stop

Similarly, a system restart can be accomplished with

dbus-send --system --print-reply --dest="org.freedesktop.ConsoleKit" \
  /org/freedesktop/ConsoleKit/Manager org.freedesktop.ConsoleKit.Manager.Restart

You can put those in scripts and add shortcuts or menu entries to call them.

share|improve this answer
While a bit more complicated than a sudo command, I agree this is the cleanest solution. –  Patrick Jan 6 '12 at 12:59
add comment

One way to do this is to have sudo not prompt for the password. Add the following to your /etc/sudoers file (replacing johndoe with your user)

johndoe ALL = (root) NOPASSWD: /sbin/shutdown
share|improve this answer
add comment

According to the documentation, i3 allows you to bind either on keycodes or on keysyms.

So, you could put an entry in your sudoers file, like:

plua  ALL=(ALL) NOPASSWD:/sbin/shutdown

which would authorize user plua to be able to shutdown without issuing a password, and then create a key bind to that effect, such as:

bind Mod+x exec /path/to/shutdownscript
share|improve this answer
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.