How to run Claude Code on a remote server and keep it running after you disconnect
Run the agent inside a persistent session on the server, not in your SSH session. The quickest way is spaces --remote you@server: it installs Spaces on the server if it is missing, starts it, and opens a terminal client over SSH. Agents you start there keep running when you close the laptop; reconnect with the same command.
Why the agent dies when you disconnect
If you ssh in and run claude, the agent is a child of that SSH session. When the connection drops, or the laptop sleeps, the shell gets a hangup and the agent goes with it. Anything long-running needs to be owned by something on the server that outlives your connection.
The one-command way
On your laptop, with Node.js installed:
npm install -g @jlongo78/agent-spaces
spaces --remote you@server
What happens:
- Spaces checks the server over SSH. If Spaces is not installed there, or is older than your copy, it offers to install or update it (add
--yesto skip the question). It installs into~/.localwhen npm's global folder would need root. - It starts the Spaces server on the remote machine, listening on loopback only, and opens an SSH tunnel to it. Nothing is exposed beyond your SSH login.
- The terminal client opens on your laptop, showing the spaces on the server. Press ctrl+k, choose New agent, pick Claude Code and a folder.
Claude Code now runs on the server. Close the laptop whenever you like; the agent keeps going. To leave the client without stopping anything, press ctrl+space then d (detach). To come back, run spaces --remote you@server again.
Keeping it running across reboots
On the server, install Spaces as a service once so it starts at login:
spaces service install
On Linux that is a systemd user service; on macOS a launchd agent; on Windows a scheduled task. After a reboot the server comes back, every space is still there, and panes stopped by the reboot start again when you open their space. Claude Code panes resume the conversation they were in. See resuming after a reboot.
Seeing it from a browser or a phone
The same sessions are available in a browser through the same tunnel:
spaces open --remote you@server
It opens the web app on your laptop, signed in, and keeps the tunnel up until you press Ctrl+C. For a phone, see checking on agents from your phone.
If you asked for a password
spaces --remote makes up to three SSH connections, so a password login would ask three times. When the server asks for a password, Spaces offers to set up SSH key sign-in for you (it uses your default key, or creates ~/.ssh/id_ed25519, and adds it to the server's authorized_keys). Accept once and you are not asked again.
Without Spaces
tmux or screen keep a terminal alive the same way; what they do not do is tell you which agent is waiting on you, show the sessions in a browser, or resume a Claude Code conversation after a reboot. If you already use tmux you can run spaces inside it, but you do not need to.
Get started
npm install -g @jlongo78/agent-spaces
spaces
Open source, Apache-2.0, no telemetry. Source on GitHub · every feature.