SSH
public key
access
authentication
keys management

How do I access my SSH public key?

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

To efficiently work with servers, secure shell (SSH) is a vital protocol that enables encrypted network connectivity. It relies heavily on SSH keys for authentication. An SSH key pair includes a public key, which is stored on the server, and a private key, which remains with the user. This article explains how to access your SSH public key, offering insights and examples for both beginners and seasoned professionals.

SSH Key Pair Basics

An SSH key pair consists of two keys:

  • Public Key: This can be freely shared with any SSH server you wish to connect to. It's typically stored in the ~/.ssh/authorized_keys file on the server.
  • Private Key: This must be kept secure and private on your local machine. It's used to authenticate your client when connecting to an SSH server.

For secure operations, it's crucial to ensure only the public key is stored on the server, while the private key is safely secured. The common format for these keys is OpenSSH.

Locating Your SSH Public Key

The default location for storing SSH keys on a Unix-based system is in the .ssh directory within your home directory:

bash
~/.ssh/id_rsa
~/.ssh/id_rsa.pub
  • id_rsa: Your private key (keep this secret)
  • id_rsa.pub: Your public key (can be shared)

How to View Your SSH Public Key

To view your SSH public key, you need to read the id_rsa.pub file. Here’s how you can do it:

  1. Open a Terminal: Launch your terminal application.
  2. Run the Command: Use cat to display the contents of your public key file:
bash
   cat ~/.ssh/id_rsa.pub

This command will display your SSH public key, looking something like this:

 
   ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAr1N... user@hostname
  1. Copy the Key: Simply copy the output and paste it where required, such as into the authorized_keys file on a server you want to access.

Generating SSH Key Pairs

If you do not have SSH keys, you can generate them using the ssh-keygen command:

bash
ssh-keygen -t rsa -b 4096 -C "[email protected]"
  • -t rsa: Specifies the type of key to create.
  • -b 4096: Sets the number of bits in the key, enhancing security.
  • -C "[email protected]": Adds a comment. It's optional but useful for identifying the key.

During generation, you will be prompted to save the key to the default location and create a passphrase (a secure, optional password).

Managing Multiple SSH Keys

In some cases, you might need multiple SSH keys for different services. Here’s how you can manage them:

  • Naming Keys: Create separate key files for each service, like id_rsa_service1 and id_rsa_service2.
  • SSH Config File: Use the ~/.ssh/config file to manage these keys efficiently:
plaintext
1  Host service1.com
2      HostName service1.com
3      User username
4      IdentityFile ~/.ssh/id_rsa_service1
5
6  Host service2.com
7      HostName service2.com
8      User username
9      IdentityFile ~/.ssh/id_rsa_service2

Common SSH Key File Permissions

Proper permissions are crucial to securing your SSH key files:

  • ~/.ssh directory should have permissions set to 700.
  • Private keys (id_rsa) should have permissions of 600.
  • Public keys (id_rsa.pub) can have permissions set to 644.

You can modify permissions using chmod:

bash
chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_rsa
chmod 644 ~/.ssh/id_rsa.pub

Troubleshooting SSH Key Issues

Common Problems and Solutions

IssueSolution
Permission denied (publickey)Ensure your public key is added to the server's authorized_keys file.
Private key invalid formatCheck that the private key is not corrupted or improperly generated.
Agent forwarding issuesVerify SSH agent is running and holds the correct identity.
Incorrect file permissionsEnsure correct permissions on ~/.ssh directory and private key file.

Understanding how to access and manage your SSH public key is key to utilizing secure, password-less authentication. By following the guidance provided in this article, users can ensure secure, hassle-free access to their servers, while mitigating common security risks associated with SSH key management.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.