SSH to a Mac from a phone or tablet, Remote Login and sleep
To SSH into a Mac, turn on Remote Login in Sharing settings and keep the Mac awake. A sleeping Mac or a closed laptop lid stops answering SSH connections.
A Mac has an SSH server built in, called Remote Login. Turn it on in Sharing settings, use the address the Mac shows you, and keep the Mac awake while you need it.
Turn on Remote Login
- On the Mac, choose Apple menu, then System Settings. Click General, then Sharing.
- Turn on Remote Login.
- Next to Allow access for, choose All users, or choose Only these users and add the accounts that may log in.
Below the "Remote Login: On" line, the Sharing settings show the exact SSH command for this Mac, with the user name and address to use. Apple's example is ssh [email protected]: the user name is your user name on the Mac, and the host is the Mac's IP address or host name. Remote Login also allows SFTP file transfer.
Why it stops answering
- The Mac is asleep. A sleeping Mac usually does not answer, so the client waits and then reports a timeout. On a Mac laptop, closing the display puts it to sleep.
- Remote Login is off. Nothing listens on port 22, so the client reports that the connection was refused.
- The address changed. A Mac that gets its address from the router can get a different one later. Check the address in Sharing settings again.
See Connection abort, reset and timed-out errors for what each client error means.
Keep the Mac awake
Apple puts the sleep settings in different places for laptops and desktops:
- Mac laptop: System Settings, Battery, Options. Turn on Prevent automatic sleeping on power adapter when the display is off. This applies while the Mac is plugged in.
- Mac desktop: System Settings, Energy. Turn on Prevent automatic sleeping when the display is off.
These settings stop automatic sleep when the display turns off. Closing a laptop's display still puts it to sleep.
Both places also have Wake for network access. Apple describes it as waking the Mac when another computer uses its shared resources, and notes that it needs a compatible Apple wireless device. Do not count on it for SSH: keeping the Mac awake is the dependable option.
A session that should survive
An SSH session ends when the Mac sleeps or the network drops. To keep a program running, start it inside tmux on the Mac and attach again later. See Keep your work after a disconnect with tmux.
Related
- SSH: how it works
- Known hosts and fingerprints
- Authorized keys and permissions
- SSH keep-alives and idle timeouts
- Mosh to a Mac: Nothing received from server, macOS firewall