User guide

16. Claude Code without logging in again: long-lived token

Create a one-year token with claude setup-token, so SSH, scripts and restarts never ask you to log in.

The usual annoyance with Claude Code on a Mac mini: it asks you to log in through the browser again after a restart, in a new terminal, over SSH or inside a script. Set up a long-lived token with the 4 steps below and you won't log in again for a year.

Needs a paid Claude plan (Pro, Max, Ultra, Team or Enterprise; Free does not work). Do this once per Mac mini. The token is valid for one year.

Step 1: Create the token

On the Mac mini, open Terminal (⌘+Space, type Terminal, Enter) and run:

claude setup-token

Your browser opens the Claude authorization page; approve it. Back in Terminal you'll see a token starting with sk-ant-oat. Copy it now: it is shown only once.

Step 2: Save it in a file only you can read

Run this line, then paste the token and press Enter (nothing shows while you paste; that's normal):

read -rs T && printf 'export CLAUDE_CODE_OAUTH_TOKEN="%s"\n' "$T" > ~/.claude-token && chmod 600 ~/.claude-token && unset T && echo saved

This keeps the token out of your shell history. chmod 600 means only your user account can read the file.

Step 3: Load it in every shell

grep -q claude-token ~/.zshenv 2>/dev/null || echo '[ -f ~/.claude-token ] && source ~/.claude-token' >> ~/.zshenv

It goes in ~/.zshenv, not ~/.zshrc, so non-interactive runs like ssh host claude, cron jobs and scripts get the token too. Running it twice won't add the line twice.

Step 4: Test it

Close Terminal, open a new one and run:

claude -p "hello"

If it answers without asking you to log in, you're done. From another computer you can also try: ssh user@host 'claude -p "hello"'.

Troubleshooting

ProblemFix
It still asks me to log inCheck that echo ${CLAUDE_CODE_OAUTH_TOKEN:0:12} prints something. If not, make sure the Step 3 line is in ~/.zshenv, then open a new terminal.
It bills my API key instead of my subscriptionIf ANTHROPIC_API_KEY is set, it takes priority. Remove that line to use your subscription.
The token expired after a yearRepeat Steps 1 and 2. Step 3 stays as it is.
I switched Claude accountsSame: create a new token and overwrite ~/.claude-token.
Remote Control from my phone stopped workingExpected: this token can only make model requests. It cannot start Remote Control sessions or fetch claude.ai connectors (locally configured MCP servers still work). For Remote Control, sign in normally with /login on that machine.
My script uses --bare and still asks to log inBare mode does not read this token. Use ANTHROPIC_API_KEY or apiKeyHelper instead.
Which login is active?Type /status in Claude Code. Order when several are set: ANTHROPIC_AUTH_TOKEN → ANTHROPIC_API_KEY → apiKeyHelper → this token → browser login.
No token, but SSH keeps asking me to log inOver SSH the macOS Keychain is locked, so Claude Code stores the login in ~/.claude/.credentials.json (readable only by you). Running /login once inside SSH usually sticks.

Security

The token is as good as your Claude account: never send it to anyone, screenshot it, or put it in a Git repo, cloud drive or group chat. Our support team will never ask for it.
  • Before your rental ends: run rm -f ~/.claude-token and delete that line from ~/.zshenv. The machine goes to the next customer afterwards.
  • If you think the token leaked: delete the file, change your Claude password and sign out of other devices, then create a new token.

Checked against Anthropic's official docs on 2026-11-05: Claude Code Authentication (Generate a long-lived token, Authentication precedence, Credential management).