Claude Code is a powerful command-line tool for agentic software development. However, if you try to use it over an SSH secure shell session on macOS, you may see a confusing mix of “Login successful” and “Missing API key” messages. The root cause: Claude Code’s OAuth token lives in the macOS Keychain, which SSH sessions can’t access by default.
Here’s a quick fix that took about 10 minutes 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 keychain password once per session, then work normally.
How It Works
- Detects SSH sessions via
$SSH_CONNECTION - Unlock the keychain once per session, using
$KEYCHAIN_UNLOCKEDto guard against multiple attempts - Delegate to the real
claudecommand with all arguments passed
The keychain stays unlocked for the duration of your SSH session, so you only enter the password once.
Security note: This doesn’t bypass macOS Keychain security. It just prompts you once per SSH session, the same as if you’d unlocked it locally.
With this wrapper in place, I can get Claude Code to behave over SSH exactly as it does locally. There are no surprises 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 command generally fixes those issues.

