Generating SSH Key and Adding it to the ssh-agent for Authentication on GitHub

Post Reply
User avatar
Eli
Senior Expert Member
Reactions: 189
Posts: 5943
Joined: 10 years ago
Location: Tanzania
Contact:

#1

When you git commit -m "Some comments"

You may get this message

git config --global user.email "you@example.com"
git config --global user.name "Your Name"

to set your account's default identity. Omit --global to set the identity only in this repository.

So this requires you to use your email and user name associated with your GitHub account to configure credentials for authentication, however, support for password authentication was removed on August 13, 2021.

What should you do then?

You can access and write data in repositories on GitHub.com using SSH (Secure Shell Protocol). When you connect via SSH, you will be required to authenticate using a private key file on your local machine. If you do not have an SSH key, you can generate a new SSH key on your local machine. After you generate the private-public key pair, you can add the public key to your account on GitHub.com to enable authentication for Git operations over SSH.

It's important to note that RSA keys (ssh-rsa) with a valid_after before November 2, 2021 may continue to use any signature algorithm, but RSA keys generated after that date must use a SHA-2 signature algorithm. Some older clients may need to be upgraded in order to use SHA-2 signatures. This type of encryption called asymmetric encryption also known as public key encryption, uses a public key-private key pairing. It uses two keys, one for encryption and one for decryption. The encryption key (also known as the public key) can be shared to others, while the decryption key (known as the private key) is confidential.

Steps to establishing the key-pairing for signing, committing or authentication includes executing the text below on a terminal, replacing the email used in the example with your GitHub email address:

  1. ssh-keygen -t ed25519 -C "your_email@example.com"

When you're prompted to "Enter a file in which to save the key", you can press Enter to accept the default file location. Please note that if you created SSH keys previously, ssh-keygen may ask you to rewrite another key, in which case it is recommended to create a custom-named SSH key. To do so, type the default file location and replace id_ALGORITHM with your custom key name.

Be aware that if you are using a legacy system that doesn't support the Ed25519 algorithm or unsure, use:

  1. ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

to create a new SSH key, using the provided email as a label.

In our case, we will use the first option.


Practical Example Creating and Using SSH Keys to Authenticate on GitHub


1. Check for existing SSH keys

First, check if you already have SSH keys set up on your computer. You can do this by running the following command in your terminal. Remember if you named your SSH key files differently.

  1. ls -al ~/.ssh


Look for files named id_rsa (private key) and id_rsa.pub (public key) or any other files you used to save the keys. If they exist, you can proceed to the next step. If not, you will need to generate a new SSH key pair.

2. Generate a new SSH key pair

If you don't have an SSH key pair, you can generate one by running the following command in your terminal:

  1. ssh-keygen -t rsa -b 4096 -C "your_email@example.com"


Follow the prompts to create a new SSH key pair. Make sure to enter a passphrase to add an extra layer of security. Entering a passphrase is optional, leave blank and hit Enter if you do not need it.

Note:

When you generate an SSH key, you can add a passphrase to further secure the key. Whenever you use the key, you must enter the passphrase. If your key has a passphrase and you don't want to enter the passphrase every time you use the key, you can add your key to the SSH agent. The SSH agent manages your SSH keys and remembers your passphrase. See in a sequel, Adding your SSH key to the ssh-agent.

You may face the "Permission denied" error when trying to save the SSH key to the specified file path. Sometimes, the permission issue can be resolved by running the ssh-keygen command with elevated privileges using sudo. You can try running the following command:

  1. sudo ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

This will prompt you to enter your administrator password to run the command with elevated permissions. After running the command, you will see more or less similar prompts as below:

  1. $sudo ssh-keygen -t rsa -b 4096 -C "administrator@tssfl.com"
  2. [sudo] password for tssfl:
  3. Generating public/private rsa key pair.
  4. Enter file in which to save the key (/root/.ssh/id_rsa):
  5. Enter passphrase (empty for no passphrase):
  6. Enter same passphrase again:
  7. Your identification has been saved in /root/.ssh/id_rsa
  8. Your public key has been saved in /root/.ssh/id_rsa.pub
  9. The key fingerprint is:
  10. SHA25WeElvT2puyp5OFs65reYNvkNLyynV1x3LGyHyPoAUs administrator@tssfl.com
  11. The key's randomart image is:
  12. +---[RSA 4096]----+
  13. |             E T  |
  14. |       + o  . o  |
  15. |      * C .  8. o |
  16. |       @ =    ..=|
  17. |      B C. S..  +.++|
  18. |     . oo =   *o |
  19. |      .. X Wo ... |
  20. |     o  =.*o .. .|
  21. |    . ...oo+=. . |
  22. +----[SHA256]-----+


