How to run Claude Code on a remote server over SSH, and keep it running
Last updated
Claude Code on a remote server, in five steps
1 / 5
Create a user for the agent
Run Claude Code as a normal user, not root. A user without sudo can't change the system even when the agent runs every command it likes; it can still read and send whatever that user can read.
2 / 5
Install Claude Code
Anthropic's recommended install, as the agent user. It needs no Node.js, and it "automatically update[s] in the background".
3 / 5
Sign in from a server without a browser
The server can't open a browser, so open the login URL Claude Code shows in your laptop's browser. Or create a key in the Claude Console, and give the server its own key so you can revoke it alone.
4 / 5
Keep the session alive after you disconnect
An SSH disconnect kills the shell and everything started from it, Claude Code included. tmux keeps a session running on the server with no client attached.
5 / 5
Run tasks headless
claude -p (print mode) runs one prompt, prints the result and exits, which suits cron jobs, scripts and tasks you don't need to watch.
To run Claude Code on a remote server, SSH into any Linux machine with 4 GB of RAM or more, install Claude Code as a normal user, sign in once with /login inside the session (or set an API key), and start it inside tmux. Detach, close your laptop, and the agent keeps working; SSH back in and reattach to pick up where it is. For unattended jobs, claude -p runs one task and exits. For several agents at once, give each its own VM rather than stacking them on one server.
| You want to | Do this |
|---|---|
| Install | curl -fsSL https://claude.ai/install.sh | bash, as the user who will run it |
| Sign in | /login in the session: open the printed URL on your laptop, paste the code back |
| Start a session that survives SSH | tmux new -A -s claude -c ~/your-repo, then claude |
| Leave it running | Ctrl-b then d |
| Come back from your laptop | ssh -t you@server tmux new -A -s claude |
| Reopen the last conversation after a reboot | claude --continue in the same directory |
| Run one task without watching | claude -p "…" with a log file |
Every Anthropic doc quoted here was read on 5 November 2026. The tmux and systemd steps were run that day on Ubuntu 26.04 with tmux 3.6 and systemd 259, with a stand-in script in place of claude so no tokens were spent. The cost of a server, and where a VPS beats a per-second VM, is on Claude Code on a VPS or a microVM; this page is the setup.
What kind of server does Claude Code need?
Anthropic lists the floor: Ubuntu 20.04+, Debian 10+ or Alpine Linux 3.19+, "4 GB+ RAM, x64 or ARM64 processor", and an internet connection. Any VPS, cloud VM, or machine under your desk that meets it works, with no graphical session: Claude Code is a terminal program, and the sign-in works from another machine's browser (below).
The 4 GB is for Claude Code itself. Your tests, dev server, database and Docker containers come on top, so size for the project, not for the agent.
Step 1: create a user for the agent
Run Claude Code as a normal user, not root. If you plan to skip permission prompts, you have to: on Linux, Claude Code refuses to start in bypass mode as root or under sudo and prints --dangerously-skip-permissions cannot be used with root/sudo privileges for security reasons.
On a fresh server reached as root with an SSH key:
useradd -m -s /bin/bash agent
install -d -m 700 -o agent -g agent /home/agent/.ssh
install -m 600 -o agent -g agent /root/.ssh/authorized_keys /home/agent/.ssh/authorized_keys
Then reconnect as ssh agent@server. A user without sudo can't change the system even when the agent runs every command it likes; it can still read and send whatever that user can read.
Lock down SSH before the agent moves in
Still as root, allow key logins only, and let nothing else in:
cat > /etc/ssh/sshd_config.d/10-hardening.conf <<'EOF'
PasswordAuthentication no
KbdInteractiveAuthentication no
PermitRootLogin prohibit-password
EOF
sshd -t && systemctl reload ssh
ufw allow 22/tcp && ufw default deny incoming && ufw --force enable
sshd keeps "the first obtained value" of each keyword, and Ubuntu includes sshd_config.d/*.conf at the top of its config, so a 10- file wins over a later one that turns passwords back on. sshd -t checks the syntax before the reload. --force skips ufw's confirmation prompt; the SSH rule comes first, so your session stays open. Test a new login before you close the current one. If your provider has a network firewall in its console, set the same rule there. For automatic security updates, apt install unattended-upgrades.
Step 2: install Claude Code
Anthropic's recommended install, as the agent user:
curl -fsSL https://claude.ai/install.sh | bash
claude --version
The native installer puts the launcher at ~/.local/bin/claude and the versions under ~/.local/share/claude/versions/. It needs no Node.js, and it "automatically update[s] in the background". If claude isn't found, open a new login shell so ~/.local/bin is on your PATH.
Two alternatives the docs give:
- The apt repository (also dnf and apk), signed by Anthropic. Its
stablechannel "serves a version that is typically about one week old", and it never updates itself:sudo apt update && sudo apt upgrade claude-code. - npm:
npm install -g @anthropic-ai/claude-code. Anthropic says not to usesudo npm install -g("can lead to permission issues and security risks").
Step 3: sign in from a server without a browser
You have two clean ways to authenticate a remote Claude Code, and one shortcut to avoid.
With your Claude subscription: /login in the session
Start claude and pick your Claude account, or type /login at the prompt. The server can't open a browser, so open the login URL Claude Code shows in your laptop's browser (press c to copy it). Anthropic's docs: "If your browser shows a login code instead of redirecting back after you sign in, paste it into the terminal at the Paste code here if prompted prompt."
You can also sign in from the shell before the first session with claude auth login, and check the result with claude auth status --text. On Linux the login is stored in ~/.claude/.credentials.json with mode 0600, readable by the agent user only. /logout signs out.
This route needs a plan that includes Claude Code (Pro, Max, Ultra, Team or Enterprise), and usage counts against that plan's limits. It is also the only route that works with Remote Control, which doesn't accept API keys.
With an API key from the Claude Console
Create a key in the Claude Console, ideally in a workspace with a spend limit, and give the server its own key so you can revoke it alone. Store it in a file only agent can read, so it stays out of your shell history:
install -m 600 /dev/null ~/.claude.env
vi ~/.claude.env # one line: export ANTHROPIC_API_KEY=sk-ant-...
echo '[ -f ~/.claude.env ] && . ~/.claude.env' >> ~/.bashrc
. ~/.claude.env
In an interactive session, Claude Code asks once whether to use the key and remembers the answer. In -p mode, "the key is always used when present". Usage is billed per token to the key's organization. claude auth login --console is the other way to sign in for API billing.
Don't copy a subscription token onto the server
Several guides that rank for this query tell you to mint a long-lived token tied to your subscription on your laptop, then export it on the server. Don't. You don't need it: the sign-in above works over SSH. Anthropic documents that token for "CI pipelines and scripts where browser login isn't available", which a server you SSH into is not. It is valid for a year, it sits in an environment variable that every process the agent starts can read and print, and it "can only make model requests", so Remote Control doesn't work with it. On a server, sign in with /login in the session, or use an API key from your organization's Console.
Whichever credential you use, the agent can read it. Treat the server as holding that credential, and revoke it if something on the server goes wrong.
Three ways to sign in on a server: two to use, one to avoid
| Credential | Use it? | What it means on a server |
|---|---|---|
| /login in the session, with your Claude subscription | Strength: Yes | Usage counts against the plan's limits; the only route that works with Remote Control |
| An API key from the Claude Console | Strength: Yes | Billed per token; give the server its own key so you can revoke it alone |
| A long-lived subscription token, minted on your laptop | Gap: Don't | Valid for a year, in an environment variable every process the agent starts can read |
Step 4: keep the session alive after you disconnect
An SSH disconnect kills the shell and everything started from it, Claude Code included. tmux keeps a session running on the server with no client attached:
tmux new -A -s claude -c ~/your-repo
claude
-A attaches to the claude session if it exists and creates it otherwise, so the same command works the first time and every time after. Detach with Ctrl-b then d. From your laptop, reattach in one line:
ssh -t agent@server tmux new -A -s claude
Inside tmux, Ctrl+B is both the tmux prefix and Claude Code's "move to background" key: press it twice to reach Claude Code. Shift+Enter, notifications and mouse scrolling need a few tmux settings, listed in Claude Code in tmux.
Reopen the conversation after a reboot
tmux doesn't survive a reboot, but Claude Code's conversation history does: it is on disk. In the same directory, claude --continue (or claude -c) loads "the most recent conversation in the current directory", and claude --resume opens a picker. Name a session at start with claude -n "auth-refactor" and come back to it with claude --resume auth-refactor.
Start the tmux session at boot with systemd
To have the session waiting for you after every reboot, let systemd start it. As root, create /etc/systemd/system/claude-tmux.service:
[Unit]
Description=Claude Code in a tmux session
After=network-online.target
Wants=network-online.target
[Service]
Type=forking
User=agent
ExecStart=/usr/bin/tmux new-session -d -s claude -c /home/agent/your-repo /bin/bash -lc 'claude; exec bash'
ExecStop=/usr/bin/tmux kill-session -t claude
Restart=on-failure
[Install]
WantedBy=multi-user.target
Then systemctl daemon-reload && systemctl enable --now claude-tmux. The login shell (bash -l) puts ~/.local/bin on the PATH, and exec bash leaves a shell in the window when Claude Code exits, so the session doesn't vanish with it. In our test (with a stand-in script printing where it ran), the unit started the session as agent, systemd restarted it after a kill -9 of the tmux server, and systemctl stop removed it:
$ systemctl is-active claude-tmux
active
$ sudo -u agent tmux capture-pane -p -t claude | grep -v '^$'
stand-in claude, PATH ok, HOME=/home/agent, USER=agent
agent@server:~/your-repo$
$ sudo kill -9 $(systemctl show claude-tmux -p MainPID --value); sleep 2
$ systemctl is-active claude-tmux; sudo -u agent tmux ls
active
claude: 1 windows (created Thu Nov 5 16:06:49 2026)
Attach as usual with ssh -t agent@server tmux new -A -s claude. Claude Code starts a fresh conversation at boot: run /resume in it to reopen the last one. The first start in a new directory stops at the workspace trust question, so attach once and answer it.
Or let Claude Code keep sessions running itself
Claude Code also has background sessions, in research preview: claude --bg "investigate the flaky test" starts one and returns, and claude agents opens the view that lists them. Anthropic says they "don't need any terminal open to keep working", because a separate supervisor process runs them, and that shutting down still stops them. tmux remains the simpler choice when you want to watch the agent's screen.
What survives an SSH disconnect, and a reboot
| How Claude Code runs | An SSH disconnect | A reboot |
|---|---|---|
| claude in a plain SSH shell | Gap: Killed, with the shell | — |
| claude inside tmux | Strength: Keeps running, with no client attached | Gap: tmux doesn't survive a reboot |
| tmux started at boot by systemd | Strength: Keeps running | Partial: The session is waiting for you, with a fresh conversation |
| A background session (claude --bg) | Strength: Needs no terminal open to keep working | Gap: Shutting down still stops it |
| The conversation history | Strength: On disk | Strength: Survives: claude --continue reopens it |
Step 5: run tasks headless with claude -p
claude -p (print mode) runs one prompt, prints the result and exits, which suits cron jobs, scripts and tasks you don't need to watch:
cd ~/your-repo
tmux new -d -s task 'claude -p "Fix the failing tests in src/api, then commit on a new branch" \
--allowedTools "Bash,Read,Edit" --max-budget-usd 5 2>&1 | tee ~/task.log'
tail -f ~/task.log
What the docs say you need to know before scripting it:
- Sign in first.
/loginisn't available in-pmode. With no credential, our run printedNot logged in · Please run /loginand exited with code 1. Sign in interactively once, or setANTHROPIC_API_KEY. - Permissions. Since v2.1.302, a
-prun with no permission mode set starts in Manual mode, where "a call that needs approval is denied". List the tools it may use with--allowedTools, or set a mode such as--permission-mode acceptEdits, or--permission-mode autowith--permission-prompts nonefor unattended runs. - Trust. A
-prun "shows no workspace trust dialog" and still runs the repository's hooks and.mcp.jsonservers. Only point it at repositories you trust, or add--bare, which skips them.--bare"never reads OAuth credentials", so it needs an API key. - Output and cost.
--output-format jsonreturns the result withtotal_cost_usd, a client-side estimate.--max-budget-usdstops a run once that estimate reaches the cap, and spend can pass it, so leave headroom. - Exit code. "Claude Code exits with code 0 on success and a non-zero code when the run fails", so a script can branch on it.
Can you drive it from your phone?
Yes, with a subscription login. Start claude remote-control in the server's tmux session (server mode), or claude --remote-control for an interactive session that is also reachable remotely, and the session appears at claude.ai/code and in the Claude app. The code still runs on your server: Anthropic says "your computer has to stay on and the claude process has to keep running", which tmux handles. Requirements: Pro, Max, Ultra, Team or Enterprise ("API keys are not supported"), and on Team and Enterprise an Owner has to turn it on.
How do you run several agents on remote servers?
One server can hold several sessions: one tmux window per agent, each in its own git worktree, as in how to run Claude Code agents in parallel. That stops two agents from editing the same files. It doesn't stop them from sharing everything else: one kernel, one network, one user, the same credential and the same port 3000. An agent that runs a bad command, or follows instructions planted in a README, reaches the other agents' work and secrets.
Past two or three agents, or as soon as you skip permissions on code you don't fully trust, give each agent its own VM: its own kernel, disk and ports, deleted when the task is done. The setup above doesn't change, you just run it once per VM. Choosing a Claude Code sandbox compares that with the built-in /sandbox, containers and dev containers; running --dangerously-skip-permissions safely tests which layer blocks what; microVM vs container explains the kernel boundary. If you'd rather not run any server, Anthropic's own cloud sessions are compared with a VM of your own in Claude Code in the cloud.
Several agents: what they share on one server, and with a VM each
One server, several agents
One VM per agent
One server, several agents One git worktree per agent
One tmux window per agent, each in its own git worktree. That stops two agents from editing the same files.
One server, several agents One user, the same credential
An agent that runs a bad command, or follows instructions planted in a README, reaches the other agents' work and secrets.
One server, several agents One network, the same port 3000
It doesn't stop them from sharing everything else: one kernel, one network, one user, the same credential and the same port 3000.
One server, several agents One kernel
Past two or three agents, or as soon as you skip permissions on code you don't fully trust, give each agent its own VM.
One VM per agent One agent, the same setup
The setup above doesn't change, you just run it once per VM.
One VM per agent Its own kernel, disk and ports
Give each agent its own VM: its own kernel, disk and ports, deleted when the task is done.
Where flockvm fits
flockvm runs one Firecracker microVM per coding agent, in France, on one OVHcloud server. It does steps 1, 2 and 4 for you: on the default Agent image (Ubuntu 26.04), Claude Code, tmux, Git, Docker and Playwright are installed, and you SSH in as user, not root, so the bypass flag is accepted. You still sign in yourself, with /login in the VM or an API key.
flockvm vm create claude-api-fix --size default --wait
flockvm vm ssh claude-api-fix
tmux new -A -s claude
claude
What it adds to a plain server: a VM per agent, billed per second ($0.016 an hour for small, 2 vCPU / 4 GB; $0.032 for default, 4 vCPU / 8 GB), and a pause that writes the VM's memory to disk, so the tmux session and Claude Code are still there after a resume. The plan is $18 a month, which comes back as $18 of VM time, with a 7-day free trial: card required, nothing charged for 7 days, cancel anytime. Connect over SSH covers ~/.ssh/config, port forwarding and VS Code; Run Claude Code in a VM is the full walkthrough.
When flockvm is the wrong pick
- One agent, all day, every day: a monthly VPS is usually the better buy. Dated prices and the break-even in hours are on Claude Code on a VPS or a microVM.
- More than 15 agents at once: the host in France runs at most 15 small VMs at once today, across all customers (2 during the trial). A larger cloud provider fits better.
- A region outside Europe: France is the only region today.
- No server at all, and your code may run on Anthropic's infrastructure: cloud sessions come with Pro, Max, Ultra and Team plans; see Claude Code in the cloud.
FAQ
Can Claude Code run on a headless server?
Yes. Claude Code is a terminal program and needs no desktop. The only step that wants a browser is the sign-in, and that browser can be on your laptop: open the URL the server prints and paste the code back. With an API key, there is no browser step at all.
Can I use my Pro or Max subscription on a remote server?
Yes: sign in with /login in the session on the server. Anthropic's terms don't stop "an end user from signing in to the unmodified Claude Code binary with their own Claude subscription". What to avoid is copying a long-lived subscription token from your laptop onto the server (above).
Does Claude Code keep running when I close my laptop?
Only if it runs inside tmux (or screen, or a background session) on the server. A plain claude started from an SSH shell stops when the connection drops.
How do I get back to a session after the connection dropped?
ssh -t agent@server tmux new -A -s claude. If the server rebooted, cd into the project and run claude --continue.
Should I run Claude Code as root on the server?
No. As root it refuses --dangerously-skip-permissions, and every mistake it makes is a root mistake. Create a user for it (step 1).
Is claude -p billed differently?
No. It uses the same credential as interactive sessions, except that when ANTHROPIC_API_KEY is set, -p always uses the key, billed per token, even if you also logged in with a subscription.
How much RAM does the server need?
Anthropic's floor is 4 GB. Add what your project runs: a test suite, a dev server, a database or Docker containers easily take more than the agent.
Run your agents in their own microVMs
flockvm gives each coding agent a Firecracker microVM that pauses with its memory and resumes, billed per second. France is live, and you can start with a 7-day free trial.
Start your 7-day free trialCard required. Nothing charged for 7 days.