# Claude Code on a Remote Server over SSH

> Run Claude Code on a remote Linux server: install, sign in with /login or an API key, keep it alive in tmux over SSH, and when one VM per agent is simpler.

- URL: https://flockvm.com/learn/claude-code-remote-server
- Last updated: 2026-11-10

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](/learn/claude-code-vps-vs-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:

```bash
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:

```bash
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:

```bash
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 `stable` channel "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 use `sudo 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](#can-you-drive-it-from-your-phone), 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:

```bash
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.

## 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:

```bash
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:

```bash
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](/learn/claude-code-tmux#how-do-you-set-up-tmux-for-claude-code).

### 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`:

```ini
[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:

```console
$ 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.

## 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:

```bash
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.** `/login` isn't available in `-p` mode. With no credential, our run printed `Not logged in · Please run /login` and exited with code 1. Sign in interactively once, or set `ANTHROPIC_API_KEY`.
- **Permissions.** Since v2.1.302, a `-p` run 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 auto` with `--permission-prompts none` for unattended runs.
- **Trust.** A `-p` run "shows no workspace trust dialog" and still runs the repository's hooks and `.mcp.json` servers. 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 json` returns the result with `total_cost_usd`, a client-side estimate. `--max-budget-usd` stops 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](/learn/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](/learn/claude-code-sandbox) compares that with the built-in `/sandbox`, containers and dev containers; [running --dangerously-skip-permissions safely](/learn/dangerously-skip-permissions-safely) tests which layer blocks what; [microVM vs container](/learn/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](/learn/claude-code-in-the-cloud).

## 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.

```bash
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](/pricing): card required, nothing charged for 7 days, cancel anytime. [Connect over SSH](/docs/vms/ssh) covers `~/.ssh/config`, port forwarding and VS Code; [Run Claude Code in a VM](/docs/agents/claude-code) 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](/learn/claude-code-vps-vs-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](/learn/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](#dont-copy-a-subscription-token-onto-the-server)).

### 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.

## Sources

- [Anthropic, Claude Code docs: Advanced setup (system requirements, native install, apt repository, auto-updates, sudo npm warning)](https://code.claude.com/docs/en/setup) (accessed 2026-11-05)
- [Anthropic, Claude Code docs: Authentication (login over a URL, credential storage, precedence, ANTHROPIC_API_KEY)](https://code.claude.com/docs/en/authentication) (accessed 2026-11-05)
- [Anthropic, Claude Code docs: Legal and compliance (authentication and credential use)](https://code.claude.com/docs/en/legal-and-compliance) (accessed 2026-11-05)
- [Anthropic, Claude Code docs: CLI reference (claude auth login, auth status, --continue, --resume, --bg, --max-budget-usd)](https://code.claude.com/docs/en/cli-reference) (accessed 2026-11-05)
- [Anthropic, Claude Code docs: Run Claude Code programmatically (claude -p, --bare, --allowedTools, permission modes, exit codes)](https://code.claude.com/docs/en/headless) (accessed 2026-11-05)
- [Anthropic, Claude Code docs: Permission modes (bypassPermissions refused as root)](https://code.claude.com/docs/en/permission-modes) (accessed 2026-11-05)
- [Anthropic, Claude Code docs: Remote Control (requirements, server mode)](https://code.claude.com/docs/en/remote-control) (accessed 2026-11-05)
- [Anthropic, Claude Code docs: Agent view (background sessions and the supervisor process)](https://code.claude.com/docs/en/agent-view) (accessed 2026-11-05)
- [Ubuntu manpages (resolute): sshd_config(5), first obtained value, PermitRootLogin](https://manpages.ubuntu.com/manpages/resolute/en/man5/sshd_config.5.html) (accessed 2026-11-05)
- [Ubuntu manpages (resolute): ufw(8)](https://manpages.ubuntu.com/manpages/resolute/en/man8/ufw.8.html) (accessed 2026-11-05)
- [systemd.service(5) manual: Type=forking, Restart=on-failure](https://www.freedesktop.org/software/systemd/man/latest/systemd.service.html) (accessed 2026-11-05)
- [tmux manual page (new-session -A, -d, -c; detach-client)](https://man.openbsd.org/tmux) (accessed 2026-11-05)
