How to Lock Your Mac Screen from the Command Line (And Why You Should)

If you use macOS and spend a lot of time in the terminal, you might be surprised to learn that you can lock your screen with a simple Bash…

How to Lock Your Mac Screen from the Command Line (And Why You Should)

If you use macOS and spend a lot of time in the terminal, you might be surprised to learn that you can lock your screen with a simple Bash script — no mouse required.

This small trick is more useful than you think: it helps you secure your machine remotely, integrate locking into automated workflows, and streamline your work habits. In this article, I’ll show you exactly how to set this up and explore practical cases when it comes in handy.

Why Lock the Screen via Command Line?

Most people lock their Mac using:

  • The Apple menu ( > Lock Screen),
  • A keyboard shortcut (Control + Command + Q),
  • Or simply closing the lid.

But there are many scenarios where a terminal command is faster, more versatile, or the only option:

Remote access:
Forgot to lock your Mac? SSH into it from another device and lock it instantly.

Automation:
Lock the screen automatically after a scheduled backup, at the end of the workday, or after a specific script runs.

Minimal distraction:
Stay in your terminal environment (iTerm, tmux, VS Code terminal) without switching to other apps or menus.

Custom workflows:
Integrate screen locking into tools like Alfred, Raycast, cron jobs, or your own scripts.

Extra security:
If you often work in shared spaces, a quick lock command is faster and more reliable.

How to Create the Lock Screen Script

Here’s a step-by-step guide to create your own lockscreen command:

1️⃣ Create a new Bash script

Open your terminal and run:

nano lockscreen

Paste the following content:

#!/usr/bin/env bash 
 
# Lock the screen using AppleScript 
osascript -e 'tell application "System Events" to keystroke "q" using {control down, command down}'

This AppleScript simulates the same keyboard shortcut you’d press manually.

2️⃣ Make the script executable

Save and exit (CTRL + O, Enter, CTRL + X), then run:

chmod +x lockscreen

3️⃣ Move it into your PATH

So you can run it anywhere:

sudo mv lockscreen /usr/local/bin/

4️⃣ Test it

Type:

lockscreen

Your Mac should lock immediately.

Use Cases: When to Use This Script

Let’s look at real-world situations when this simple trick is a lifesaver:

🚀 1. Remote Locking

SSH into your Mac while away and run:

lockscreen

Perfect if you left your machine open.

🗓️ 2. Scheduling via Cron

Lock your screen every day at 6 PM automatically:

0 18 * * * /usr/local/bin/lockscreen

🛠️ 3. Post-Script Locking

Secure your Mac after running a long job:

./run_backups.sh && lockscreen

💼 5. Shared or Public Spaces

If you work in libraries or coworking spaces, this is the fastest way to secure your machine without lifting your hands off the keyboard.


✨ Wrapping Up

With this tiny script, you can make your Mac security:

  • More automated
  • More flexible
  • More convenient

It’s a simple hack, but you’ll wonder how you lived without it.

Try it out today, and let your terminal be your security sidekick.


Did you find this helpful?
Clap 👏, share, or comment below — I’d love to hear how you use terminal automation in your Mac workflow!