brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: ssh-keygen -y rebuilds a lost public key from the private key

Network and access
~/tips/tip-ssh-keygen-y-public-key

ssh-keygen -y -f ~/.ssh/id_ed25519

Prints the public key that belongs to a private key, so you can restore or match it.

The private key is still in ~/.ssh, but the .pub file next to it is gone. Or you have a few keys lying around and no idea which public key belongs to which. Neither needs a new key pair, because the public key can always be derived from the private one.

ssh-keygen -y -f ~/.ssh/id_ed25519

-y reads a private key in OpenSSH format and prints the matching public key, and -f names the file. If the key has a passphrase, ssh-keygen asks for it first.

To put the missing file back:

ssh-keygen -y -f ~/.ssh/id_ed25519 > ~/.ssh/id_ed25519.pub

I’d also use it to check whether a key matches a line in a server’s authorized_keys, or a .pub you found somewhere. This compares only the key type and the key itself, so a different comment at the end doesn’t count as a mismatch:

diff <(ssh-keygen -y -f ~/.ssh/id_ed25519 | cut -d' ' -f1,2) \
     <(cut -d' ' -f1,2 ~/.ssh/id_ed25519.pub)

No output means they belong together.

Two things to watch. The comment at the end of the line (often user@host) may be missing or different from what your old .pub had, so add it by hand if you rely on it to recognise keys in authorized_keys. And be careful with the >: point it at the .pub name, never at the private key file, because the shell empties the target before ssh-keygen even reads anything.

Further reading: ssh-keygen(1) manual

More Network and access tips scp -3 alice@server-a:/var/backups/db.sql.gz bob@server-b:/srv/restore/ tailscale exit-node suggest tailscale lock sign nodekey:1abddef1 tlpub:abcdef12 wg genpsk > peer1.psk Table = off All Network and access tips (163) All topics

Link