Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate public ssh key from private key

Tags:

linux

ssh

I have private ssh (id_rsa) key.

How I can generate public key from it?

like image 580
FoxDev Avatar asked Feb 05 '26 16:02

FoxDev


1 Answers

The option -y outputs the public key. From the linux manual for the ssh-keygen command:

-y ---- This option will read a private OpenSSH format file and print an OpenSSH public key to stdout.

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

As a side note, the comment of the public key is lost. I've had a site which required the comment (Launchpad?), so you need to edit ~/.ssh/id_rsa.pub and append a comment to the first line with a space between the comment and key data. An example public key is shown truncated below.

ssh-rsa AAAA..../VqDjtS5 ubuntu@ubuntu

If you want a scripted way to add a comment and also add the pubkey to your authorized_keys file, you can do...

ssh-keygen -y -f ~/.ssh/id_rsa    | \
    sed 's/$/ comment-goes-here/' | \
    tee ~/.ssh/id_rsa.pub         | \
    tee -a authorized_keys
like image 73
Avihoo Mamka Avatar answered Feb 08 '26 07:02

Avihoo Mamka