How to remove an ssh private key from ssh-agent/keychain

keychainpasswordssh

I followed some aws guide to add my private key to ssh-agent, using:

ssh-add -K "MyPrivateKey.pem"

However, I can't seem to remove the key using :

ssh-add -d "MyPrivateKey.pem"

which gives me the following error:

Bad key file MyPrivateKey.pem: No such file or directory

Unless I do ssh-add -D which removes all of the private keys I stored (obviously not favorable option). I also find that macOS ssh key management is tightly bind with Keychain, but I haven't discovered any method to identify the specific ssh key from Keychain Access. I am running on Mojave 10.14.6

tl;dr How do I get ssh-add -d to work on macOS?

Best Answer

After I post the question, I found the answer, the following command will generate the public key using the private key file:

ssh-keygen -y -f ./MyPrivateKey.pem > ./MyPublicKey.pub

then to remove it:

ssh-add -d "MyPublicKey.pub"

Reference