Introduction
In this article, I'll walk through the process of setting up and troubleshooting SSH key authentication for connecting to GitHub repositories on a Windows system using Git Bash. SSH key authentication provides a secure and convenient way to interact with GitHub without entering your username and password repeatedly.
Prerequisites
Before you begin, ensure you have the following:
- Git installed on your Windows machine. Download here
- A GitHub account. If you don't have one, you can sign up at GitHub.
Steps to Set Up SSH Key Authentication
1. Generate SSH Key Pair
- Open Git Bash on your Windows machine.
- Create a new ssh folder:
mkdir ssh
- Change your directory into that folder:
cd ssh
- Generate a new SSH key pair by running the following command:
ssh-keygen -t rsa -C "your-email-address" -f "github-username"
Replace with your email address and username associated with your GitHub account.
- Optionally, provide a passphrase for extra security.
2. Start SSH Agent and Add SSH Key
- You can now change directory back into your default root:
cd ..
- Start the SSH agent by running:
eval $(ssh-agent -s)
- Add your SSH private key to the SSH agent:
ssh-add /c/Users/USER/.ssh/id_rsa
Replace /c/Users/USER/.ssh/id_rsa
with the actual file location.
3. Add SSH Public Key to GitHub Account
- Change directory into the ssh folder again:
cd ssh
- Copy the contents of your SSH public key (
id_rsa.pub
):
cat id_rsa.pub
replace "id_rsa.pub" with the name of your ssh with the .pub
extension.
- Log in to your GitHub account.
- Navigate to
Settings > SSH and GPG keys
. - Click on
New SSH key
. - Paste your SSH public key into the
Key
field and give it a descriptive title. - Click
Add SSH key
to save.
4. Configure SSH for GitHub
- Change directory into the ssh folder again:
cd ssh
- Open or create the SSH configuration file (
config
) in the.ssh
directory:
touch config.txt
it is a normal txt file so fine ways to add the information below to it, you can use vim too.
- Add the following SSH configuration for GitHub in the config file:
Host github.com
HostName github.com
User git
IdentityFile "/c/Users/USER/.ssh/id_rsa"
Replace /c/Users/USER/.ssh/id_rsa
with the path to your SSH private key. Don't forget the double quote if you are on windows, use this if you are on mac:
~/.ssh/id_rsa
5. Test SSH Connection to GitHub
- Test the SSH connection to GitHub:
ssh -T git@github.com
- If prompted to confirm the authenticity of the host, type
yes
and pressEnter
.
Troubleshooting
- Permission Denied (publickey) Error: If you encounter this error, ensure that your SSH key is added to the SSH agent and correctly configured in your GitHub account and SSH configuration file.
-
Path Formatting (Windows): Use forward slashes (
/
) and quotes (" "
) for file paths in the SSH configuration file (config
) on Windows.
Conclusion
By following these steps, you should now have SSH key authentication set up and working with GitHub on your Windows machine. This method provides a secure and efficient way to interact with GitHub repositories using Git commands without the need to enter your GitHub credentials repeatedly.
Feel free to share this article with others who may encounter similar issues when setting up SSH key authentication with GitHub on Windows.
Top comments (0)