3. Check for the created SSH keys:

  1. $ls -al ~/.ssh

This gives the output similar to

  1. total 84
  2. drwx------  2 tssfl tssfl  4096 Apr 20 21:32 .
  3. drwxr-x--- 39 tssfl tssfl 40960 Apr 20 21:05 ..
  4. -rw-------  1 tssfl tssfl   576 Sep 16  2023 authorized_keys
  5. -r--------  1 tssfl tssfl  2610 Sep 15  2023 id_rsa
  6. -rw-------  1 tssfl tssfl  3389 Sep 16  2023 id_rsa_local
  7. -rw-r--r--  1 tssfl tssfl   747 Sep 16  2023 id_rsa_local.pub
  8. -rw-r--r--  1 tssfl tssfl   575 Sep 15  2023 id_rsa.pub
  9. -rw-r--r--  1 tssfl tssfl   583 Sep 16  2023 id_rsa.pub.save
  10. -rw-------  1 tssfl tssfl  1910 Apr 20 21:32 known_hosts
  11. -rw-rw-r--  1 tssfl tssfl   725 Sep 23  2023 known_hosts2
  12. -rw-------  1 tssfl tssfl  1074 Apr 20 21:32 known_hosts.old


4. Adding your SSH key to the ssh-agent

Before adding a new SSH key to the ssh-agent to manage your keys, you should have checked for existing SSH keys and generated a new SSH key.

4 (a) Start the ssh-agent in the background

Execute

  1. $eval "$(ssh-agent -s)"

Output:

  1. Agent pid 97882

Depending on your environment, you may need to use a different command. For example, you may need to use root access by running sudo -s -H before starting the ssh-agent, or you may need to use exec ssh-agent bash or exec ssh-agent zsh to run the ssh-agent.

4 (b) Add your SSH private key to the ssh-agent

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:

  1. ssh-add ~/.ssh/id_ed25519

In our case:

  1. $ssh-add ~/.ssh/id_rsa

Output:

  1. Identity added: /home/tssfl/.ssh/id_rsa (tssfl@TSSFL-ThinkBook)


5. Add your SSH public key to your GitHub account

Copy the contents of your SSH public key (id_rsa.pub file) by running:

  1. cat ~/.ssh/id_rsa.pub

Output:

  1. ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQCUC1jmx+Cr24o6PImq88DvoZCH7zNALj3UNdJ28ihIkCu7fbWt8CxP4NW7uu+FL4MFt7dugC0Tv95hsuvnb3tX+ruyRTFpKkgqaKiPF1T2SWK+MNRgGeK5mb3m/A6G9Cchfs+OUzfHjHq6AWC7rikhfDg1hO+CWBA9x3mOeLAtBVIXGaGyYPrSGgi43Z3yRgcXBQ31b+QSoLJcxfOuXZNWjkEqtYp8jXOWFU7OfcCgKTS2uAvFr8F1+bEXczSk+Lxd3b6QeIXojeZpsJ3QQzqK3UbiEcTOEkPGpWt5tG82LN/mrvs13RFuDn4zhEv/9w6BAy9gsaoxvsQ7Qni0rUn8qtcItN8O9MZyz+2d+Mo91m0QbBWAO5S6jPOg8BXteSAgmH5QY7yIGFgIkvFfH8eK5ZH5lF/OvgJXiBMokVvFl48oX7CnxF2Grh+15YQAU2BHYhfCG0rLmGlbOdDozeQ4P4UIKk7NlK6Ih37DEipNiLPAAVdjrfhOZfWDWAgaCzM= tssfl@TSSFL-ThinkBook

Then, go to your GitHub account settings, navigate to "SSH and GPG keys," and click on "New SSH key." Paste the copied public key into the "Key" field and save it.


6. Use SSH URL


Double-check that you are using the SSH URL when cloning or setting the remote repository in Git:

  1. git remote add origin git@github.com:username/repository.git
  2. git remote set-url origin git@github.com:username/repository.git

