Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Shutdown via command script / Sudoers question

Hello MacGeniuses.


Having tried to figure out the solution for myself, I've only managed to get to a partial solution.


Primary Issue:

I want to use a desktop alias to shut down the computer. The reason is because I wish to to run another command first without having to open a terminal every time.


And I also don't want to enter a password every time.


Based on my homework, I created a text file, gave it a .command extension, did a chmod +x, and then tried to use to to run the following commands, both starting with a space to keep them out of the command history:


echo password | sudo nvram SystemAudioVolume=%80

echo password | sudo shutdown -h now


The commands work fine if and only if I've already done a sudo within terminal that session. If I haven't then I'm prompted for my password.


I even tried adding myself to the sudoers file (in the last line) but that didn't help:


Steve ALL=(ALL) NOPASSWD: /sbin/poweroff, /sbinreboot, /sbin/shutdown, /usr/sbin/nvram


Please know that when it comes to Linux-type stuff I'm just a monkey-see, monkey-do person, so if you give me good advice I may come back with a response like, "What's a power cord?" Please be as gentle as you can.


Secondary issue:

Every time I open terminal I get a list of stuff like shown below (there were several, I just cut and paste one of them). Anyone know how to stop that?


[Restored Jul 22, 2016, 20:54:32]

Last login: Fri Jul 22 20:53:18 on ttys000

Restored session: Fri Jul 22 20:53:56 EDT 2016

Steves-iMac:Desktop Steve$ chmod +x Shutdown.command

Steves-iMac:Desktop Steve$ exit

logout

Saving session...

...saving history...truncating history files...

...completed.


[Process completed]

iMac (Retina 5K, 27-inch, Late 2015), El Capitan

Posted on Jul 24, 2016 2:11 PM

Reply
Question marked as Best reply

Posted on Jul 24, 2016 4:05 PM

This is a way. It is NOT the only way.

Google "passwordles ssh"

Setup your account for passwordless ssh logins.


Now take the ~/.ssh/id_*.pub file and put the file's contents into the 'root's /var/root/.ssh/authorized_keys file (you will need to use sudo to make this change).


The id_*.pub file will most likely be id_rsa.pub or id_dsa.pub, but it could be slightly different.


Now you can

ssh root@localhost shutdown -h now

and you will not need a password.


NOTE: Make sure that your id_* (not the .pub file), which is your private key NEVER leaves your control and copies are not made where someone else can get a hold of it. If someone else gets your id_* file, they can login to your system with out a password.


NOTE 2: There are ways to put a passphrase on your ssh keys, and store them in your keychian. Google: "ssh passphrase in keychain". This would be more secure than no passphrase, but you still have to keep control of your keychain file. It is always something 🙂

9 replies
Question marked as Best reply

Jul 24, 2016 4:05 PM in response to Steev3

This is a way. It is NOT the only way.

Google "passwordles ssh"

Setup your account for passwordless ssh logins.


Now take the ~/.ssh/id_*.pub file and put the file's contents into the 'root's /var/root/.ssh/authorized_keys file (you will need to use sudo to make this change).


The id_*.pub file will most likely be id_rsa.pub or id_dsa.pub, but it could be slightly different.


Now you can

ssh root@localhost shutdown -h now

and you will not need a password.


NOTE: Make sure that your id_* (not the .pub file), which is your private key NEVER leaves your control and copies are not made where someone else can get a hold of it. If someone else gets your id_* file, they can login to your system with out a password.


NOTE 2: There are ways to put a passphrase on your ssh keys, and store them in your keychian. Google: "ssh passphrase in keychain". This would be more secure than no passphrase, but you still have to keep control of your keychain file. It is always something 🙂

Jul 24, 2016 4:05 PM in response to Steev3

Because ssh gives you the ability to create ssh-keys that allow logging into an account WITHOUT a password, and one of the accounts can be 'root' which is exactly what sudo is doing for you, and you do not need to expose your password in plain text stored in a file that anyone that gains access to your system or a backup of your system can read.


But if you want expose your password, you could try the sudo -S option.

Jul 24, 2016 4:12 PM in response to Steev3

ssh allows you to login to another account.

'localhost' is YOUR system

'root' is the account with ultimate privilege on your Mac. When you issue the sudo command you are exeucting the command as 'root'

so

ssh root@localhost some_command ....

will login to the root account, and execute the specified command as 'root'. The same as when you use sudo.


The Main advantage is that ssh can have private and public keys that allow you to login WITHOUT a password, were as sudo does NOT have this feature. ssh can be scripted, and if you have setup the public and private keys as suggested, then you will never be prompted for a password, and you will never need to store your password as plain text in a file. In fact you will not even need to update the ssh keys if you decide to change your login password, as the ssh keys are not tied to your login password.


Also putting your .pub key file contents into the root's .ssh/authorized_keys file does NOT require to enable root, but it will still work. So no one will be able to use or guess the password for the root account because it will never be enabled, but ssh into root via ssh-keys will still be possible (I use this trick for myself to perform backups using some Unix tools). Carbon Copy Cloner uses ssh keys to perform remote backups (another place I take advantage of ssh keys and root access).

Jul 24, 2016 4:20 PM in response to Steev3

Are you always using this script to shutdown? If that is the case it may make more sense to create a launchd job to create the nvram setting at shutdown. That way you just forget about it after setting it up. Stackoverflow has some posts on that topic.


I think there may be apps for it…

http://auto-mute.com/screenshots/

https://itunes.apple.com/us/app/sleepmute/id421822575?mt=12

Startninja & StartupSound.prefPane are others that are mentioned. I vaguely recall a different app so search around if you want to use a different approach, disabling SIP, tweaking sudoers, enabling ssh seems a bit overkill to me.


Personally I find setting the NVRAM variable once is enough to stick, perhaps that is a function of being on a desktop using multiple audio interfaces?

Jul 27, 2016 6:45 AM in response to Drew Reece

First: APOLOGIES to all. I marked one or two initial answers as helpful but am not allowed to mark any more. They were ALL helpful.


The actual solution I adopted:


1. Create the "tell application "Finder" to shut down" apple script, compile and save it as an app to my desktop, and give it an icon cooler than any of yours </smirkmode>


2. Install the free Volume Reset app from the app store.


==============supplemental freebee question==============

I have dirty (verbose) boot turned on, but with an SSD my machine boots really fast (even faster than Sheldon Cooper's). Any way to slow down the scroll?

Shutdown via command script / Sudoers question

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.