Difference between revisions of "Allow users to shutdown"

From ArchWiki
Jump to: navigation, search
(Using systemd-logind: 'man poweroff' syas it's a legacy command and we should use 'systemctl poweroff')
(Updated example. Style fixes.)
Line 7: Line 7:
 
[[uk:Allow Users to Shutdown]]
 
[[uk:Allow Users to Shutdown]]
 
[[zh-CN:Allow Users to Shutdown]]
 
[[zh-CN:Allow Users to Shutdown]]
 +
 
== Using systemd-logind ==
 
== Using systemd-logind ==
  
If you're using [[systemd]] (which is default in Arch Linux), users with non-remote session can issue power-related commands as long as {{pkg|polkit}} is installed and [[xinitrc#Preserving the session|the session is not broken]].
+
If you're using [[systemd]] (which is default in Arch Linux), users with non-remote session can issue power-related commands as long as {{Pkg|polkit}} is installed and [[xinitrc#Preserving the session|the session is not broken]].
  
 
To check if your session is active
 
To check if your session is active
Line 17: Line 18:
 
  $ systemctl poweroff
 
  $ systemctl poweroff
  
The suspend, poweroff and hibernate button presses and lid close events are also handled by ''logind'' as described in [[Power Management#ACPI events]].
+
The suspend, poweroff and hibernate button presses and lid close events are also handled by ''logind'' as described in [[Power Management#ACPI events|ACPI events]] page.
  
 
== Using sudo ==
 
== Using sudo ==
{{Out of date|This method relies on {{ic|/usr/bin/shutdown}}, which is only provided by {{Pkg|systemd-sysvcompat}} and in fact links to {{ic|/usr/bin/systemctl}}.}}
 
  
First install sudo:
+
[[pacman|Install]] {{Pkg|sudo}}, then, as root, add the following to the end of {{ic|/etc/sudoers}} using the {{ic|visudo}} command. Substitute ''user'' for your username and ''hostname'' for the machine's hostname.
# pacman -S sudo
+
  
Then, as root, add the following to the end of {{ic|/etc/sudoers}} using the {{Ic|visudo}} command. Substitute {{Ic|'''user'''}} for your username and {{Ic|'''hostname'''}} for the machine's hostname.
+
''user'' ''hostname'' =NOPASSWD: /usr/bin/systemctl poweroff,/usr/bin/systemctl halt,/usr/bin/systemctl reboot
  
'''user''' '''hostname''' =NOPASSWD: /usr/bin/shutdown -h now,/usr/bin/halt,/usr/bin/poweroff,/usr/bin/reboot
+
Now your user can shutdown with {{ic|sudo systemctl poweroff}}, and reboot with {{ic|sudo systemctl reboot}}. Users wishing to power down a system can also use or {{ic|sudo systemctl halt}}. Use the {{ic|NOPASSWD:}} tag only if you do not want to be prompted for your password.
 
+
Now your user can shutdown with {{Ic|sudo shutdown -h now}}, and reboot with {{Ic|sudo reboot}}. Users wishing to power down a system can also use {{Ic|poweroff}} or {{Ic|halt}}. Use the {{ic|NOPASSWD:}} tag only if you do not want to be prompted for your password.
+
  
 
For convenience, you can add these [[Bash#Aliases|aliases]] to your user's {{ic|~/.bashrc}} if you have it enabled (or to {{ic|/etc/bash.bashrc}} for system-wide settings):
 
For convenience, you can add these [[Bash#Aliases|aliases]] to your user's {{ic|~/.bashrc}} if you have it enabled (or to {{ic|/etc/bash.bashrc}} for system-wide settings):
  
  alias reboot="sudo reboot"
+
  alias reboot="sudo systemctl reboot"
  alias poweroff="sudo poweroff"
+
  alias poweroff="sudo systemctl poweroff"
  alias halt="sudo halt"
+
  alias halt="sudo systemctl halt"
 +
 
 +
== Using acpid ==
  
==Using acpid==
 
 
[[acpid]] can be used to allow anyone with physical access to cleanly shut down the computer by using this power button.
 
[[acpid]] can be used to allow anyone with physical access to cleanly shut down the computer by using this power button.

Revision as of 18:06, 8 November 2013


Using systemd-logind

If you're using systemd (which is default in Arch Linux), users with non-remote session can issue power-related commands as long as polkit is installed and the session is not broken.

To check if your session is active

$ loginctl show-session $XDG_SESSION_ID --property=Active

To shutdown:

$ systemctl poweroff

The suspend, poweroff and hibernate button presses and lid close events are also handled by logind as described in ACPI events page.

Using sudo

Install sudo, then, as root, add the following to the end of /etc/sudoers using the visudo command. Substitute user for your username and hostname for the machine's hostname.

user hostname =NOPASSWD: /usr/bin/systemctl poweroff,/usr/bin/systemctl halt,/usr/bin/systemctl reboot

Now your user can shutdown with sudo systemctl poweroff, and reboot with sudo systemctl reboot. Users wishing to power down a system can also use or sudo systemctl halt. Use the NOPASSWD: tag only if you do not want to be prompted for your password.

For convenience, you can add these aliases to your user's ~/.bashrc if you have it enabled (or to /etc/bash.bashrc for system-wide settings):

alias reboot="sudo systemctl reboot"
alias poweroff="sudo systemctl poweroff"
alias halt="sudo systemctl halt"

Using acpid

acpid can be used to allow anyone with physical access to cleanly shut down the computer by using this power button.