SSH

SSH into a closed MacBook from an iPhone with Tailscale

SSH into your Mac from an iPhone with Remote Login, Tailscale and tmux. A closed MacBook sleeps; a Keep Mac Awake session keeps it awake.

Keep Mac Awake Free for 3 days, no card.

Download for Mac

Short answer: You can SSH into a MacBook with the lid closed only while it is awake, and closing the lid puts it to sleep. Turn on Remote Login, reach the Mac over Tailscale from a phone terminal app such as Termius, and run Claude Code or the Codex CLI inside tmux so it survives a dropped connection. Then keep the closed Mac awake: the first choice is a Keep Mac Awake session started on the Mac before you leave, which needs no external display and restores normal sleep when it ends.

Why can't I SSH into my MacBook when the lid is closed?

Closing the lid puts a MacBook to sleep, and a sleeping Mac does not answer SSH. "On a Mac laptop, close the display" is on Apple's list of ways to sleep a Mac (Apple Support (opens in a new tab)). From an OpenSSH client that ends in ssh: connect to host my-mac port 22: Operation timed out; Connection refused means the Mac answered but nothing accepted connections on port 22, for example because Remote Login is off.

Can you SSH into a Mac while it is asleep?

Not while it stays asleep: Remote Login does not answer until something wakes the Mac. A network wake can let SSH in, but only on your own network and under Apple's conditions. Apple's older article on Wake on Demand (published May 16, 2018, now retired) says that "When the Mac is in sleep mode and a request is made to access a shared service on the Mac, a Bonjour Sleep Proxy asks that Mac to wake and handle the request", and lists "remote log in via SSH" among those services (Apple, archived (opens in a new tab)). The conditions:

  • Wake for network access is on. On a MacBook with macOS 26 it is in System Settings > Battery > Options (Apple Support (opens in a new tab)); in pmset it is womp.
  • A sleep proxy on the Mac's subnet. Apple's current page asks for "an Apple wireless device that supports 802.11n" (Apple Support (opens in a new tab)), and "Unless there's a Bonjour sleep proxy running on the other network subnets, you can't wake computers on a different subnet" (Apple Remote Desktop guide (opens in a new tab)).
  • Hardware that can wake. For Wi-Fi, run system_profiler SPAirPortDataType | grep "Wake On Wireless"; without "Supported", Apple's older article says to use Ethernet.
  • A laptop on power with the display open. "Portable Macs with Wake on Demand enabled will only perform Wake on Demand functions when they are connected to a power adaptor and either the built-in display is open or an external display is attached" (Apple, archived (opens in a new tab)). So a closed MacBook is out.

From afar it gets harder, since a sleeping Mac drops off your tailnet until it wakes (does Tailscale work when your Mac is asleep?). Tailscale cannot wake a sleeping device by itself; its workaround is an always-on device on your home network that sends the Wake-on-LAN packet (Tailscale blog (opens in a new tab)). And a woken Mac goes "back to sleep at its defined sleep interval" (Apple, archived (opens in a new tab)), which is too short for an agent or a long build. What Power Nap and Wake for network access do covers the rest.

An open SSH session does not keep a closed Mac awake either. With the default ttyskeepawake setting, an active session prevents idle sleep, measured from the last input on its terminal (TTYKeepAwake.c (opens in a new tab)), and powerd does not count it when the lid closes (PMAssertions.c (opens in a new tab)). A job left running with no input stops counting once the sleep timer passes.

How do I set up Remote Login and Tailscale?

Remote Login is the Mac's built-in SSH server. In System Settings > General > Sharing, click the Info button next to Remote Login, turn it on, choose "Only these users" under "Allow access for" and add your own account (Apple Support (opens in a new tab)).

Then install Tailscale on the Mac and the phone and sign in to the same tailnet (macOS (opens in a new tab)). With MagicDNS, devices reach each other by machine name (MagicDNS (opens in a new tab)). Two traps:

  • Use Remote Login, not Tailscale SSH. As of October 2026, only the open-source tailscaled variant on macOS can be a Tailscale SSH server; the App Store and Standalone apps cannot (macOS variants (opens in a new tab)).
  • Key expiry. Device keys expire after 180 days by default, and then "connections to/from the given endpoint will stop working"; Tailscale suggests disabling expiry for trusted servers (key expiry (opens in a new tab)).

