How to log in to Claude Code on a remote server over SSH

Sign Claude Code in on a headless server over SSH with the paste-code login, a one-year setup-token, or an API key, and keep it working for sessions you start from your phone.

Based on
CLI
0.2.17
Preview ref
a2c45c8

TL;DR

On a server you reach over SSH, Claude Code can't open a browser, and the browser on your laptop can't reach the server's local callback. You have three ways in:

  1. Paste-code login. Run claude, open the login URL on your laptop or phone, sign in, and paste the code the browser shows back into the terminal. Good for a box you sit at now and then.
  2. A long-lived token. Run claude setup-token on a computer that has a browser, then set CLAUDE_CODE_OAUTH_TOKEN on the server. Good for a box nobody sits at.
  3. An API key. Set ANTHROPIC_API_KEY. Billed per token through the Claude Console, and the right fit for scripts and CI.

If the login screen won't let you copy the URL over SSH, skip straight to option 2.

The problem

A common setup: Claude Code sessions run on a Linux box in another room. It has the repos and it never sleeps. The first time you run claude there over SSH, the login screen asks you to open a browser, and the box has no browser.

The browser on your laptop can open the link, but after you sign in it tries to send you back to localhost on the laptop, where nothing is listening. Then the credentials expire a few weeks later and you go through it again, maybe from your phone on a train.

So the real questions are: how to sign in once, how to keep it signed in, and what to do when you're not at a keyboard.

Option 1: Log in to Claude Code over SSH with the paste-code flow

This is the normal path, and Anthropic's authentication docs describe it for SSH and headless hosts.

ssh you@server
claude

At the login screen:

  1. Press c to copy the login URL, or select it.
  2. Open it in a browser on your laptop or phone and sign in.
  3. The browser can't reach the server's callback, so it shows a code instead of redirecting.
  4. Paste that code at the Paste code here if prompted prompt.

Where it gets awkward:

  • The copy shortcut copies to the server's clipboard, which you can't reach over SSH. A GitHub issue on the Claude Code repo reports the URL wrapping inside the full-screen login UI so it can't be selected either. If that happens to you, use option 2.
  • tmux and narrow terminals can wrap the URL across lines. Widen the window or zoom the pane before you copy.
  • Credentials expire. When they do, the next session on the box stops at the login screen again, and you're back to doing this over SSH.

Option 2: Sign Claude Code in with a long-lived claude setup-token

For a box nobody sits at, Anthropic documents a one-year OAuth token. You create it once on a computer that has a browser:

# on your laptop
claude setup-token

It opens the same browser sign-in as /login, prints the token, and saves it nowhere. Copy it, then on the server:

# on the server, in the environment the agent runs in
export CLAUDE_CODE_OAUTH_TOKEN=your-token
claude

What Anthropic's docs say about this token:

  • It needs a Pro, Max, Team or Enterprise subscription.
  • It can only make model requests, so it can't start Claude Code Remote Control sessions or fetch claude.ai connectors. MCP servers you configure locally still work.
  • --bare mode doesn't read it. If a script passes --bare, use an API key instead.

Two practical notes:

  • Put the token somewhere only your user can read, such as a 0600 file you source, rather than a shell profile on a shared box. Anything that can read it can spend your subscription.
  • Set it where the agent actually starts. If Claude Code is launched by a background service, a variable exported in your interactive shell won't be there. This is the most common reason a token "works over SSH but not from the service".

Option 3: Use an Anthropic API key on the server

export ANTHROPIC_API_KEY=sk-ant-...
claude

Claude Code asks you once to approve the key, then skips the browser. It bills per token through the Claude Console, separate from a Pro or Max subscription. It suits scripts and scheduled jobs.

Keep the Claude Code login working for sessions you start from your phone

Options 1 to 3 solve the first login. The harder part is the second one: a session started from your phone a month later, stuck on an expired login on a box you aren't SSH'd into.

If you want to start and steer Claude Code sessions on the server from your phone, and see when one is blocked on a login without opening an SSH app, Happier is an open-source app that lets you run and orchestrate Claude Code, Codex and other agents cross-device, from your phone, desktop or the web.

The agent runs on your own computer or server, and the phone, desktop or web app connects to it. Run Happier from your phone covers the setup. It's on GitHub, you can download it here, and there's a Discord.

Here's how the login side works with it.

1) Pair the server with Happier by scanning a QR code

On the server:

curl -fsSL https://happier.dev/install | bash
happier auth login --method mobile

The terminal shows a QR code. Scan it with the Happier app on your phone and approve. If you're on a laptop that already has the Happier CLI, happier auth pair-remote --ssh you@server does the pairing over SSH for you.

2) Log in to Claude Code on the server from the Happier app

In the app, open Settings → AI & Agents → AI backends → Claude, choose the server (a machine in Happier) in the context bar, and look at the Authentication section. It shows whether Claude Code is logged in on that computer, and how.

Log in (or Reauthenticate) opens a Provider login terminal that runs Claude Code on the server and sends /login for you. You open the URL on the phone, sign in, and paste the code back into that terminal. It's option 1, without the SSH app. Check now refreshes the status after you've logged in some other way.

3) Store the Claude setup-token in Happier once

If you'd rather use a setup-token, Happier can hold it for you as a connected service:

claude setup-token          # on a computer with a browser
happier connect claude      # paste the token when asked

happier connect claude asks for a setup-token by default. The token is stored encrypted in your Happier account and decrypted only on your own devices. It shows up under Settings → Connected services, and you can pick it when you start a Claude session on any of your computers. An Anthropic API key works the same way with happier connect claude --api-key.

The honest limit: the server still has to be on and reachable. Happier doesn't keep a powered-off box signed in, and the provider's own rules about the token still apply.

Troubleshooting

Pressing c in the Claude Code login copies nothing I can paste

The copy lands on the server, not on your laptop. Select the URL with your terminal's own selection, widen the window so it doesn't wrap, or use claude setup-token on another computer.

The browser can't reach localhost after the Claude login

That's the callback trying to reach your laptop. Go back to the browser tab that shows a code, or restart the login and look for the code step. Over SSH, the code is the expected path.

Claude Code works over SSH, but sessions started by a service ask me to log in

The service doesn't see your interactive shell's environment. Set CLAUDE_CODE_OAUTH_TOKEN in the service's environment, or store the token as a connected service so the session gets it at start.

Claude Code Remote Control won't start with a setup-token

Anthropic's docs say the setup-token can only make model requests, so Remote Control needs the regular claude.ai login from option 1.

FAQ

Can I log in to Claude Code without a browser at all?

You need a browser somewhere, once. It can be on your laptop or your phone. claude setup-token and the paste-code login both use a browser on another device. Only an API key skips the browser entirely.

How long does the setup-token last?

One year, according to Anthropic's docs. It's printed once and saved nowhere, so store it before you close the terminal.

Do I need Claude Pro or Max for this?

For the paste-code login and the setup-token, you need a Claude subscription (the setup-token needs Pro, Max, Team or Enterprise). An API key doesn't need a subscription and bills through the Console.

Can I copy ~/.claude from my laptop to the server?

People do, but it's not a documented login path, and copied credentials can stop working when the provider rotates them. The setup-token is the documented way to reuse a sign-in on another computer.

Does this work the same on a Raspberry Pi or a VPS?

Yes. The steps don't depend on the hardware, only on having no browser on the box.

Get Happier

Happier is an open-source app that lets you run and orchestrate Claude Code, Codex and other agents cross-device, from your phone, desktop or the web.

claudesshlinuxheadlessself-host
Last updated: 2026-10-26