How to Generate an SSH Key Pair Using ssh-keygen -t ed25519 -C "your_email@example.com" for GitHub

Umar Farooque Khan
5 min read2 days ago

If you’re looking to secure your remote connections and avoid using passwords, SSH keys are a great solution. One of the most common ways to generate an SSH key pair is by using the command ssh-keygen -t ed25519 -C "your_email@example.com". This command helps create a secure public/private key pair for SSH authentication. In this article, we’ll walk you through what this command does, how it works, and why it’s beneficial.

Breaking Down the Command

When you run the command ssh-keygen -t ed25519 -C "your_email@example.com", you're triggering the generation of a new SSH key pair. Let's break down each component of the command to understand what it’s doing:

  • ssh-keygen: This is the command-line tool used for generating SSH keys. It creates a private key and a public key, which are used for secure authentication. SSH keys are commonly used for tasks such as accessing remote servers, pushing code to Git repositories, or securely connecting to cloud services.
  • -t ed25519: The -t flag specifies the type of key to create. In this case, ed25519 is the algorithm chosen for key generation. Ed25519 is considered a modern and highly secure elliptic-curve algorithm, which is both fast and more resistant to attacks than the older RSA algorithm. Using this option ensures that you are creating a key with better cryptographic properties.
  • -C "your_email@example.com": The -C flag is used to add a comment to the key. It’s generally a good practice to add an identifying comment, often your email address, to help you identify the key later. When you list your SSH keys or upload the public key to a service like GitHub or GitLab, this comment helps to keep track of which key belongs to whom.

What Happens When You Run the Command?

Once you run the command, the ssh-keygen tool will begin the process of generating the SSH key pair. Here’s what you can expect:

  1. File Location Prompt: After entering the command, the system will ask you where you want to save the key. By default, it will suggest saving the key in ~/.ssh/id_ed25519. You can either accept the default path by pressing Enter or provide a custom file location.
  2. Passphrase (Optional): Next, you’ll be prompted to enter a passphrase to protect your private key. The passphrase is optional but adds an additional layer of security. If you choose to set a passphrase, you’ll need to enter it every time you use the key. If you prefer not to set one, simply press Enter to leave it empty.
  3. Key Generation: Once you’ve selected a location and optionally set a passphrase, ssh-keygen will generate the key pair. The process creates two files:
  • Private Key (id_ed25519): This is your private key and should never be shared. Keep it secure on your local machine.
  • Public Key (id_ed25519.pub): This is your public key, which can be shared with remote services like GitHub, GitLab, or any server you need to authenticate with.

Example: What You’ll See

When you run the command, you will see something like this in your terminal:

Generating public/private ed25519 key pair.
Enter file in which to save the key (/Users/your_user/.ssh/id_ed25519):

You can either press Enter to accept the default location or specify a custom file path.

Next, you’ll be prompted for a passphrase:

Enter passphrase (empty for no passphrase):
Enter same passphrase again:

After completing these steps, ssh-keygen will generate the SSH key pair, and you’ll see a confirmation message similar to this:

Your identification has been saved in /Users/your_user/.ssh/id_ed25519.
Your public key has been saved in /Users/your_user/.ssh/id_ed25519.pub.
The key fingerprint is:
SHA256:abcdefgh1234567890yourkeyfingerprint your_email@example.com
The key's randomart image is:
+---[ED25519 256]---+
| .o=. |
| . + . |
| . o +. . |
| . + * B . . |
|.o. +.E S |
| o. . o o |
|.oo. . |
|oo. |
|o... |
+----[SHA256]-------+

Using the SSH Keys

Now that you have your SSH key pair, you can use it for authentication. Here’s how it works:

  1. Private Key: The private key (id_ed25519) should remain on your local machine and be kept secure. It’s used to authenticate your identity when connecting to remote servers or services.
  2. Public Key: The public key (id_ed25519.pub) can be shared with services like GitHub, GitLab, or a remote server. You’ll add the public key to these services to enable secure, passwordless authentication.

How to Add Your SSH Key to GitHub

Now that your SSH key pair is ready, follow these steps to connect it to GitHub for secure authentication:

  1. Copy the Public Key to Clipboard: The public key is stored in the .pub file you generated earlier. You can copy its content using the following command:
cat ~/.ssh/id_ed25519.pub
  1. This will print the public key to the terminal. Select the entire key (starting with ssh-ed25519 and ending with your email) and copy it to your clipboard.

Add the SSH Key to GitHub:

  • Go to GitHub and log in to your account.
  • In the top-right corner, click your profile picture and go to Settings.
  • In the left sidebar, click SSH and GPG keys.
  • Click the New SSH key button.
  • In the Title field, add a label for the key (e.g., “MacBook Pro SSH Key”).
  • Paste the copied public key into the Key field.
  • Click Add SSH Key to save the key to your GitHub account.
  1. Test Your SSH Connection: After adding the key to GitHub, test the connection by running this command in your terminal:
ssh -T git@github.com
  1. If everything is set up correctly, you should see a message like this:
Hi your_username! You've successfully authenticated, but GitHub does not provide shell access.

This confirms that your SSH key has been successfully added to GitHub, and you can now authenticate with GitHub securely.

Conclusion

Using the ssh-keygen -t ed25519 -C "your_email@example.com" command, you can generate a secure SSH key pair for connecting to GitHub without the need to enter your password every time. This key pair will allow you to push code, pull repositories, and interact with GitHub securely and efficiently.

By following the steps outlined in this guide, you can easily set up SSH authentication for GitHub and streamline your workflow. SSH keys offer a reliable and safe way to authenticate with remote services, and now you’re ready to leverage them for your GitHub repositories.

--

--

Umar Farooque Khan
Umar Farooque Khan

Written by Umar Farooque Khan

Experienced software developer with a passion for clean code and problem-solving. Full-stack expertise in web development. Lifelong learner and team player.

No responses yet