Shutdown from terminal without entering password?

Open up a terminal (CTRL + T) and type the following sudo visudo

Add the following line:

%group_name ALL=(ALL) NOPASSWD: /sbin/poweroff, /sbin/reboot, /sbin/shutdown

or

user_name ALL=(ALL) NOPASSWD: /sbin/poweroff, /sbin/reboot, /sbin/shutdown

This allows the user/group to run the above three commands, using sudo, however with no password.

So, sudo poweroff will now result in a passwordless shutdown.


However, to make this even cleaner, we'll add an alias, so that running shutdown calls sudo shutdown now.

Open ~/.bash_aliases for editing.

nano ~/.bash_aliases

Insert the following line at the end of the file:

alias shutdown='sudo shutdown now' 

Finally, load the changes to the .bash_aliases file...

source ~/.bash_aliases

Try it out!

shutdown

Thanks, Eric.


A safe way to do this without using sudo and without tinkering with the system, is by executing these one-liner commands:

For Ubuntu 14.10 or earlier:

Shutdown:

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

Restart:

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

consolekit Install consolekit should of course be installed your system.

Other commands you may like:

Suspend:

/usr/bin/dbus-send --system --print-reply --dest="org.freedesktop.UPower" /org/freedesktop/UPower org.freedesktop.UPower.Suspend

Hibernate: (if enabled on your system)

/usr/bin/dbus-send --system --print-reply --dest="org.freedesktop.UPower" /org/freedesktop/UPower org.freedesktop.UPower.Hibernate

For Ubuntu 15.04 and later:

(This is due to Ubuntu's shift in using systemd instead of Upstart)

systemctl poweroff

systemctl reboot

systemctl suspend

systemctl hibernate

systemctl hybrid-sleep

Since hibernate is normally disabled by default in Ubuntu systems, you can enable this by checking this answer. Original source here.