Adding ssh-keys to GitHub account

HimanshuHimanshu
3 min read

For security reasons on GitHub, now users have to verify themselves before they can push, commit, etc on their GitHub account using Git.

Otherwise, you will get an error when you try to push your files to GitHub - "You do not have access to this repository. Check your rights."

There are 2 ways to verifications of users:

  • By adding ssh-keys, and

  • By adding PAT (Personal Access Token), when you set up a remote repository URL.

Steps to add ssh-keys:

  1.  #Generate the ssh-key
     ssh-keygen -t ed25519 -C "your_email@example.com"
    
  2.  #Location where your ssh key will be saved
     Enter a file in which to save the key (/home/YOU/.ssh/id_ALGORITHM):[Press enter]
    
  3.  #You can enter the passphrase if you want to, otherwise you can leave it empty by just using Enter
     Enter passphrase (empty for no passphrase): [Enter]
     Enter same passphrase again: [Enter]
    
  4.  #Before storing the ssh-key to ssh-agent, you have to start the ssh-agnet in the background
     ssh-agent bash
    
  5.  #If you created your key with a different name, or if you are adding an existing key that has a different name, replace id_ed25519 in the command with the name of your private key file.
     ssh-add ~/.ssh/id_ed25519
    
  6. Now add the ssh-keys to your GitHub account.

  7.  #Copy the public key of your generated ssh key
     cat ~/.ssh/id_ed25519.pub
     # Then select and copy the contents of the id_ed25519.pub file
     # displayed in the terminal to your clipboard
    
  8. Go to your GitHub account > Choose Setting > Select SSH and GPG keys.

  9. Click New SSHKey or Add SSH Key. Enter the name of the key (whatever you want) in the Title field. Select the type of key, authenticating or signing.

  10. In the key field, copy your public key.

  11. Click Add SSH Key.

Steps to add PAT:

  1. Go to your GitHub account > Choose Setting > Select or search for the Developer Setting at the bottom.

  2. Click on Personal Access Tokens > select Tokens(classic).

  3. Click on Generate New Token. Enter your password for verification.

  4. Select the expiration time of your token, and click on Generate Token.

  5. Copy the generated token in a file, as you will not able to see it again for security reasons.

    How you can use PAT to push your code/files to GitHub

  6. Before using "git push", you have to add the generated PAT token when you set up the remote URL.

  7.  git remote add origin https://your_PAT@github.com/your_repository_address.git
    

    Facing the following error when pushing the code to GitHub -

    ssh: connect to host github.com port 22: No route to host
    fatal: Could not read from remote repository.
    Please make sure you have the correct access rights and the repository exists.
    Solution :

    Change your internet connection. Sometimes if you are using your hostels/office/school internet service, they may have some firewall enabled that stops your connection to the GitHub.

    For more information, read the official documentation on GitHub.

0
Subscribe to my newsletter

Read articles from Himanshu directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Himanshu
Himanshu