SSH key setup

SSH key setup

Publish Date: Apr 20
7 0

Setting up an SSH key and adding it to your GitHub account is a great way to securely connect to GitHub without needing to enter your username and password every time. Here's a step-by-step guide:


Step 1: Check for Existing SSH Keys

Before generating a new SSH key, check if you already have one:

  1. Open a terminal (on macOS/Linux) or Git Bash (on Windows).
  2. Run the following command to check for existing SSH keys:
   ls -al ~/.ssh
Enter fullscreen mode Exit fullscreen mode

Look for files like id_rsa.pub, id_ecdsa.pub, or id_ed25519.pub. These are your public SSH keys.

If you don't have any, proceed to the next step.


Step 2: Generate a New SSH Key

  1. Run the following command to generate a new SSH key:
   ssh-keygen -t ed25519 -C "your_email@example.com"
Enter fullscreen mode Exit fullscreen mode

Replace your_email@example.com with your GitHub email address.

If you're using an older system that doesn't support ed25519, use:

   ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
Enter fullscreen mode Exit fullscreen mode
  1. When prompted to "Enter a file in which to save the key," press Enter to accept the default location.

  2. You'll be asked to enter a passphrase. This is optional but recommended for added security.


Step 3: Add the SSH Key to the SSH Agent

  1. Start the SSH agent in the background:
   eval "$(ssh-agent -s)"
Enter fullscreen mode Exit fullscreen mode
  1. Add your SSH private key to the SSH agent:
   ssh-add ~/.ssh/id_ed25519
Enter fullscreen mode Exit fullscreen mode

If you used a different name for your key, replace id_ed25519 with the correct filename.


Step 4: Copy the SSH Key to Your Clipboard

  1. Display your SSH public key:
   cat ~/.ssh/id_ed25519.pub
Enter fullscreen mode Exit fullscreen mode

If you used a different name for your key, replace id_ed25519.pub with the correct filename.

  1. Copy the output to your clipboard. On macOS, you can use:
   pbcopy < ~/.ssh/id_ed25519.pub
Enter fullscreen mode Exit fullscreen mode

On Linux, use:

   xclip -selection clipboard < ~/.ssh/id_ed25519.pub
Enter fullscreen mode Exit fullscreen mode

On Windows (Git Bash), use:

   clip < ~/.ssh/id_ed25519.pub
Enter fullscreen mode Exit fullscreen mode

Step 5: Add the SSH Key to Your GitHub Account

  1. Go to GitHub and log in to your account.
  2. Click on your profile picture in the top-right corner and select Settings.
  3. In the left sidebar, click SSH and GPG keys.
  4. Click the New SSH key button.
  5. Give your key a title (e.g., "My Laptop").
  6. Paste the SSH key you copied earlier into the "Key" field.
  7. Click Add SSH key.

Step 6: Test Your SSH Connection

  1. Run the following command to test your SSH connection to GitHub:
   ssh -T git@github.com
Enter fullscreen mode Exit fullscreen mode
  1. If everything is set up correctly, you'll see a message like:
   Hi username! You've successfully authenticated, but GitHub does not provide shell access.
Enter fullscreen mode Exit fullscreen mode

Step 7: Use SSH for Git Operations

Now, when you clone a repository, use the SSH URL instead of HTTPS. For example:

git clone git@github.com:username/repository.git
Enter fullscreen mode Exit fullscreen mode

For existing repositories, you can update the remote URL to use SSH:

git remote set-url origin git@github.com:username/repository.git
Enter fullscreen mode Exit fullscreen mode

That's it! You've successfully set up an SSH key and added it to your GitHub account. 🎉

Comments 0 total

    Add comment