- Setup Git in WSL
- Install Git
- Setup global configuration settings
- Checking configuration settings
- Sharing an existing SSH key between Windows and WSL
- Creating a new SSH key
- Your public key
- Set up Git
- Using Git
- Setting up Git
- Authenticating with GitHub from Git
- Connecting over HTTPS (recommended)
- Connecting over SSH
- Next steps
- Help and support
- Help us make these docs great!
- cham11ng / git-ssh-configuration.md
Setup Git in WSL
The purpose of this post is to detail how to setup Git within the Windows Subsystem for Linux (WSL). Git is the source control tool of choice nowadays. It’s distributed style makes it great for working with a remote team but also allows you to work completely offline, which wasn’t an option for previous tools like Subversion or CVS.
This post focuses on setting up Git for command line access and scripting on Windows. There are other options like Git BASH, which I have used in the past, but why settle for a “BASH emulation” when you can use the real thing?
Install Git
Git appears to come as standard as part of the WSL install. You can test this by running:
$ git --version git version 2.17.1
If for some reason Git is not installed then you can simply pull it down:
Setup global configuration settings
First up you need to configure your name and email address, e.g:
$ git config --global user.name "Pete O'Shea" $ git config --global user.email "mail@peteoshea.co.uk"
This is used to mark you as the author of the changes you make. I also recommend turning off the auto line endings feature as this can hide certain issues especially when working with both Linux and Windows. There is another config setting to achieve this:
$ git config --global core.autocrlf false
If you prefer you can just edit the global config for the current user directly:
This does use the default editor, which seems to default to Nano. If you’ve never used Nano before you may prefer to just edit the file directly with something like Vi:
Anyway this file should look something like:
# This is Git's per-user configuration file. [user] name = Pete O'Shea email = "mail@peteoshea.co.uk" [core] autocrlf = false
Checking configuration settings
If you want to check your settings you can run:
$ git config --list --show-origin file:/home/pete/.gitconfig user.name=Pete O'Shea file:/home/pete/.gitconfig user.email=mail@peteoshea.co.uk file:/home/pete/.gitconfig core.autocrlf=false
This shows you the settings and the value they have been set to. It also shows which configuration file the values come from so if you run this within a git repository for example there may be some extra project specific settings included.
Sharing an existing SSH key between Windows and WSL
If you have an SSH key already setup on Windows you could reuse it rather than creating a new one. (Note that PuTTY keys do not work here). To do this you can just copy the key from the Windows filesystem into the WSL’s filesystem. Linux has some rules about how visible the key is. So you need to make sure the access levels are strict enough to meet these requirements:
$ cd ~ $ mkdir .ssh $ chmod 700 .ssh $ cd .ssh $ cp /mnt/c/Users/user/.ssh/id_rsa* . $ chmod 600 id_rsa $ chmod 644 id_rsa.pub
Creating a new SSH key
If you do not already have an SSH key then you could generate one in WSL:
$ ssh-keygen -t rsa -b 4096 -C "mail@peteoshea.co.uk"
Save the key as id_rsa in the .ssh directory in your home directory, e.g. /home/pete/.ssh/id_rsa for user pete.
As mentioned earlier you will likely want to copy this file back to the Windows system. A good reason for this is that the WSL distro could be uninstalled or a second system could be setup and you don’t want to lose your key or have to setup a new key for each environment on the same machine.
If .ssh isn’t listed then create it now:
Now you can copy these files so that Windows and WSL can use the same key:
This gives you the additional benefit that you have a copy of the key on the main Windows filesystem. This is very useful if you remove the specific WSL Linux distribution app, or want to use multiple distros.
One additional step here though might be to set the id_rsa file to be read-only in Windows. Using chmod from WSL doesn’t seem to work so you’ll have to do this in Windows Explorer.
Your public key
To make use of your key the public key needs to be added to remote systems. This will then allow secure connections using your private key. The details of the public key can be retrieved with:
Add this public SSH key to the services you use e.g. GitHub and Bitbucket. You may also want to add it to the authorized_keys file on remote servers to allow SSH access using the key.
Set up Git
At the heart of GitHub is an open-source version control system (VCS) called Git. Git is responsible for everything GitHub-related that happens locally on your computer.
Using Git
To use Git on the command line, you will need to download, install, and configure Git on your computer. You can also install GitHub CLI to use GitHub from the command line. For more information, see «About GitHub CLI.»
If you want to work with Git locally, but do not want to use the command line, you can download and install the GitHub Desktop client. For more information, see «Installing and configuring GitHub Desktop.»
If you do not need to work with files locally, GitHub lets you complete many Git-related actions directly in the browser, including:
Setting up Git
- Install a terminal emulator such as Termux from the Google Play Store on your Chrome OS device.
- From the terminal emulator that you installed, install Git. For example, in Termux, enter apt install git and then type y when prompted.
Authenticating with GitHub from Git
When you connect to a GitHub repository from Git, you will need to authenticate with GitHub using either HTTPS or SSH.
Note: You can authenticate to GitHub using GitHub CLI, for either HTTP or SSH. For more information, see gh auth login .
Connecting over HTTPS (recommended)
If you clone with HTTPS, you can cache your GitHub credentials in Git using a credential helper. For more information, see «About remote repositories» and «Caching your GitHub credentials in Git.»
Connecting over SSH
If you clone with SSH, you must generate SSH keys on each computer you use to push or pull from GitHub. For more information, see «About remote repositories» and «Generating a new SSH key and adding it to the ssh-agent.»
Next steps
You now have Git and GitHub all set up. You may now choose to create a repository where you can put your projects. Saving your code in a repository allows you to back up your code and share it around the world.
- Creating a repository for your project allows you to store code in GitHub. This provides a backup of your work that you can choose to share with other developers. For more information, see “Create a repository.».
- Forking a repository will allow you to make changes to another repository without affecting the original. For more information, see «Fork a repo.»
- Each repository on GitHub is owned by a person or an organization. You can interact with the people, repositories, and organizations by connecting and following them on GitHub. For more information, see «Be social.»
- GitHub has a great support community where you can ask for help and talk to people from around the world. Join the conversation on GitHub Community.
Help and support
Help us make these docs great!
All GitHub docs are open source. See something that’s wrong or unclear? Submit a pull request.
cham11ng / git-ssh-configuration.md
We can check if Git is installed by typing git —version in your Terminal or Git Bash (Windows) respectively.
Download Git for Windows from this link https://git-scm.com/downloads
Configuration
Open your Terminal or Git Bash(Windows).
$ ssh -v usage: ssh [-1246AaCfGgKkMNnqsTtVvXxYy] [-b bind_address] [-c cipher_spec] [-D [bind_address:]port] [-E log_file] [-e escape_char] [-F configfile] [-I pkcs11] [-i identity_file] [-L address] [-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port] [-Q query_option] [-R address] [-S ctl_path] [-W host:port] [-w local_tun[:remote_tun]] [user@]hostname [command]
List the contents of your ~/.ssh directory.
If we have already setup default identity, we can see two id_ files
$ ls -a ~/.ssh . .. id_rsa id_rsa.pub known_hosts
The system adds keys for all identities to the /home//.ssh (Linux) or /c/Users//.ssh (Windows) directory. The following procedure creates a default identity.
Hit ssh-keygen command on Terminal or Git Bash.
$ ssh-keygen Generating public/private rsa key pair. Enter file in which to save the key (/c/Users/yourname>/.ssh/id_rsa):
Press enter to accept the default key and location or we can set somewhere else.
We enter and re-enter a passphrase when prompted. Unless you need a key for a process such as script, you should always provide a passphrase. The command creates your default identity with its public and private keys
$ ssh-keygen Generating public/private rsa key pair. Enter file in which to save the key (/c/Users/yourname>/.ssh/id_rsa): Created directory '/c///.ssh'. Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /c/Users/yourname>/.ssh/id_rsa. Your public key has been saved in /c/Users/yourname>/.ssh/id_rsa.pub. The key fingerprint is: SHA256:+V6cLhFWngIFC+TdHwQHxJP39lsZUWnP6TDj1/7hBVA yourname>@yourname> The key's randomart image is: +---[RSA 2048]----+ | .o .==+o E +| | . o.o+oo. + | | . o..=oo..+| | .+.o*ooo| | S. oo.*.+| | . o =+| | ..+ .o+| | ..o ..+| | . .o| +----[SHA256]-----+
List the contents of your ~/.ssh directory.
$ ls ~/.ssh . .. id_rsa id_rsa.pub known_hosts
Ensure ssh-agent is enabled:
$ eval "$(ssh-agent -s)" # Agent pid 59566
Add your SSH key to the ssh-agent
- Find and take a note of your public key fingerprint. If you’re using OpenSSH 6.7 or older:
$ ssh-add -l # 2048 a0:dd:42:3c:5a:9d:e4:2a:21:52:4e:78:07:6e:c8:4d /Users//.ssh/id_rsa (RSA) If you are using OpenSSH 6.8 or newer: $ ssh-add -l -E md5 # 2048 MD5:a0:dd:42:3c:5a:9d:e4:2a:21:52:4e:78:07:6e:c8:4d /Users//.ssh/id_rsa (RSA)
$ cat ~/.ssh/id_rsa.pub ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC2zeZVIph1tP0UZJ007AC1OWqThpYjDlao1PlQnZbrSMeS8LXkU/nMxuZdAv+2JeqhezOtb6/e8e50NOTWB9Z2O8thCMwc29cp6C+vHL2oWQYMcCOuT34/R2yDEOMQ5nkIZ1fVFJNCTIZUaKjyaHX89w0v2p9cMsZ1q36w9lEdKXs8N5fuN/6rAy3JQgMcbD+dDd0cWpP8CLiUyNCq32xwqhX+nS1P43AgOQdLpX74uljwr7rE2CmrJQkvh/m+h68tv8+mLMGJtg5cJ+doZ+9r9yPhKJYGEsW4bL+8sSRQn3gJWUib8xhOgaWrMfXj+94o1KbcI12lK772GNyP74rX yourname>@yourname>
Copy this output to respective SSH keys setting.
You have now successfully configured SSH for Git in Windows/Linux/Mac OS.