Run Claude Code Reliably with tmux on Remote Servers
Why combine Claude Code, tmux, and a remote server?
Claude Code can work directly in a project on a remote machine. Pairing it with tmux gives you a persistent terminal session: if your laptop sleeps or your SSH connection drops, the process can keep running on the server. Reconnect later and pick up where you left off.
This setup is useful for long-running coding tasks, development environments with more memory than your laptop, and projects that need tools or files installed on a server. It does not make Claude Code autonomous or remove the need to review its changes. You still control the terminal and should inspect generated code before using it.
1. Connect and prepare the server
Use SSH to sign in to a server where you have permission to work. For example, run ssh username@server-address from your local terminal. On the server, move into your project directory and check that the project is available:
- cd /path/to/project
- git status to see whether there are existing changes
- node --version if your project or Claude Code installation requires Node.js
Install Claude Code using the current instructions from its official documentation, then start it once with claude to confirm the installation works. If your account or organization requires a particular login method, complete that setup before starting a long session.
2. Start a persistent tmux session
Install tmux through your server's package manager if it is not already available. Start a named session with tmux new -s claude-work, then launch Claude Code from the project directory by running claude. Keeping one session per task or project makes it easier to return to the right terminal.
To leave the session running without closing Claude Code, detach from tmux with the keyboard sequence Ctrl-B, followed by D. You can now close SSH. When you reconnect, list sessions with tmux ls and return to the task using tmux attach -t claude-work. If you need a second shell without stopping the running one, create another tmux window with Ctrl-B, followed by C.
3. Configure a compatible API relay
If you use 59API to access Claude models, configure Claude Code to send requests through its API base URL. Set the environment variables in the shell that will launch Claude Code:
- export ANTHROPIC_BASE_URL=https://api.59api.com
- export ANTHROPIC_API_KEY=your_59api_key
Replace the example key with your own. Follow 59API's current account instructions if it specifies a different key format or setup detail. Then run claude inside that same shell. Environment variables set this way are available to programs launched from the shell, including Claude Code in tmux.
For convenience, you can put the base URL in your shell startup file, such as ~/.bashrc or ~/.zshrc. Avoid saving a live API key in a shared file or committing it to Git. On a multi-user server, restrict access to any file containing credentials, and rotate the key if it is exposed. If you start tmux before setting the variables, open a new shell inside tmux or restart the session so Claude Code inherits them.
4. Work, review, and reconnect
Give Claude Code a focused task, such as asking it to inspect a failing test and propose a fix. Review the plan and changes, then run the project's tests in the server environment. Use git diff to inspect edits and commit only the changes you intend to keep. A persistent session preserves the terminal process, not a backup of your project, so use Git and your normal backup practices.
If Claude Code appears frozen after reconnecting, first check whether the tmux session is still active with tmux ls. Attach to the named session rather than starting another Claude Code process in the same project. When finished, exit Claude Code and close the session with tmux kill-session -t claude-work.
Keep API costs predictable
Remote work can make it tempting to leave lengthy tasks running. Check usage and set sensible limits in your API account, and ask for bounded tasks rather than open-ended work. 59API offers pay-as-you-go access to Claude Opus, Sonnet, Haiku, and Fable, as well as GPT models, through a base URL compatible with Claude Code and other supported clients. Its low-cost relay pricing, referral rebate, and access to native official-quality models can make it a practical option without relying on downgraded models. If that fits your workflow, you can sign up with 59API and try it on a small task before moving more work over.
¿Listo para empezar?
Conecta Claude y GPT en minutos a los precios más bajos, sin recortes. Regístrate para obtener tu clave API.
Registro gratis