To set up an SSH key for secure authentication with your Git repositories, follow these steps:
**Generating an SSH Key:**
1. Open a terminal window and create an SSH key by executing the following command:
$ ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
**Choosing a Key File Name:**
2. Choose a meaningful name for your key file based on the service you're using, such as `github_rsa` for GitHub or `gitlab_rsa` for GitLab.
**Copying the Public Key:**
3. Copy the public key to your clipboard from the following path:
$ ~/.ssh/github_rsa.pub
Note: If you didn't specify a name during key generation, it will be saved as `id_rsa.pub` by default.
**Adding the Key to Your GitHub Account:**
4. Navigate to your GitHub account and access the SSH keys settings page.
5. Click on "New SSH Key."
6. Paste the SSH key from step 3 into the "Key" field and provide a descriptive title.
7. Click "Add Key" to complete the process of adding the SSH key to your GitHub account.
--------------
In case you encounter a "permission denied" issue, follow these additional steps:
**Resolving Permission Issues:**
1. rename known_hosts to known_hosts.old
$ mv ~/.ssh/known_hosts ~/.ssh/known_hosts.old
2. Run the following command to start the SSH agent:
$ eval "$(ssh-agent -s)"
3. Add your SSH key to the agent:
$ ssh-add ~/.ssh/github_rsa
--------------
note: if the problem still exist read the following.
You need to tell Git which SSH key to use when connecting to GitHub.
You can do this by editing your:
~/.ssh/config
file or creating it if it doesn't exist.
Add the following lines to the file which name is config:
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/github_rsa
By following these steps, you'll establish a secure SSH key-based authentication method for your Git interactions, ensuring a smoother and more secure workflow.