ssh: ssh-agent 'invalid key length' error

Tags: til ssh gpg

If the gpg agent is used as the ssh-agent and an invalid SSH key is added then it can completely break the agent:

ssh-add -l
# error fetching identities: Invalid key length
# 🤔
ssh-add -D
# All identities removed.
ssh-add -l
# error fetching identities: Invalid key length
# 😱

Thanks to this reddit post from itmecho for the answer; remove the offending key via gpg-connect-agent:

$ gpg-connect-agent
> KEYINFO --ssh-list --ssh-fpr # List the SSH keys with their MD5 fingerprints
...
S KEYINFO SOMELONGKEYGRIP D - - 1 P MD5:so:me:md:5c:he:ck:su:mm - S
...
> DELETE_KEY SOMELONGKEYGRIP # This gave me a prompt with the comment from the SSH key so it was easy to tell which one I was deleting

If you want to match up the keys in the KEYINFO output, you can print out your public keys:

fd .pub ~/.ssh --exec ssh-keygen -l -f
Published on: 24 Jun 2025