SSH Tricks: extract the public key from the private key
Recently i learned something really cool! since the public ssh key is part of the private ssh key we can extract the public key from the…
Recently i learned something really cool! since the public ssh key is part of the private ssh key we can extract the public key from the private; to do this we use the ssh-keygen command vs the private key.ssh-keygen -y -f ~/.ssh/id_rsa
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDkCGI5oGH0fqYelPZXIMTdcvXDlRxKzGey3UH4L7QmChi8sS9VY74qsgBwiE3rio/D0zTnR7Jdk63pFFg75ZjKakQEZK5iINqLNSj5tesST0W5/J6AaPRbUQhMZqiA1mhfUaVw15VtvRzle/ke9t+pWXNNkaWgvrSuNCP8v9/IAfBdQaoPy+7y7x9XSWR22XebxtFm55Rzk7p/HTdJHtVf6i+HtJFXYg06KcczyVZQTVq1bL2xWDtj4kX0F0XgdR96kI05cipYJbFqqjR9ksPjMV3JiLFcCXX6Bd1L0HJ4wW7bTwDzojuYqACr/JyAi4tX9Pz95wdk6SyJnC/QjbLtY8RddpWYilJpwJKWDJXdfc4fNxwOkVeSewaaVILpi11q+jvbqYcu+cAQ+dSmN+eKrfCGPypaWZSuGGkfgXjymVR+dhLY1WLBn4X5Q8= kpatronas@DESKTOP-B707TG6
Explaining the parameters
-y : This option will read a private OpenSSH format file and print an OpenSSH public key to stdout.
-f: The filename of the key file.
So if we redirect the output of the previous command to a file we have the our public key! (of-course not with the same path and filename as our private key, or else we will overwrite it).
Verify the match of public and private key
Using again the ssh-keygen command we can extract the fingerprint of both private and public keys, if it match then those keys are a valid pair of public and private key% ssh-keygen -l -f ~/.ssh/id_rsa.pub | cut -d " " -f2
SHA256:+urE9Bi7p6x3h+1CHUfME7whVx7dzk28FR9WwVcbPEQ% ssh-keygen -l -f ~/.ssh/id_rsa | cut -d " " -f2
SHA256:+urE9Bi7p6x3h+1CHUfME7whVx7dzk28FR9WwVcbPEQ
I hope you found this short article useful :)