DEV Community

Cover image for Set up SSH to auth GitHub in a Terminal session
Emmanuel Morales
Emmanuel Morales

Posted on • Edited on

Set up SSH to auth GitHub in a Terminal session

Verify the existence of the directory ~/.ssh

  • Let's verify if we have a directory called .ssh in the root (cd ~). We can verify by changing to that directory cd ~/.ssh

Creating the .ssh directory

  • If we don't have the directory then we create it: mkdir ~/.ssh.

Generate a new key

  • If we have the directory, then we generate a key by
ssh-keygen
Enter fullscreen mode Exit fullscreen mode
  • We press ENTER to save the key in our directory /Users/user-name/.ssh/id_rsa

  • If we want a password that protect the key, we enter a passphrase and click ENTER, and we repeat the same passphrase settled later and press ENTERagain.

    • If we don't want to, we just left it empty and press ENTER.

Add the public password to Github

  • Look and copy the public password:
cat id_rsa.pub
Enter fullscreen mode Exit fullscreen mode

Image description

  • We go to Settings profile:
    Image description

  • Then we move to SSH and GPG keys:
    Image description

  • And we add a New SSH key by adding a Title, the use of that key (in our case Authentication key), and the public key generated from the public password (cat id_rsa.pub).
    Image description

    • Note: The key we use is the public one, from the id_rsa.pub, NOT the id_rsakey.

Adding an agent to hold the ssh key.

An agent holds the public and private key for authenticate into services, in our case GitHub.

  • Start the agent:
eval $(ssh-agent -s)
Enter fullscreen mode Exit fullscreen mode

Image description

  • Add the password to the agent:
ssh-add ~/.ssh/id_rsa
Enter fullscreen mode Exit fullscreen mode

Image description

Testing

Now in our session is validated to use push and clone on our public and private repositories.

Basic Test

git init
git add .
Enter fullscreen mode Exit fullscreen mode
  • Then we add the SSH link from a GitHub Repository:
git remote add origin git@github.com:user/Repo_Name
Enter fullscreen mode Exit fullscreen mode

Image description

  • We make our commit:
git commit -m "First Commit"
Enter fullscreen mode Exit fullscreen mode

So when we do some git push -u origin master the authentication will be settled.

Top comments (0)