Linux Commands

How to Find SSH Public Key

In some situations, you may need to view the contents of your SSH keys. For example, you may need to view the contents of a public key to add it to remote services requiring SSH authentication, such as Google Cloud. This article shows you how to view SSH key contents using a simple cat command in Linux.

How to Generate an SSH Key

The first step to setting up an SSH key is to generate a pair. An SSH-key pair contains a public and private key. Using the public and private pair, you can authenticate a user to a remote host.

In Linux, use the following command to generate an SSH key pair:

ssh-keygen

The above command will require you to input information to set up and create the keys. If you are on unsecured networks or critical systems, be sure to encrypt your keys with a passphrase.

Generating public/private rsa key pair.
Enter file in which to save the key (/home/ubuntu/.ssh/id_rsa):
Created directory '/home/ubuntu/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/ubuntu/.ssh/id_rsa
Your public key has been saved in /home/ubuntu/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:hVkOnzk7nLWx3j4vqLv/B83tYN7w3juLAbFw610xh7Q ubuntu@CSALEM
The key's randomart image is:
+---[RSA 3072]----+
|        . .   .  |
|         B o . o |
|        o.Boo Eo.|
|         oo=++  +|
|        S =+o  +.|
|          .oo.* +|
|           ..*.B |
|            ..*.*|
|          +=.ooOB|
+----[SHA256]-----+

NOTE: You must have the OpenSSH package installed on your system to use the ssh-keygen command.

How to View an SSH Key

The first method that you can use to view your SSH key is by using a simple cat command. This command will print the file’s contents, which you can copy and paste to the remote host. By default, SSH keys are stored in the /home/$USER/.ssh

To view the contents:

cd ~/.ssh
cat id_rsa.pub

The above command will print the contents of your SSH public key. The following is an example key:

ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQC4P7J4iUnK+lbKeBxEJqgBaapI6/tr2we9Ipr9QzYvAIzOyS396uYRhUldTL0sios0BlCes9k9FEU8/ZFABaPlvr/UcM/vBlVpEv1uCkq1Rg48bK8nWuCBcLmy2B+MUoiXT/0W51qT2fSYRUk0fafnxvBnqRidRdOpRZtxMKjvsSua+tU5AciEuYJ+L4X32UF2sHe6o+GzAyItK5ZzpneiEPfoHUSJ4N7+wUcrTI52NPrHmH11jzLPpMHxoqiDBzF2IIVxxU1GSioGAij7T5Sf6aWDOnBHnpeJBFujChg+p2WPlha+B2NaCt25eBtwPMMFQqmJ38xoPr1BCtF6ViOR1e2e7rk/+XML3ypZU8mawhJbl6IqfzRtn5C8dP6vGqMg30kW9vIp4GqlbGLMeAyuBsA45rNnVqxtiMXdKcHPvA+Mmbm+7YSXzoyQcuRUzJY9K+Y+ty7XQPmwYgvT7bvtFvC5B9wWAqt5qgmTToLp7qHLCXK+m/6rpJp7d57tGv0= ubuntu@UBUNTU

Another method that you can use to view the contents of your SSH key is by using the Open-SSH authentication tool with the command shown below:

ssh-agent sh -c "ssh-add; ssh-add -L"

This command will prompt you for an authentication password for the passphrase if one is assigned, as follows:

Enter passphrase for /home/ubuntu/.ssh/id_rsa:
Identity added: /home/ubuntu/.ssh/id_rsa (ubuntu@CSALEM)
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQC4P7J4iUnK+lbKeBxEJqgBaapI6/tr2we9Ipr9QzYvAIzOyS396uYRhUldTL0sios0BlCes9k9FEU8/ZFABaPlvr/UcM/vBlVpEv1uCkq1Rg48bK8nWuCBcLmy2B+MUoiXT/0W51qT2fSYRUk0fafnxvBnqRidRdOpRZtxMKjvsSua+tU5AciEuYJ+L4X32UF2sHe6o+GzAyItK5ZzpneiEPfoHUSJ4N7+wUcrTI52NPrHmH11jzLPpMHxoqiDBzF2IIVxxU1GSioGAij7T5Sf6aWDOnBHnpeJBFujChg+p2WPlha+B2NaCt25eBtwPMMFQqmJ38xoPr1BCtF6ViOR1e2e7rk/+XML3ypZU8mawhJbl6IqfzRtn5C8dP6vGqMg30kW9vIp4GqlbGLMeAyuBsA45rNnVqxtiMXdKcHPvA+Mmbm+7YSXzoyQcuRUzJY9K+Y+ty7XQPmwYgvT7bvtFvC5B9wWAqt5qgmTToLp7qHLCXK+m/6rpJp7d57tGv0= ubuntu@UBUNTU

Conclusion

This article showed you how to generate an SSH key, as well as two methods that you can use to view the content of an SSH key. For the most part, you will only need to view the content within public keys, and not private keys. Always make sure to protect your SSH keys at all times. Secure your Shell!

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list