Renewed early, expired anyway: Claude Code, SSH and the macOS keychain
My AI agents run on a headless Mac mini: long-running Claude Code sessions, started in the background, that I reach through Claude Code’s Remote Control from the web, desktop and mobile apps. They all share one Claude sign-in, and a sign-in has an expiry date. A couple of days before this one was due, I connected to the mini over SSH and signed in to Claude Code again. The sign-in completed normally. That should have been the expiry dealt with.
On the day itself, I couldn’t reach any of the sessions. Not from the web app, not from the desktop app. They didn’t all go at once: the manager agent’s session dropped off in the early afternoon, the small watcher I run alongside them kept going until the middle of the afternoon, and then nothing. Messages to the agents sat unanswered for the rest of the day.
The obvious fixes that weren’t #
That evening I did what most of us would do. I opened an SSH session to the mini and signed in to Claude Code again. Again it went through cleanly, and a new terminal session I started straight afterwards worked perfectly. The agents’ sessions, though, carried on failing.
So I rebooted the Mac. The background services came back up and went straight into a loop of “must be logged in”.
That combination is what made it interesting. I had signed in twice. A fresh claude in a terminal was happy. The services, started fresh after a reboot, were not. Whatever I was renewing, it clearly wasn’t the thing they were reading.
Where the sign-in actually went #
Working back through the evening with the manager agent, using the session transcripts, the watcher’s log and Claude Code’s own prompt history, three things lined up:
- The evening sign-in over SSH had written a plain credentials file, and the item Claude Code keeps in the login keychain hadn’t changed.
- In an SSH session about ten minutes after that sign-in,
security show-keychain-infocame back with “User interaction is not allowed.” - Late that night I signed in once more, this time in the Mac’s own desktop session, through a remote desktop session to it. The keychain item’s modification time moved, and the agents’ sessions started working again straight away.
The next morning the watcher, which reads the sign-in the same way the agents do, reported a new expiry date about four weeks out, where the day before it had shown the old one. That was the confirmation I wanted: the services were now running on the new sign-in, not just my terminal.
So there were two places a sign-in could live. Anthropic’s documentation (opens in a new tab) describes both. On macOS, Claude Code stores its credentials in the login keychain, and “when the Keychain rejects the write, such as when it’s locked in an SSH session”, it stores the login in ~/.claude/.credentials.json, with file mode 0600, instead. If you’ve set CLAUDE_CONFIG_DIR, both move with it: the file sits under that directory, and the keychain entry is keyed to it too, so a session with a different CLAUDE_CONFIG_DIR reads a different entry. That’s worth checking first if your services and your shell don’t share the same environment.
You can see whether a sign-in has gone to the file without opening it:
ls -lT ~/.claude/.credentials.jsonOn a Mac where the sign-in is in the keychain, as it should be, this is what you want to see:
ls: /Users/<you>/.claude/.credentials.json: No such file or directoryIf the file is there, ls -lT shows its permissions, which Anthropic documents as 0600 (-rw-------), and its modification time to the second. That time tells you when something last wrote to it. Don’t cat it; it holds the login itself.
The keychain side needs the same treatment: look at the entry’s metadata, never its contents. Claude Code 2.1.330 keeps its sign-in in an entry called Claude Code-credentials. You can find it by searching for that name in Keychain Access, or from the terminal:
security find-generic-password -s "Claude Code-credentials" ~/Library/Keychains/login.keychain-db | grep -E '"svce"|"cdat"|"mdat"'Without -w or -g, security prints the entry’s attributes and not the secret. The grep keeps the output to the three lines that matter, because the full list of attributes includes your account name. After a sign-in, it looks like this (dates replaced):
"cdat"<timedate>=0x… "YYYYMMDDhhmmssZ\000"
"mdat"<timedate>=0x… "YYYYMMDDhhmmssZ\000"
"svce"<blob>="Claude Code-credentials"cdat is when the entry was created and mdat is when it was last changed, both in UTC. If there’s no entry at all, you get this instead:
security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain.I haven’t tested what the entry is called with a custom CLAUDE_CONFIG_DIR, so if you use one, look in Keychain Access for the entry that changes when you sign in with it.
Compare mdat with the file’s modification time. If you’ve just signed in and the file is newer than the keychain entry, that strongly suggests the renewal landed in the file, and that anything reading the keychain is still on the old sign-in. Timestamps don’t prove it on their own, but alongside what the services are doing they’re hard to argue with. That’s the picture we had: a file written by the SSH sign-in, and a keychain entry that didn’t change until I signed in from the desktop.
Why SSH and the keychain don’t mix #
This is the part worth understanding, because it isn’t really about Claude Code.
When you log in at a Mac’s screen, your password unlocks your login keychain for that desktop session, and anything that needs a keychain item can, if necessary, ask you through a dialogue. An SSH session is a different security session. Your login keychain isn’t necessarily unlocked there, and there’s no screen to show a prompt on, so anything that would need to ask gets refused.
You can see that from the SSH side with one command:
security show-keychain-info ~/Library/Keychains/login.keychain-dbThis is what it returned on the mini during the incident, in an SSH session ten minutes after the evening sign-in:
security: SecKeychainCopySettings …: User interaction is not allowed.The error is errSecInteractionNotAllowed (opens in a new tab), which Apple describes simply as “Interaction with the Security Server is not allowed.” Here it meant that reading the keychain’s settings needed the user, and the session wasn’t allowed to ask. That’s good evidence the keychain wasn’t usable from that session for anything that needs it unlocked, and it matches Anthropic’s own example of when the write is rejected. What it doesn’t prove is the keychain’s state at the exact moment of either SSH sign-in, because nobody captured that.
So, most likely, the sign-in over SSH couldn’t be written to the keychain, and it landed in the fallback file. My terminal read the file and worked. The background sessions, as far as I can tell, kept reading the keychain, which still held the old sign-in, and that one ran out on schedule. Renewing early hadn’t renewed anything they could see.
I should be clear about what’s confirmed and what’s inferred. The file being written by the evening sign-in, the keychain entry changing only with the desktop sign-in, and the services recovering the moment it did all come from the timeline we rebuilt from the records, which read the stores’ dates rather than their contents. The keychain being unusable over SSH is supported by that error and by the documented behaviour, but its state at the moment I signed in wasn’t captured. And the early renewal isn’t in the records at all; I know about it because I did it.
The fix, and testing it #
There are two ways to renew safely. The simplest is to sign in from the desktop session. If SSH is all you have, unlock the login keychain first, then sign in:
security unlock-keychain ~/Library/Keychains/login.keychain-db && claude auth loginsecurity unlock-keychain asks for the keychain’s password, which is normally your login password, and unlocks the login keychain. The && matters: if the unlock fails, the sign-in doesn’t run, so you don’t get another sign-in that only looks successful. It doesn’t guarantee the write itself succeeds, which is why the checks below still matter.
I wanted to see both halves of this for myself: the failure and the fix. So I reproduced it on a separate test Mac over SSH, with macOS 27.2.1 and Claude Code 2.1.330. This is controlled testing afterwards, not evidence from the incident itself. The outputs are from that run, with my username, email address, organisation, plan, the sign-in links and the dates taken out.
To start clean, I signed Claude Code out with claude auth logout and locked the login keychain with security lock-keychain ~/Library/Keychains/login.keychain-db. There was no credentials file and no keychain entry:
ls: /Users/<you>/.claude/.credentials.json: No such file or directory
security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain.And over SSH, the keychain refused interaction, just as the mini’s had:
security show-keychain-info ~/Library/Keychains/login.keychain-dbsecurity: SecKeychainCopySettings /Users/<you>/Library/Keychains/login.keychain-db: User interaction is not allowed.The failure, on purpose #
Then I signed in over SSH without unlocking anything:
claude auth loginLogin successful.That’s the trap. The sign-in reported success and exited cleanly, exactly as it had for me on the mini. Where it went is a separate question:
ls -lT ~/.claude/.credentials.json
security find-generic-password -s "Claude Code-credentials" ~/Library/Keychains/login.keychain-db | grep -E '"svce"|"cdat"|"mdat"'-rw------- 1 <you> staff 700 <date and time> /Users/<you>/.claude/.credentials.json
security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain.The credentials file had appeared, with the 0600 permissions Anthropic documents (opens in a new tab) for it, and the keychain still had no entry. So the sign-in went to the file, as the documentation says it will when the keychain rejects the write.
Here’s one result I didn’t expect. claude auth status --text reported a valid sign-in both from the SSH shell and from a launch agent in my GUI domain (I’ll come to how). With no keychain entry at all, both used the file. During the incident the mini was different: its keychain still held the old sign-in. My reading is that the services kept using that keychain entry and never looked at the file, while the shell used the file, but I haven’t reproduced that exact combination, so treat it as an inference.
The fix #
Then the one-liner from above, unlocking the keychain first:
security unlock-keychain ~/Library/Keychains/login.keychain-db && claude auth loginpassword to unlock /Users/<you>/Library/Keychains/login.keychain-db:
Login successful.And the same checks again:
ls: /Users/<you>/.claude/.credentials.json: No such file or directory
"cdat"<timedate>=0x… "YYYYMMDDhhmmssZ\000"
"mdat"<timedate>=0x… "YYYYMMDDhhmmssZ\000"
"svce"<blob>="Claude Code-credentials"
Keychain "/Users/<you>/Library/Keychains/login.keychain-db" no-timeoutThis time the keychain entry was created, with cdat and mdat set to the same moment, and the credentials file had gone. Signing in properly had removed the fallback copy. The last line shows the keychain’s settings can now be read from this session. That’s a good sign on its own, but it doesn’t establish whether Claude Code can write its credentials there; the new entry is what shows that. And the missing file isn’t proof on its own either: it’s the new entry, alongside the missing file, that tells you where the sign-in went.
It’s worth keeping the four results apart, because each proves something different. The keychain’s settings being readable says the session can reach it. “Login successful” says you authenticated. The new entry, with no file, says where the result was stored. And none of those says your services can use it. That needs its own check.
Checking from the service’s side #
claude auth status --text reports the sign-in like this (the first three lines identify you, so I’ve replaced them):
Login method: <plan>
Organization: <organisation>
Email: <email>
Sign-in ends: between <date> and <date> (<date and time>Z)The line that matters is Sign-in ends, which Claude Code prints from version 2.1.318, according to the same page (opens in a new tab). It gives the window in which the sign-in ends, followed by the exact time. Compare it before and after a renewal: if it hasn’t moved, whoever is asking is still seeing the old sign-in.
The catch is where you run it. The same username isn’t the same context. Apple’s old but still useful TN2083, “Daemons and Agents” (opens in a new tab) describes how a GUI login gets its own per-session bootstrap namespace, and “a non-GUI per-session bootstrap namespace is created when a user logs in via SSH”. A launch agent like the ones my agents run under runs in the GUI one. So I check from there, with a one-shot launch agent that runs the status command and reports back:
#!/bin/zsh
# Run `claude auth status --text` as a one-shot launch agent in your GUI domain,
# so you see what a background launch agent sees, not what your shell sees.
set -u
CLAUDE=$(command -v claude) || { echo "claude not found on PATH" >&2; exit 1; }
[[ -x $CLAUDE ]] || { echo "not executable: $CLAUDE" >&2; exit 1; }
umask 077
WORK=$(mktemp -d "${TMPDIR:-/tmp}/authcheck.XXXXXX") || exit 1
LABEL="local.authcheck.$$"
DOMAIN="gui/$(id -u)"
trap 'launchctl bootout "$DOMAIN/$LABEL" 2>/dev/null; rm -rf "$WORK"' EXIT
trap 'exit 130' INT; trap 'exit 143' TERM
xml() { print -r -- "$1" | sed -e 's/&/\&/g' -e 's/</\</g' -e 's/>/\>/g'; }
cat > "$WORK/check.plist" <<EOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0"><dict>
<key>Label</key><string>$LABEL</string>
<key>ProgramArguments</key><array>
<string>/bin/sh</string><string>-c</string>
<string>"\$0" auth status --text >"\$1/out" 2>&1; echo \$? >"\$1/done.tmp"; mv "\$1/done.tmp" "\$1/done"</string>
<string>$(xml "$CLAUDE")</string><string>$(xml "$WORK")</string>
</array>
<key>RunAtLoad</key><true/>
</dict></plist>
EOF
launchctl bootstrap "$DOMAIN" "$WORK/check.plist" \
|| { echo "bootstrap failed: are you logged in at the Mac's desktop?" >&2; exit 1; }
for i in {1..30}; do [[ -f $WORK/done ]] && break; sleep 1; done
[[ -f $WORK/done ]] || { echo "timed out after 30 seconds" >&2; exit 1; }
cat "$WORK/out"
rc=$(<"$WORK/done")
[[ $rc == <-> ]] || { echo "no exit status recorded" >&2; exit 1; }
exit $rcIt writes a temporary launch agent into a private folder, loads it into your GUI domain, waits for the status command to finish (up to 30 seconds), prints what it said and exits with its exit code. Then it unloads the agent and deletes the folder, even if you interrupt it. It needs you to be logged in at the Mac’s desktop, because that’s where the GUI domain lives; otherwise launchctl bootstrap fails and the script says so. This is the exact script I ran on the test Mac, where it reported the same sign-in as the SSH shell both before and after the fix.
It reproduces an important part of a service’s context, the GUI login session and its access to the keychain, but not all of it. A real service has its own environment variables and its own configuration. If yours sets CLAUDE_CONFIG_DIR, add it to the agent too, with an EnvironmentVariables dictionary in the plist; if you leave it out, you’re checking the default location, not the service’s.
On the mini itself, recovery showed in two ways: the agents’ sessions came back within moments of the desktop sign-in, and the next morning the watcher reported a sign-in about four weeks away, where the day before it had shown the old one. That’s the service recovery check. If you can, check the service’s own log for a successful request too. If the evidence comes from your shell, it’s evidence about your shell.
For the record on versions: the mini was on macOS 27.2, or a later point release of it, and Claude Code 2.1.330 had been installed on the day of the expiry, before it ran out, after 2.1.325 the week before. The evening sign-in came after that install, but the versions the sessions were actually running weren’t captured. The test Mac ran macOS 27.2.1 and Claude Code 2.1.330. Authentication and credential storage are exactly the kind of behaviour that can change between releases, so treat this as what I saw on the versions above, and check the documentation for yours.
What I took from it #
Signing in isn’t the same as the services being signed in. Mine succeeded twice, and the second time the proof I had was a terminal that worked. But the terminal wasn’t what needed the sign-in. The services were, and as far as I can tell they read from somewhere else.
So when I renew anything a service depends on now, I check from the service’s side: the expiry date it reports, a request it makes, a line in its own log, not a fresh shell of my own. The same applies well beyond Claude Code. A certificate renewed in the wrong store, a token written to the wrong profile: each one looks finished from where you’re standing.
And on a Mac in particular, an SSH session and the desktop session don’t see the login keychain the same way. If a tool keeps its secrets in the keychain, sign in where the keychain is unlocked, or unlock it first. Otherwise you may find, as I did, that you renewed early and it expired anyway.