📅  最后修改于: 2023-12-03 15:17:21.107000             🧑  作者: Mango
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:
sudo visudo
username ALL=(ALL) NOPASSWD: /sbin/shutdown
Replace "username" with your own username.
Now you should be able to run the shutdown command without entering a password.
sudo nano /etc/systemd/system/shutdown.service
[Unit]
Description=Shutdown the machine
DefaultDependencies=no
Before=final.target
[Service]
ExecStart=/sbin/shutdown -h now
[Install]
WantedBy=final.target
Save the file and exit.
Reload the systemd daemon with the following command:
sudo systemctl daemon-reload
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.