
- Git - Home
- Git - Version Control
- Git - Basic Concepts
- Git - Command Line
- Git - Installation
- Git - First Time Setup
- Git - Basic Commands
- Git - Getting Help
- Git - Tools
- Git - Cheat Sheet
- Git - Terminology
- Git - Life Cycle
- Git - Get a Repository
- Git - Adding New Files
- Git - Recording Changes
- Git - Viewing Commit History
- Git Branching
- Git - Branches in a Nutshell
- Git - Creating a New Branch
- Git - Switching Branches
- Git - Branching and Merging
- Git - Merge Conflicts
- Git - Managing Branches
- Git - Branching Workflows
- Git - Remote Branches
- Git - Tracking Branches
- Git - Rebasing
- Git - Rebase vs. Merge
- Git - Squash Commits
- Git Operations
- Git - Clone Operation
- Git - Tagging Operation
- Git - Aliases Operation
- Git - Commit Operation
- Git - Stash Operation
- Git - Move Operation
- Git - Rename Operation
- Git - Push Operation
- Git - Pull Operation
- Git - Fork Operation
- Git - Patch Operation
- Git - Diff Operation
- Git - Status Operation
- Git - Log Operation
- Git - Head Operation
- Git - Origin Master
- Git Undoing
- Git - Undoing Changes
- Git - Checkout
- Git - Revert
- Git - Reset
- Git - Restore Operation
- Git - Rm
- Git - Switch Operation
- Git - Cherry-pick
- Git - Amend
- Git on the Server
- Git - Local Protocol
- Git - Smart HTTP Protocol
- Git - Dumb HTTP Protocol
- Git - The SSH Protocol
- Git - The Git Protocol
- Git - Getting Git on a Server
- Git - Setting up the Server
- Git - Daemon
- Git - GitWeb
- Git - GitLab
- Git - Third Party Hosted Options
- Distributed Git
- Git - Distributed Workflows
- Git - Contributing to a Project
- Git - Maintaining a Project
- Customizing Git
- Git - Configuration
- Git - Hooks
- Git - Attributes
- Git - Init
- Git - Commit
Git - Generating SSH Key
Users authenticate with Git servers by utilizing SSH public keys, which they need to generate if they do not already have them.
SSH key generation is done in the same way on all operating systems.
Users need to make sure they have a key before generating new ones.
The user's ~/.ssh directory contains SSH keys by default.
Use these commands to list the contents of the ~/.ssh directory and check if you already have a SSH key:
$ cd ~/.ssh $ ls authorized_keys2 id_dsa known_hosts config id_dsa.pub
To generate a SSH key with Git services for secure authentication, you need to follow the following steps:
1. Open a Terminal
Open up the terminal on your respective operating system, such as Git Bash in Windows.
2. Generate the SSH Key
To generate a new SSH key pair using RSA (recommended) with a 4096-bit key size, run the following command:
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
-t rsa − Signifies the type of key to be created (RSA).
-b 4096 − Signifies the bit-size, where 4096 is for strong encryption.
-C "your_email@example.com" − Signifies the email id with which the SSH key gets associated and used for identification.
3. Specify the Location
In order to save the key, you will be asked to specify a location. You can specify a desired location or press enter to accept the default location provided, which is ~/.ssh/id_rsa.
Enter file in which to save the key (/home/your_user/.ssh/id_rsa):
4. Set a Passphrase (optional)
A passphrase can also be added, though it is optional, it adds an extra layer of security. This step can be skipped even.
Enter passphrase (empty for no passphrase):
5. View the Generated SSH Key
After following the steps mentioned above, your SSH key pair will be created. You will have two files, one for a private key and other for public key.
Private Key − You need to keep this file safe and do not share it ~/.ssh/id_rsa
Public Key − You need to share this file with services ~/.ssh/id_rsa.pub
6. Add the SSH Key to the SSH Agent
In order to ensure the SSH Key is available for Git services, you need to add it to the SSH agent.
-
Start the agent.
eval "$(ssh-agent -s)"
-
Add your key.
ssh-add ~/.ssh/id_rsa
7. Copy the Public Key
Add the content of the public key file id_rsa.pub to your Git service (GitHub, GitLab, BitBucket, etc.). You can use the following command to display the public key:
cat ~/.ssh/id_rsa.pub
Select and copy the output to be added to the Git Service. The actual public key content is a large string of characters (AAAAB3NzaC1yc2EAAAABIwAAAQEA2kLx2K5DwsUgrA8WqA+zm4e5+JY1ZqD0Gi?.......).
8. Add SSH Key to Git Service
GitHub
To add the SSH Key to GitHub, go to Settings > SSH and GPG keys and click New SSH key.
GitLab
To add the SSH Key to GitLab, go to Profile Settings > SSH keys and paste the public key.
BitBucket
To add the SSH Key to BitBucket, go to Personal Settings > SSH keys and add a new key.
Once the key has been added to your Git service, you can clone, push, and pull the repositories using SSH.