Extract a public key from a private key
Rebuild a public key from a private key file with ssh-keygen -y. The output holds the type and the key but no comment, so it differs from the .pub file.
The private key is on disk but the .pub file is missing, and you need the public half to install on a server.
ssh-keygen -q -t ed25519 -f ./k -N '' -C [email protected]
rm k.pub
ssh-keygen -y -f ./k > k.rebuilt
cut -d' ' -f1,3 k.rebuiltssh-ed25519 [email protected]The public key is derived from the private one, so -y reads the private key file and prints the public key line to standard output. See the ssh-keygen manual for the option. In the OpenSSH key format used since 6.5 the comment is stored inside the private key, so it comes back too, as the output shows.
A private key in the older PEM format has no stored comment, so the rebuilt line has only two fields and you add the label by hand if you use it to tell keys apart in authorized_keys. If the key has a passphrase, -y asks for it.