Git and GitHub SSH KeyGen by Example
In order to push, pull and clone securely between your local Git installation and a remote GitHub or GitLab repository, you must first create an SSH key pair. This both identifies you and authenticates your local Git installation with the remote GitHub or GitLab server to which you are attempting to connect.
GitHub SSH KeyGen Tools
There are numerous ways to create SSH keys for Git. I often used PuttyGen as my GitHub SSH KeyGen tool of choice, especially if I was working in a Windows environment. But for Ubuntu and Windows 10 or 11 users, there’s a much better GitHub SSH KeyGen tool that comes pre-installed on the OS. It’s aptly named ssh-keygen.
To create SSH keys for GitHub and Git, simply open up a Terminal window in Ubuntu or PowerShell in Windows and run the ssh-keygen command.
SSH KeyGen on Windows and Ubuntu
On Ubuntu, the GitHub SSH KeyGen command is invoked like this:
ssh@keygen~/.ssh$ ssh-keygen -o -t rsa -C "[email protected]"
On Windows the SSH KeyGen command is invoked like this:
PS C:\ssh\keygen\example> ssh-keygen -o -t rsa -C "[email protected]"
By the way, SSH support on Windows has been around since 2018. As long as your installation is up to date, you don’t need Putty, PuttyGen or Plink to use GitHub SSH KeyGen tools.
Post SSH Key Generation Tasks |
---|
After you’ve run the ssh keygen command, you’ll need to push, pull and clone with Git. Here are some quick tutorials to show you how:
|
The GitHub SSH KeyGen tool generates two files that map to the public SSH key and the private SSH key. The SSH keys are name is_rsa and id_rsa.pub.
ssh@keygen~/.ssh$ ls is_rsa id_rsa.pub
GitHub SSH KeyGen Options | ||
---|---|---|
KeyGen Option | Meaning | Usage |
-t | Create a DSA, RSA or ECDSA key | RSA |
-o | Use the new OpenSSH format | Accept default |
-C | Metadata appended to the public SSH key |
Git SSH Key Locations
The ssh-keygen command will ask if you want to place the generated keys in a custom folder. Reject that option and accept the defaults.
On both Linux and Windows, SSH keys must go in a folder named .ssh located off the root of the user’s home directory. For example, on Windows where the user who ran the GitHub SSH KeyGen command was named Owner, the public and primary keys would be listed from the following folder:
PS C:\Users\Owner\.ssh> dir Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 1/28/2022 8:02 PM 2602 id_rsa -a---- 1/28/2022 8:02 PM 572 id_rsa.pub
Copy the public SSH key
The contents of the public key file named id_rsa.pub must be copied and pasted into the GitHub SSH config window. So, cat the file if you’re on Ubuntu, or open the file in a text editor on Windows, and copy the key.
GitHub SSH Config
With the value of the public key copied to the clipboard, log into GitHub and navigate to your account settings. There you will see an SSH and GPG link. Open this link and then add a new SSH key.
Paste in the value of the public key into the text field and give the key a unique name. Save your changes and your GitHub SSH key setup is almost complete.
Clone GitHub repos with SSH
Each GitHub repository has a green Code button that provides CLI, HTTP and a GitHub SSH URL to be used with the git clone command. The SSH link for the repo to be cloned in this GitHub SSH KeyGen example is:
[email protected]:cameronmcnz/github-made-real-easy.git
To clone your repository, simply open a Terminal Window in Ubuntu or a command prompt in windows and issue the following Git clone command:
PS C:\ssh\keygen\example> git clone [email protected]:cameronmcnz/github-made-real-easy.git Cloning into ‘github-made-real-easy’… Authenticity of GitHub SSH KeyGen Example host ‘ (131.2.14.9)’ can’t be established. Do you still want to connect(yes or no)? yes Receiving objects: 100% (222/222), 2.23 MiB | 3.41 MiB/s, done. Resolving deltas: 100% (111/111), done.
There will be a warning about authenticating the SSH key. Just type in ‘yes’ when the prompt occurs and allow the repository to clone.
Push to GitHub with SSH KeyGen Keys
Once the SSH connection to GitHub is successfully established, all subsequent push, pull and fetch operation will execute over a secure connection using the keys generated by the SSH keygen operation. No extra flags or options are required.
An SSH push to GitHub from a repository that was already cloned over SSH looks like this:
PS C:\ssh\keygen\example> git pull origin Everything up to date!
And that’s how you securely connect Git to GitHub with SSH KeyGen generated keys.