Mac Terminal: Avoid password

Solution 1:

It could be configured to make sudo skip password prompt for specific command with /etc/sudoers.

# modify /etc/sudoers with visudo
# never open it directly with an editor
sudo visudo

Append the following conf into the file,

Cmnd_Alias SHUTDOWN_NOW = /sbin/shutdown -h now
%admin ALL=(root) NOPASSWD: SHUTDOWN_NOW

Done.

Solution 2:

You can avoid the issue entirely by taking another approach to shutting down the computer:

osascript -e 'tell application "System Events" to shut down'

This will also prompt you to save any unsaved documents, just as if you'd selected "Shut Down" from the Apple menu.

Solution 3:

The common way to do this is to allow your user to run the shutdown command without entering a password.

You that by opening Terminal.app and then run the following command:

sudo visudo 

This gives you an editor where you can edit the sudoers configuration file.

Add the following line at the bottom:

jonathan ALL=(ALL) /sbin/shutdown 

After saving, you should be able to run shutdown with your user without having to enter a password.

Note that you should replace "jonathan" with your actual username.