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
- Detects SSH sesยญsions via
$SSH_CONNECTION - Unlock the keyยญchain once per sesยญsion, using
$KEYCHAIN_UNLOCKEDto guard against mulยญtiยญple attempts - Delegate to the real
claudecomยญ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.

