close
close
How to view your SSH keys on Linux, macOS and Windows

There may be times when you actually need to view your SSH certificates on Linux. Why? For example, you need to add a certificate to authenticate on GitHub (or another online service that requires SSH authentication). You know you created these SSH certificates, but how do you view them?

Anyone familiar with SSH probably already knows the answer to this question. After all, this is pretty simple SSH stuff. For those new to SSH (or Linux, macOS, or Windows) methods, the task may seem overwhelming.

In this article and video tutorial above, I’ll show you how easy it is to view these SSH keys so you can use them for third-party services.

SEE: How to create and copy SSH keys with 2 simple commands (TechRepublic)

What you need

All you need is access to a server or desktop (Linux, macOS or Windows) and a created SSH key. If you haven’t created your SSH key pair yet, you can do so with the following command:

ssh-keygen

This command generates a key pair, both public and private keys. The public key is the one you send to servers for SSH key authentication. When you try to log in to this server, SSH compares the public and private keys. If these keys match, you get access. Very easy. You are ready to move on.

How to view your SSH public key in Linux

There are two easy ways to view your SSH public key on Linux: using the cat command or using the ssh-agent and ssh-add commands, which is a bit more complicated. The second method is probably overkill for what you need, but it’s a good way to view the key while needing your SSH key pair password.

Using the ssh-agent command

  1. Run the command:
    ssh-agent sh -c 'ssh-add; ssh-add -L'
  2. After successful authentication, your public SSH key will be printed out in the terminal.
  3. You can then copy and paste that wherever you need it.

If you don’t want to remember another command, you can just use the cat command.

Using the cat command

  1. Run the command:
    cat ~/.ssh/id_rsa.pub
  2. The command prints your SSH key on your Linux computer without prompting you for your key authentication password.

SEE: How to mount remote directories using SSH (TechRepublic)

How to view your SSH public key on macOS

Viewing your keys on MacOS can be done in a similar way to Linux.

How to view SSH public key on macOS

  1. Open your terminal window and run the command
    cat ~/.ssh/id_rsa.pub
    or
    cat /Users/USERNAME/.ssh/id_rsa.pub
    Where USERNAME is your macOS username.
  2. The above commands will print out your SSH public key.

macOS also has another nifty trick up its sleeve. The pbcopy tool allows you to copy the contents of the SSH key directly to the clipboard without displaying the key.

How to copy SSH public key on Mac

  1. Run the command:
    cat ~/.ssh/id_rsa.pub | pbcopy
  2. Once you have copied the key to your clipboard, you can paste it wherever you want.

SEE: How to use an SSH configuration file on macOS for easier connections to your data center servers (TechRepublic)

How to view your SSH public key on Windows

If you haven’t generated an SSH key yet, you can generate one now.

How to generate an SSH public key on Windows

  1. Run the command:
    ssh-keygen
  2. You will be asked where you want to save the key.
  3. If you use the default settings, your keys will be stored in C:\Users(yourusername).sshid.
  4. You will then be asked to enter a passphrase. You can press Enter to enter an empty/no passphrase.
  5. You will then be asked to confirm the passphrase.
  6. Your key will be generated.

How to view your SSH public key on Windows

  1. Run the command:
    type ~\.ssh\id_rsa.pub
    Where USERNAME is the name of your user.
  2. The command displays your public SSH key.
  3. Now use the keyboard shortcut Ctrl+C to copy the contents of the file.

You can also do something similar to what we did on macOS (copy the SSH public key directly to the clipboard).

How to copy SSH public key on Windows

  1. Issue the command:
    type ~\.ssh\id_rsa.pub | clip
    Where USERNAME is your username.
  2. Now paste this key into a word processing program or wherever you need it.

SEE: Top commands Linux administrators need to know (TechRepublic Premium)

How to view your private key

Chances are, you’ll never have to see your private key. After all, this is the secret of the sauce, which is not visible to anyone. However, if you don’t need this key, you can follow the same steps as above, but in any case remove the .pub file from the filename.

Remember that id_rsa is the private key and id_rsa.pub is the public key. And that’s all you need to view your public and private SSH keys on Linux, macOS and Windows.

Be sure to treat these keys with the care and security they deserve. Although your public key is shared with other users and services, this private key must be hidden and never shown to the public.

If you accidentally share this private key, you must do the following:

  1. Remove the public key from the authorized_keys file on each server that uses the key pair.
  2. Delete the public and private keys on the host.
  3. Generate a new key pair.
  4. Send the new key pair to the servers you need to log in to using SSH key authentication.

If you leave traces of this compromised key pair on a server or desktop, you risk giving someone access.

Leave a Reply

Your email address will not be published. Required fields are marked *