Find SSH keys in Ubuntu
How To

How to Find SSH Keys in Ubuntu

Sagar Sharma
Sagar Sharma

Table of Contents

There are multiple reasons why one would want to find the SSH keys in Ubuntu such as one may want to inspect if the keys are generated in the first place or not.

If not, the user can generate SSH keys to move ahead. Another reason can be a troubleshooting where you start with locating keys.

Sure, you may have your own reasons. But the question is how do you find the SSH keys in Ubuntu Linux?

Well if you stored the SSH keys in the default directory, then you can use the ls command to list the contents of the .ssh directory:

ls ~/.ssh/
Use the ls command to find the SSH keys in ubuntu

Want more details? Here you have it.

Find SSH keys in Ubuntu

In this tutorial, I will walk you through two ways to find SSH keys in Ubuntu or any other Linux distribution:

  • By listing the contents of the .ssh directory
  • Using the find command (useful if you used a different directory to store keys)

Let's start with the first one.

Find SSH keys by listing the contents of the SSH directory

While generating the SSH it will ask you to decide the directory in which you want to store the SSH keys and if you went with the default settings, you can use this method.

To list the content of the ~/.ssh directory (yes, it is a hidden directory), use the ls command as shown here:

ls ~/.ssh
find ssh keys in ubuntu using the ls command

Here, you will find 3 files (or 4 if you've been using SSH for a long time) and each has a meaning:

  • id_rsa: the private SSH key which is not meant to be shared with anyone and used for user authentication.
  • id_rsa.pub: the public key which can be freely shared with anyone.
  • known_hosts: it stores the public keys of the hosts that were accessed by a user.
  • known_hosts.old: it is a backup created by your system when changes are made to the original known_hosts file.
💡
If you don't see any files in .ssh or if you don't see .ssh folder at all (even when displaying the hidden files), it means you haven't generated SSH keys yet.

To learn more about the known_hosts file, I would recommend checking out the detailed guide on known_hosts in Linux:

What is known_hosts File in Linux [Everything to Know]
There is a known_hosts file in the .ssh directory and it is an integral part of the SSH mechanism. Learn more about it.

Find SSH keys using the find command

Imagine you used a different location to store SSH keys and now you don't know what directory it was. That's where this method kicks in to help you out.

All you have to do is execute the find command in the following manner:

find / -name "id_rsa*" 2>/dev/null
Use the find command to find the SSH keys in Ubuntu

The above command will search for the id_rsa title in the root directory and will redirect errors to the /dev/null.

More on SSH

You can use the secure copy protocol to send and receive files from the server and client. Want to know how? Here's a detailed guide to using SCP command:

SCP Remote to Local [Practical Examples]
Learn to copy files from the remote server to the local system with the scp command in this quick Ubuntu tutorial.

I hope you will find this guide helpful.



Sagar Sharma

Sagar Sharma

A software engineer who loves to tinker with hardware till it gets crashed. While reviving my crashed system, you can find me reading literature, manga, or watering my plants.