-----BEGIN … PRIVATE KEY-----

Exposed Private Key (RSA / SSH / EC)? Here's how to detect and rotate it

Found a BEGIN PRIVATE KEY block in your repo? Check which kind it is, swap it safely without locking yourself out, and run a free Trust scan to find more.

A private key is the secret half of a keypair - the block that starts with -----BEGIN … PRIVATE KEY-----. It might be an SSH key that logs into your servers, a TLS key that terminates HTTPS for your domain, or a signing key that proves a token or artifact really came from you. The public half is meant to be shared; the private half must never leave your control. If it leaks, anyone holding it can act as you.

What an attacker can do with a leaked Private Key (RSA / SSH / EC)

How Private Key (RSA / SSH / EC)s get exposed

Scan your repo for exposed Private Key (RSA / SSH / EC)s - free

Trust checks the files in your repo and the JavaScript your site ships. No signup, results in about a minute.

Run a free scan →

How to rotate a leaked Private Key (RSA / SSH / EC)

  1. Generate a brand-new keypair - for SSH run ssh-keygen -t ed25519 -C "you@example.com" to a new filename; for TLS generate a fresh key and CSR; for a signing key mint a new one in your tool.
  2. Add the NEW public key everywhere the old one was trusted - append it to ~/.ssh/authorized_keys on each server, add it as a new GitHub/GitLab deploy key, or install the reissued cert - and keep the old one in place for now so you don't lock yourself out.
  3. Test that the new key works (ssh in with it, or verify the new cert serves) before you touch the old one.
  4. Now remove every trace of the old key - delete its line from authorized_keys, delete the old deploy key in your Git host's settings, and delete the leaked key file locally.
  5. For a TLS key, reissue the certificate from your CA and REVOKE the old cert so the leaked key can't be used even if someone kept it.
  6. Audit auth and access logs - SSH logins, Git clone events, cert usage - for any sessions from unfamiliar IPs while the key was exposed.

Is it really a private key?

Many key-looking files are meant to be shared. Check the first line before you worry.

  • BEGIN CERTIFICATE, or a file ending in .pub or .crt: a public half, not a private key. Still check that its matching private key did not leak with it.
  • BEGIN PRIVATE KEY, BEGIN RSA PRIVATE KEY, BEGIN OPENSSH PRIVATE KEY, BEGIN EC PRIVATE KEY: private. Replace it.
  • A throwaway key in a test folder that never protected anything real: still remove it from the repo, but there is nothing to rotate.

Keep keys out of your repo for good

  • Add *.pem, *.key and id_* to .gitignore.
  • Store deploy keys in your host's secret settings, not in a file.
  • For SSH, use ssh-agent so the key stays on your own machine.

How Trust helps

Trust's repo scan checks your files for BEGIN PRIVATE KEY, BEGIN RSA PRIVATE KEY and BEGIN OPENSSH PRIVATE KEY blocks. Run it before you push, and again after you replace the key.

Other secret types to check

Keep your app safe