📜  linux shutdown no password - Shell-Bash (1)

📅  最后修改于: 2023-12-03 15:17:21.107000             🧑  作者: Mango

Linux Shutdown Without Password

When you are trying to shutdown a Linux machine, it may prompt you to enter a password for the root user. However, for certain situations, you may want to shut down the Linux machine without entering a password.

Here are two methods to achieve this:

Method 1: Edit sudoers File
  1. Open the sudoers file in the terminal with the command:
sudo visudo
  1. Add the following line to the end of the file:
username ALL=(ALL) NOPASSWD: /sbin/shutdown

Replace "username" with your own username.

  1. Save the file and exit.

Now you should be able to run the shutdown command without entering a password.

Method 2: Create a Systemd Unit
  1. Create a new systemd unit with the following command:
sudo nano /etc/systemd/system/shutdown.service
  1. Insert the following lines to the file:
[Unit]
Description=Shutdown the machine
DefaultDependencies=no
Before=final.target

[Service]
ExecStart=/sbin/shutdown -h now

[Install]
WantedBy=final.target
  1. Save the file and exit.

  2. Reload the systemd daemon with the following command:

sudo systemctl daemon-reload
  1. Start the new service:
sudo systemctl enable shutdown.service

Now you can use the following command to shut down the machine without entering a password:

sudo systemctl start shutdown.service

Both methods achieve the same result, but the second method is more secure and recommended for production environments.

Note: Be cautious when following these steps, as any incorrect changes made to the system can result in damages.