惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

月光博客
月光博客
Martin Fowler
Martin Fowler
Last Week in AI
Last Week in AI
罗磊的独立博客
阮一峰的网络日志
阮一峰的网络日志
博客园 - 【当耐特】
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
博客园 - 三生石上(FineUI控件)
S
SegmentFault 最新的问题
V
Visual Studio Blog
Hugging Face - Blog
Hugging Face - Blog
雷峰网
雷峰网
博客园_首页
人人都是产品经理
人人都是产品经理
量子位
美团技术团队
The Cloudflare Blog
小众软件
小众软件
WordPress大学
WordPress大学
有赞技术团队
有赞技术团队
M
MIT News - Artificial intelligence
Microsoft Security Blog
Microsoft Security Blog
D
DataBreaches.Net
博客园 - Franky

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Multiple accounts in Claude Code: the complete setup
jguillaumesio · 2026-06-26 · via DEV Community

You have a personal Claude Code subscription and a work one. Or you freelance for two clients who each provide their own API key. Or you want a sandboxed account for experiments that won't pollute your main config.

Whatever the reason, Claude Code doesn't ship with a built-in "switch account" command. But the architecture makes it straightforward: everything lives in a single config directory, and you can redirect it.


How Claude Code stores state

Claude Code keeps all its state in ~/.claude by default. That includes:

  • Authentication tokens
  • Project-level settings (.claude/ inside each repo)
  • Conversation history
  • Memory files
  • MCP server configs

The key insight: the CLAUDE_CONFIG_DIR environment variable overrides where Claude Code looks for all of this. Point it somewhere else, and you get a completely independent instance.


Basic setup: shell aliases

Create one config directory per account:

mkdir -p ~/.claude-personal
mkdir -p ~/.claude-work

Find your Claude binary path:

which claude
# e.g. /Users/you/.nvm/versions/node/v22.15.0/bin/claude

Add aliases to your ~/.zshrc (or ~/.bashrc):

alias claude-personal='CLAUDE_CONFIG_DIR=~/.claude-personal claude'
alias claude-work='CLAUDE_CONFIG_DIR=~/.claude-work claude'

Reload your shell:

source ~/.zshrc

Then authenticate each one separately:

claude-personal   # run /login inside the session
claude-work       # run /login inside the session

Each alias now opens Claude Code with its own auth, memory, and settings.


What the alias approach misses

The alias trick works, but it has gaps that will bite you in production use.

Problem 1: NVM version upgrades break absolute paths

If you hardcode the binary path in your alias (as many guides suggest), upgrading Node via NVM silently breaks it. Use just claude instead of the absolute path, and let $PATH resolve it:

# fragile
alias claude-work='CLAUDE_CONFIG_DIR=~/.claude-work /Users/you/.nvm/versions/node/v22.15.0/bin/claude'

# resilient
alias claude-work='CLAUDE_CONFIG_DIR=~/.claude-work claude'

Problem 2: project-level config leaks between accounts

Claude Code creates a .claude/ directory inside your project repo. That directory stores project settings, CLAUDE.md, and settings.json. These are shared across all your aliases because they live in the repo, not in the config dir.

This means your work account and personal account see the same project-level instructions. That's usually fine, but if you need different MCP servers or permissions per account per project, you'll need to handle it differently (see the wrapper script section below).

Problem 3: hooks and MCP servers are per-config-dir

If you've configured custom hooks or MCP servers in ~/.claude/settings.json, those won't exist in your new config directories. You'll need to copy or symlink the parts you want shared:

# share hooks across accounts
ln -s ~/.claude/settings.json ~/.claude-work/settings.json

Or, if you want different hooks per account, copy and customize:

cp ~/.claude/settings.json ~/.claude-work/settings.json

Problem 4: memory doesn't transfer

Each config directory has its own memory system. Your personal account won't remember what your work account learned. If you use memory-heavy workflows, that isolation is sometimes a feature, sometimes a problem.


Better approach: a wrapper script

Instead of simple aliases, a small wrapper gives you account switching with validation:

#!/usr/bin/env bash
# Save as ~/bin/claude-switch and chmod +x

ACCOUNT="${1:?Usage: claude-switch <account-name> [claude args...]}"
shift

CONFIG_DIR="$HOME/.claude-$ACCOUNT"

