I want to have the computer log me in so all my login items can get started, but I need it to be secure, aka it still needs to ask me for my password before I can actually use the computer.
4 Answers
I outlined how to do this at https://www.engadget.com/2011-03-07-terminally-geeky-use-automatic-login-more-securely.html but I will summarize it for you:
- set your account to autologin
- create a login item (using launchd) which calls:
/System/Library/CoreServices/Menu\ Extras/User.menu/Contents/Resources/CGSession -suspend
NOTE: that command should be ONE LONG LINE not two lines with a line break/line feed.
A more thorough explanation is available at TUAW, so I won't repeat it all here, because those are the relevant details.
What it does:
The idea is very simple, your account is set to auto-login, so all of your login items will run, however, one of those login items (the CGSession command listed above) will tell the loginwindow to appear. If you have used Fast User Switching before, you will be familiar with how it works.
"But what if someone holds down the shift key to stop autolaunching?"
In my testing, that will also prevent the user from being automatically logged in.
Is this 100% as safe as not using auto-login?
Probably not, but I'd consider it close to 99.999% as safe.
That said, use at your own risk, etc.
-
Addendum: I use this technique on my office computer in case I need to reboot it while out of the office via ssh. HOWEVER if you use FileVault in Lion, this will not work because the user needs to manually enter their password before the computer will boot.– TJ LuomaJan 16, 2012 at 18:28
-
I'm not familiar with using
launchd
. Could you please explain in your answer how to create the login item? I tried to figure this out using the link, but I did not really get it.– gentmattJan 16, 2012 at 18:47 -
@gentmatt That might be best asked as a separate question, but the short answer is "use Lingon" <itunes.apple.com/us/app/lingon/id411211026?mt=12>. Note that there is a separate version #3 which has fewer features, but version 2 will probably not be updated due to Apple's sandboxing requirements.– TJ LuomaJan 17, 2012 at 5:21
-
The Lingon link is dead. A good one is apps.apple.com/us/app/lingon-3/id450201424?mt=12– CalionMar 9, 2021 at 22:28
Since not everybody is familiar with creating a launchd / launchctl script, here's how you do it.
This explains step two of @TJ Luomas' answer and provides another option for locking your computer based on @jordanbtucker's answer:
Set your account to auto login
Open TextEdit and create a new file containing the following
#!/bin/bash /System/Library/CoreServices/Menu\ Extras/User.menu/Contents/Resources/CGSession -suspend
- Another option is to put the screen to sleep combined with requiring a password every time your computer wakes from sleep, which keeps more processes enabled
To use this alternate method:
- Go to System Preferences > Security & Privacy > General and enable Require password
immediately
after sleep or screen saver begins Use this script instead
#!/bin/bash pmset displaysleepnow
- Go to System Preferences > Security & Privacy > General and enable Require password
Name the file
suspend.sh
and save it to your User folder (~
)- Starting in Catalina, launchd may not be able to access the script from your User folder due to new privacy restrictions; I have confirmed it works from your Applications folder without requiring any privilege changes
Open TextEdit and create a new file with the following content
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>Label</key> <string>com.fl034.suspendAfterLogin</string> <key>Program</key> <string>/Users/fl034/suspend.sh</string> <key>RunAtLoad</key> <true/> </dict> </plist>
- Replace
fl034
with your username - Ensure the
Program
path matches the location you chose in step 3
- Replace
Save the file to
~/Library/LaunchAgents/com.fl034.suspendAfterLogin.plist
, replacingfl034
with your usernameRestart your Mac to see if it works
WARNING: This script leads to problems with App Store.app in macOS Catalina
-
1Hey @Jawann thanks for editing my answer. With your alternative solution (display sleep instead of suspend session) it works very neatly. When the computer starts after the Apple Logo the screen just goes black directly. Before you would have seen the screen for a second. Also App Store.app works as expected– heyfrankApr 2, 2020 at 15:18
-
1CGSession has since moved or been removed. Alternatively the following command should work:
osascript -e 'tell application "System Events" to keystroke "q" using {control down, command down}'
Jan 9, 2021 at 23:49
In System preferences set the screen saver so that it is on and required a password to start
I left the Disable automatic login ticked really as a mistake as to answer the user exactly this should be unticked.
However I would strongly suggest that it is ticked otherwise any could reboot the computer and get into a logged in session as the user.
Such behavior is not possible to be set in the System Preferences. Login Items are user specific. They aren't loaded unless you select a user and enter the password.
You would need to e.g. write a script that locks the screen as soon as you are logged in and set this script to be excecuted as the first thing after login.
-
FYI - That's precisely what my suggest (which I was writing while you posted yours :-) does.– TJ LuomaJan 16, 2012 at 18:26
-