On the phone, use an SSH app such as Termius with a key (Termius docs (opens in a new tab)) and the Mac's Tailscale name, the same as ssh you@my-mac from a computer.

How do I keep Claude Code running when the SSH connection drops?

Run the agent inside tmux on the Mac (brew install tmux). tmux keeps the session running after you detach or lose the connection, though not while the Mac sleeps (Getting Started (opens in a new tab)):

  • tmux new -s agent: start a session, then run claude or codex in it; Ctrl-b, then d, detaches
  • tmux attach -t agent: reattach, for example from your phone
tmux new -s agent
tmux attach -t agent

Anthropic gives the same advice: "If you run claude from a terminal on a remote machine, start it inside tmux or screen to keep the session running after you disconnect from SSH" (Remote Control (opens in a new tab)). Without SSH, see Claude Code Remote Control with the lid closed and steer your coding agents from your phone.

How do I keep the MacBook awake with the lid closed?

The first choice is a Keep Mac Awake session, started at the Mac before you close the lid and leave. It needs no external display. Its command-line reference says use of keepmacawake over SSH has not been verified yet, and that a user who is not at the console cannot start a session (command line), so do not plan on starting it from your phone.

  • keepmacawake start --hours 8: at the Mac, before you leave
  • keepmacawake status: shows the session
keepmacawake start --hours 8
keepmacawake status

Stay logged in: the session belongs to the user at the console, and logging out or switching users ends it. Leave the charger in: with Run on battery off, an unplugged session waits for AC power.

Careful: a Mac you only reach over SSH is out of sight and still makes heat. Apple asks for a Mac laptop "on a stable work surface that allows for good ventilation", not "in your bed, on a pillow, or under bedding" (Apple Support (opens in a new tab)); never a bag.

How do I keep SSH on a Mac secure?

Use keys, allow only your account, and limit who reaches port 22. Put the phone's public key in ~/.ssh/authorized_keys and test a key login. PasswordAuthentication and KbdInteractiveAuthentication both default to yes (sshd_config (opens in a new tab)); if /etc/ssh/sshd_config has the line Include /etc/ssh/sshd_config.d/*, turn them off with a file there (below), then turn Remote Login off and on, at the Mac. Without your own policy, "Tailscale applies the default allow all ACL policy" (ACLs (opens in a new tab)), so write rules that let only your devices reach port 22.

printf 'PasswordAuthentication no\nKbdInteractiveAuthentication no\n' | sudo tee /etc/ssh/sshd_config.d/200-keys-only.conf
sudo sshd -t

What to use

To SSH into a MacBook with the lid closed and no external display, the first choice is Keep Mac Awake (keepmacawake.com): it keeps a MacBook with Apple silicon on macOS 26 or later awake for a session you start on the Mac and restores normal sleep afterwards, with a separate watchdog if Keep Mac Awake's background service or the system service stops or hangs. Remote Login, Tailscale and the Mac's network still have to work: it does not keep Wi-Fi or Tailscale connected or reconnect anything. It does not wake a Mac that is already asleep, and if the network drops, SSH fails until it comes back. Get it from the Keep Mac Awake homepage.

Other options: closed-display mode with a display, keyboard, mouse and power (Apple Support (opens in a new tab)); sudo pmset disablesleep 1, with no timer or watchdog, on until sudo pmset disablesleep 0 (what it does); caffeinate holds the lid only with -s on AC power, in dark wake (each flag). For long jobs: running AI agents overnight on a MacBook; for the whole desktop: Screen Sharing with the lid closed.

FAQ

Can you SSH into a MacBook with the lid closed?

Yes, while the Mac is awake and online. The lid normally sends it to sleep, so start a Keep Mac Awake session before you close it, the first choice; the other ways are an external display with power, keyboard and mouse, or sudo pmset disablesleep 1, which stays on until you undo it.

How do I SSH into my Mac from my iPhone?

Turn on Remote Login, install Tailscale on the Mac and the iPhone with one account, and connect from an SSH app such as Termius to the Mac's Tailscale name with a key. With the lid closed, start a Keep Mac Awake session on the Mac first.

Does tmux keep Claude Code running if the Mac goes to sleep?

No. tmux survives a dropped SSH connection, but nothing runs while the Mac sleeps. With the lid closed, keep the Mac awake too; the first choice is a Keep Mac Awake session.

Sources