59

I have desktop edition of Ubuntu.

I like the Terminal so that that I prefer to shutdown my computer with the shutdown command.

However when I type shutdown now it prompts me to enter my password.Is there any way I can shutdown my computer using this command without entering my password?

dlin
  • 3,830
  • 4
    Does anyone know what command is executed when I click "shutdown" in Desktop Environment? – kubahaha Feb 20 '15 at 17:15
  • For me both poweroff and shutdown now works from terminal. In Ubuntu 20,04, but I an sure that it has been like that for years. – Soren A Apr 29 '20 at 22:00
  • Note that there is a difference to whether you have a password and just dont want to type it, or if you have no access to superuser privileges. For the first case, there are many useful answers, but for the latter one, only Ubuntu 14.10 and earlier are helped by this answer and if you have access to gnome, you can consider this one. I didn't find a solution for my scenario. – Cadoiz Nov 24 '21 at 11:31

6 Answers6

75

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.

SirCharlo
  • 39,486
38

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

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.


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
Majal
  • 7,711
  • 2
    This is a useful reference, however the systemctl command requires to input the user's password, so it is not applicable as a direct answer to the question. – Andrea Lazzarotto Aug 17 '20 at 12:48
  • 1
    Revisited this after more that 8 years. :-) And saw the comment above. I tried the systemctl commands on a freshly installed Ubuntu 20.04 system and it did not require sudo to execute them. – Majal Jan 25 '22 at 13:38
2

unity uses many gnome services, and in that case too - you can shutdown gnome way.

gnome-session-quit --power-off --force --no-prompt

will do the job.

  • Looks like this only works for logout. From man page of --no-prompt: End the session without user interaction. This only works with --logout. – Pylinux Nov 02 '19 at 21:41
1

On Ubuntu 20.04 LTS, I created a polkit file with

echo "[Shutdown or suspend without a password]
Identity=unix-user:$USER
Action=org.freedesktop.login1.set-wall-message;org.freedesktop.login1.halt;org.freedesktop.login1.suspend
ResultAny=yes
" | sudo tee /etc/polkit-1/localauthority/50-local.d/allow_shutdown_suspend.pkla

With this, systemctl halt and systemctl suspend work without a password. This also work remotely with e.g. ssh 192.168.0.55 -t 'systemctl suspend'.

1

While you can use the method of allowing NOPASSWD on /usr/sbin/shutdown, although another, DE-independent solution is to just use init 0.

-1

that is easy. using -S option like this:

echo <your-password> | sudo -S poweroff
Vijay
  • 8,056
Qijun Liu
  • 115
  • 1
    Warning: that is very bad practice, as everybody and every process could read <your-password> from the history! – Cadoiz Nov 24 '21 at 11:26