if [ ! -d "$CONFIG_DIR" ]; then
  echo "Account '$ACCOUNT' not found. Available accounts:"
  ls -d ~/.claude-* 2>/dev/null | sed 's|.*/.claude-||'
  exit 1
fi

CLAUDE_CONFIG_DIR="$CONFIG_DIR" exec claude "$@"

Usage:

claude-switch work
claude-switch personal --resume
claude-switch work -p "fix the login bug"

This passes all arguments through, so flags like --resume, --print, and -p all work.


Per-project account defaults

If a specific repo should always use a specific account, you can set it in a .envrc file (if you use direnv):

# /path/to/work-project/.envrc
export CLAUDE_CONFIG_DIR="$HOME/.claude-work"

Now every time you cd into that project and run claude, it automatically uses the work account. No alias needed.

Without direnv, you can add it to the project's shell history or a local .env file that your shell sources.


API key accounts vs OAuth accounts

There are two authentication modes in Claude Code, and they interact differently with multi-account setups:

OAuth (default): You run /login and authenticate through Anthropic's web flow. The token is stored in the config directory. This is what most people use with Claude Pro/Max subscriptions.

API key: You set ANTHROPIC_API_KEY as an environment variable. This bypasses the config directory's auth entirely.

For API key setups, you don't even need separate config directories for auth. You can just switch the key:

alias claude-client-a='ANTHROPIC_API_KEY=$CLIENT_A_KEY claude'
alias claude-client-b='ANTHROPIC_API_KEY=$CLIENT_B_KEY claude'

But you'll still want separate config directories if you need isolated memory, hooks, or MCP servers.


Combining both: API key + config isolation

The most robust setup for freelancers or consultants:

# ~/.zshrc
export CLIENT_A_KEY="sk-ant-..."  # or source from a secrets manager

alias claude-client-a='ANTHROPIC_API_KEY=$CLIENT_A_KEY CLAUDE_CONFIG_DIR=~/.claude-client-a claude'
alias claude-client-b='ANTHROPIC_API_KEY=$CLIENT_B_KEY CLAUDE_CONFIG_DIR=~/.claude-client-b claude'

Each client gets:

  • Their own API key (billing goes to the right place)
  • Their own memory (client context stays separate)
  • Their own MCP servers (different clients, different tools)
  • Their own hooks (different code review standards, different workflows)

Running accounts simultaneously

You can run multiple Claude Code instances in parallel, each in its own terminal tab. The config directories are independent, so there's no locking or conflict.

# Terminal 1
claude-work

# Terminal 2
claude-personal

Both sessions run concurrently without interference. This is useful when you're waiting on a long task in one account and want to work on something else in another.


Quick reference

What you want What to set
Different auth CLAUDE_CONFIG_DIR
Different API key ANTHROPIC_API_KEY
Different memory CLAUDE_CONFIG_DIR
Different MCP servers CLAUDE_CONFIG_DIR + custom settings.json
Per-project default .envrc with CLAUDE_CONFIG_DIR
All of the above Combined alias with both env vars

Going further: multiple Claude Desktop instances

Everything above covers Claude Code (the CLI). If you also use the Claude desktop app and want two instances running side by side with different accounts, the approach is different: you need to duplicate the app itself.

On macOS, Parallels Toolbox can create an "app shortcut" that acts as a second copy of Claude. Each copy maintains its own login session, so you can run your work account in one window and your personal account in another, without logging in and out. This walkthrough shows the full setup.

The process: open Parallels Toolbox, create an app shortcut pointing to Claude, give it a distinct name (like "Claude Work"), approve it in macOS security settings, and log in with your second account. Both instances live in your dock and run independently.

This pairs well with the CLI multi-account setup: use CLAUDE_CONFIG_DIR aliases for terminal work, and Parallels app shortcuts for the desktop GUI.


What I actually use

Two config directories: ~/.claude-personal for my own projects, ~/.claude-work for client work. Direnv handles the switching per project, so I just type claude and it picks the right account. I symlink settings.json from my personal config to the work one because I want the same hooks everywhere, but memory stays separate.

The total setup took five minutes. The part that took longest was realizing I needed to re-run /login in each config directory after creating it.


Originally published on jguillaumesio.com