As a tech note to myself, this is how I set up access to GitHub.com via SSH
on Windows 10 and on Ubuntu Linux 18.10.
In order to keep this short and to the point, let’s assume that both SSH and Git has already been setup.
I’m using Git for Windows 2.21.0.
The Git installation comes with bundled OpenSSH, so I do not need the
GIT_SSH environmental variable. If
GIT_SSH exists, Git will use explicitly the specified application whenever it attempts to use SSH instead of the OpenSSH.
This little detail really tripped me over for a while as because of some past setup I had
GIT_SSH pointing to plink.exe while I had been eagerly configuring OpenSSH. How odd it was that Git kept failing with SSH (plink.exe) while direct SSH (OpenSSH) connections worked just fine…
Step 1: Generate a RSA Key Pair
$ ssh-keygen -t rsa -b 4096
In the dialogue that follows, I chose to store the new key as
Mind you that the above notation is as it is used in the Git Bash. Two new files were created:
C:\Users\<user>\.ssh\github_id_rsa (private key) C:\Users\<user>\.ssh\github_id_rsa.pub (public key)
Step 2: Edit SSH Config File
I added following entry to the
C:\Users\<user>\.ssh\config (and if the file does not exist, just create a new one):
Host github.com Hostname github.com User git IdentityFile C:\Users\<user>\.ssh\github_id_rsa
Step 3: Store Public Key to GitHub Account
I opened the
C:\Users\<user>\.ssh\github_id_rsa.pub file, which contains a long string, which begins with “
ssh-rsa ...“, and then copied the text to clipboard.
- I opened my profile Settings / SSH and GPG keys
- Clicked New SSH key
- Named the key in Title
- Pasted the public key’s text into Key
- Clicked Add SSH key
The page lists all added SSH keys. The new one shows at this point as Never used – Read/write.
Step 4: Test SSH Connection to GitHub
In Git Bash I tried if the basic SSH authentication works. Note that one must always use the user git instead of own GitHub user account.
$ ssh -T email@example.com
If all goes well, the server responds:
"Hi <github user>! You've successfully authenticated, but GitHub does not provide shell access."
If the attempt fails, try
ssh -Tv firstname.lastname@example.org and figure it out. I did, eventually.
Step 5: Add the Private Key to SSH-Agent
If the ssh-agent is not already running, start it with following command:
$ eval $(ssh-agent -s)
If the agent is running it’s pid is shown. Next add the private key to the ssh-agent (again, in Git Bash).
$ ssh-add /c/Users/<user>/.ssh/github_id_rsa
If successful, following message is shown:
Identity added: /c/users/<user>/.ssh/github_id_rsa (<user>@<host>)
Step 6: Clone a Repository
One of my public repositories is mstenback/reference_java, which contains small reference applications. (While they are mostly small and simple, I like them for being good reference point as I implement the same applications also in other langauges and scripts, such as Python, Bash, Golang and so on. It’s works for me as way to learn new languages.)
$ git clone email@example.com:mstenback/reference_java
Note that the repository must be cloned by using the SSH notation instead of HTTPS.
Step 7: Commit and Push Changes
After making a small change to
README.md I committed the change
$ git commit README.md
and then pushed the changes to GitHub
$ git push
At this point, after a long evening trying to figure this out, I allowed myself feel a bit pleased when the connection finally worked as intended.
Finally, as a minor detail, if I would check my SSH key status in GitHub profile, it would now show: “Last used within the last week – Read/write“
The Same with the Ubuntu Linux
1) SSH key files are located in
2) Using OpenSSH 7.7p1 (not that it makes any practical difference in this case).