Claude Code CLI over SSH on macOS: Fixing Keychain Access

Claude Code is a powยญerยญful command-โ€‹line tool for agenยญtic softยญware develยญopยญment. However, if you try to use it over an SSH secure shell sesยญsion on macOS, you may see a conยญfusยญing mix of โ€‹โ€œLogin sucยญcessยญfulโ€ and โ€‹โ€œMissing API keyโ€ mesยญsages. The root cause: Claude Codeโ€™s OAuth token lives in the macOS Keychain, which SSH sesยญsions canโ€™t access by default.

Hereโ€™s a quick fix that took about 10 minยญutes to build โ€” with Claude Codeโ€™s help. (Meta, but effective.)

The Fix

Add this to your ~/.zshrc:

# Wrapper function to unlock keychain before running claude
claude() {
  if [ -n "$SSH_CONNECTION" ] && [ -z "$KEYCHAIN_UNLOCKED" ]
  then
    security unlock-keychain ~/Library/Keychains/login.keychain-db
    export KEYCHAIN_UNLOCKED=true
  fi
  command claude "$@"
}

Reload your shell (source ~/.zshrc), then run claude over SSH. It will prompt for your keyยญchain passยญword once per sesยญsion, then work normally.

How It Works

  1. Detects SSH sesยญsions via $SSH_CONNECTION
  2. Unlock the keyยญchain once per sesยญsion, using $KEYCHAIN_UNLOCKED to guard against mulยญtiยญple attempts
  3. Delegate to the real claude comยญmand with all arguยญments passed

The keyยญchain stays unlocked for the duraยญtion of your SSH sesยญsion, so you only enter the passยญword once.

Security note: This doesยญnโ€™t bypass macOS Keychain secuยญriยญty. It just prompts you once per SSH sesยญsion, the same as if youโ€™d unlocked it locally.

With this wrapยญper in place, I can get Claude Code to behave over SSH exactยญly as it does localยญly. There are no surยญprisยญes and no API keys, and my Claude Pro login works as expected.

The broader lesson

Command line tools that rely on the macOS Keychain often break over SSH. Wrapping those tools with the security unlock-keychain comยญmand genยญerยญalยญly fixยญes those issues.


Discover more from The Phoenix Trap

Subscribe to get the latest posts sent to your email.

Mark Gardner Avatar

Hi, Iโ€™m Mark.

Hi, Iโ€™m Mark Gardยญner, and this is my personal blog. I show software developers how to level up by building production-ready things that work. Clear code, real projects, lessons learned.