For example:

  1. git remote add origin git@github.com:TSSFL/Graphs.git
  2. git remote set-url origin git@github.com:TSSFL/Graphs.git

Without running these commands, you may be asked to supply credentials and get the error message below, for example when you try to push local contents on Github, git push -u origin master:

  1. Username for 'https://github.com': TSSFL
  2. Password for 'https://TSSFL@github.com':
  3. remote: Support for password authentication was removed on August 13, 2021.
  4. remote: Please see https://docs.github.com/get-started/getting-started-with-git/about-remote-repositories#cloning-with-https-urls for information on currently recommended modes of authentication.


7. Test your SSH connection

To test your SSH connection to GitHub, run the following command in your terminal:

ssh -T git@github.com

You should see a message confirming that you've successfully authenticated with GitHub:

  1. $ssh -T git@github.com
  2. Hi TSSFL! You've successfully authenticated, but GitHub does not provide shell access.

8. Start Using Git and GitHub

You can now start carrying out various git and GitHub operations, such as:

  1. $git push -u origin master
  2.  
  3. Enumerating objects: 6, done.
  4. Counting objects: 100% (6/6), done.
  5. Delta compression using up to 12 threads
  6. Compressing objects: 100% (6/6), done.
  7. Writing objects: 100% (6/6), 4.65 KiB | 1.55 MiB/s, done.
  8. Total 6 (delta 0), reused 0 (delta 0), pack-reused 0
  9. remote:
  10. remote: Create a pull request for 'master' on GitHub by visiting:
  11. remote: https://github.com/TSSFL/Graphs/pull/new/master
  12. remote:
  13. To github.com:TSSFL/Graphs.git
  14.  * [new branch] master -> master
  15. branch 'master' set up to track 'origin/master'.

See some reference:

viewtopic.php?t=5137
0
TSSFL -- A Creative Journey Towards Infinite Possibilities!
User avatar
Eli
Senior Expert Member
Reactions: 189
Posts: 5943
Joined: 10 years ago
Location: Tanzania
Contact:

#2

Sometimes git pull origin master will give raise to the following message if you have divergent branches that needs to be reconciled, usually the local and the remote repositories:

From github.com:Username/Repository_Name
* branch master -> FETCH_HEAD
hint: You have divergent branches and need to specify how to reconcile them.
hint: You can do so by running one of the following commands sometime before
hint: your next pull:
hint:
hint: git config pull.rebase false # merge
hint: git config pull.rebase true # rebase
hint: git config pull.ff only # fast-forward only
hint:
hint: You can replace "git config" with "git config --global" to set a default
hint: preference for all repositories. You can also pass --rebase, --no-rebase,
hint: or --ff-only on the command line to override the configured default per
hint: invocation.

fatal: Need to specify how to reconcile divergent branches.

To address this problem, you can issue one of the following commands depending on how you would like to reconcile branches:
  • git config pull.rebase false: This command sets the configuration option for the pull command to use merge strategy instead of rebase. When pulling changes from a remote repo, this setting will result in a merge commit being created in the local repo to integrate changes.
  • git config pull.rebase true: This command sets the configuration option for the pull command to use rebase strategy when pulling changes from a remote repo. This setting will rebase the local branch onto the remote branch instead of creating a merge commit.
  • git config pull.ff only: This command sets the configuration option for the pull command to only allow fast-forward merges when pulling changes from a remote repo. This means that if the local branch has not diverged from the remote branch, the pull operation will simply fast-forward the local branch to the latest commit on the remote branch. If a fast-forward merge is not possible, the pull operation will fail.
Then the GNU Nano text editor will open, asking you to write comment:

#Please enter a commit message to explain why this merge is necessary, especially if it merges an updated upstream into a topic branch.
#Lines starting with '#' will be ignored, and an empty message aborts the commit.

Write your comment(s) and then follow these steps to save and exit from the GNU nano text editor:
  • Press Ctrl key and O keys simultaneously - This will prompt you to enter a filename to save the changes. Press Enter to confirm the filename.
  • Press Ctrl key and X key simultaneously - This will exit the nano text editor.
  • Your changes will be saved and you will exit the editor.
You can then git pull origin master and continue to edit and commit the code to GitHub.
0
TSSFL -- A Creative Journey Towards Infinite Possibilities!
Post Reply

Return to “Git, Mercurial, GitHub and Bitbucket”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 1 guest