DEV Community

Cover image for GitHub SSH Key Authentication and SSH Config
Ricardo
Ricardo

Posted on • Originally published at rmauro.dev on

GitHub SSH Key Authentication and SSH Config

By setting up SSH keys for GitHub, you can securely connect to your repositories without the hassle of repeatedly entering your username and password.

This guide will walk you through the process of setting up and using SSH keys with GitHub via the SSH config file.

While many developers use the ssh-agent to manage their SSH keys, an alternative method involves configuring the SSH config file for even smoother operations.

πŸ“’Table of Contents

🧾 Benefits of Using SSH Keys with GitHub

SSH keys offer a secure method of authentication, utilizing a pair of cryptographic keys – one public and one private.

GitHub SSH Key Authentication and SSH Config
SSH Github Connection

When configured correctly, SSH keys provide:

  • Enhanced Security: SSH keys are virtually immune to brute-force attacks.
  • Convenience: Automate your GitHub interactions without repeatedly typing your credentials.
  • Efficiency: Speed up your workflow with seamless and secure connections.

πŸš— Set Up Step by Step

Let's dive into the step-by-step process of generating and using SSH keys with GitHub.

Generate an SSH Key

The first step is to generate a new SSH key on your local machine. This key will be used to authenticate your GitHub account.

  • Open your terminal. On macOS and Linux, you can use the built-in Terminal application. On Windows, you might use Git Bash or any terminal emulator that supports SSH.
  • Generate the key. Use the ssh-keygen command to create a new SSH key pair. Replace your_email@example.com with your GitHub email address.
ssh-keygen -t ed25519 -C "your_email@example.com"
Enter fullscreen mode Exit fullscreen mode

Generates Keys using ed25519

If your system does not support the Ed25519 algorithm, use RSA instead.

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
Enter fullscreen mode Exit fullscreen mode

Generates Keys using RSA 4096

  • Follow the prompts : You'll be asked to enter a file name to save the key. Press Enter to accept the default location (/home/you/.ssh/id_ed25519 or /home/you/.ssh/id_rsa).
  • Set a passphrase : For added security, you can enter a passphrase. Press Enter if you prefer not to use one.

Add SSH key to the Github account

Copy the SSH public key.

The public key is usually located in ~/.ssh/id_ed25519.pub (or ~/.ssh/id_rsa.pub for RSA).sh

Go to GitHub and navigate to Settings > SSH and GPG keys > New SSH key.

By the way. Follow me on Github. https://github.com/ricardodemauro

GitHub SSH Key Authentication and SSH Config

Github Profile

Give your SSH key a descriptive title and paste the public key into the key field. Click Add SSH key.

GitHub SSH Key Authentication and SSH Config

Add SSH Key Page

Configure the SSH Config File

To manage your SSH keys without using ssh-agent, configure your SSH config file. This file allows you to define specific settings for SSH connections.

Open the SSH config file. Use a text editor to open or create the SSH config file located at ~/.ssh/config.sh

nano ~/.ssh/config
Enter fullscreen mode Exit fullscreen mode

Add the following configuration to the file. This setup specifies the SSH key for GitHub.

Host github.com
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_ed25519
Enter fullscreen mode Exit fullscreen mode

For RSA keys, adjust the IdentityFile path accordingly. IdentityFile ~/.ssh/id_rsa

Save the changes and close the text editor.

Test the SSH Connection

Finally, verify that your setup works correctly by testing the SSH connection to GitHub.

Execute the following command to test your SSH connection.sh

ssh -T git@github.com
Enter fullscreen mode Exit fullscreen mode

If the configuration is correct, you should see a message like.

Hi username! You've successfully authenticated, but GitHub does not provide shell access.

Conclusion

Configuring SSH keys with GitHub using the SSH config file offers a streamlined and secure way to manage your repository interactions.

By following these steps, you can enhance your development workflow, ensuring secure and seamless connections without the need for ssh-agent.

Embrace the power of SSH keys and optimize your GitHub experience.

Happy coding! For this and more check out https://rmauro.dev

Top comments (0)