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.


3 Answers 3


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.

  • 1
    While a bit more complicated than a sudo command, I agree this is the cleanest solution.
    – phemmer
    Jan 6, 2012 at 12:59

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

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

You must log in to answer this question.