Connecting to GitHub with SSH
Git
Install Git
On Windows, download git for windows.
On Linux, running the command sudo apt-get install git
to install git.
Verify that the installation was successful:
git --version |
Git Settings
Setting your user name and email for git
git config --global user.name "taogen" |
Check your git settings
git config user.name |
Checking for existing SSH keys
Before you generate an SSH key, you can check to see if you have any existing SSH keys.
Open Terminal or Git Bash
Enter
ls -al ~/.ssh
to see if existing SSH keys are present.Check the directory listing to see if you already have a public SSH key. By default, the filenames of supported public keys for GitHub are one of the following.
id_rsa.pub
id_ecdsa.pub
id_ed25519.pub
Either generate a new SSH key or upload an existing key.
Generating a new SSH key and adding it to the ssh-agent
Generating a new SSH key
Open Terminal or Git Bash
Paste the text below, substituting in your GitHub email address.
$ ssh-keygen -t ed25519 -C "your_email@example.com"
Note: If you are using a legacy system that doesn’t support the Ed25519 algorithm, use:
$ ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
After running the above command, you need to enter a file path or use the default file path and enter a passphrase or no passphrase.
Generating public/private ALGORITHM key pair.
Enter file in which to save the key (C:/Users/YOU/.ssh/id_ALGORITHM):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in C:/Users/YOU/.ssh/id_ALGORITHM.
Your public key has been saved in C:/Users/YOU/.ssh/id_ALGORITHM.pub.
The key fingerprint is:
SHA256:24EfhoOdfZYXtdBt42wbDj7nnbO32F6TQsFejz95O/4 your_email@example.com
The key's randomart image is:
+--[ED25519 256]--+
| .. o|
| . .++|
| o++.|
| o = .ooB.|
| . S = =o* +|
| * =.+ =o|
| . o .+=*|
| +=O|
| .oBE|
+----[SHA256]-----+
Adding your SSH key to the ssh-agent
You can secure your SSH keys and configure an authentication agent so that you won’t have to reenter your passphrase every time you use your SSH keys.
- Ensure the ssh-agent is running.
Start it manually:
# start the ssh-agent in the background |
Auto-launching the ssh-agent Configuration
You can run ssh-agent
automatically when you open bash or Git shell. Copy the following lines and paste them into your ~/.profile
or ~/.bashrc
file in Git shell:
env=~/.ssh/agent.env |
- Add your SSH private key to the ssh-agent.
If your private key is not stored in one of the default locations (like ~/.ssh/id_rsa
), you’ll need to tell your SSH authentication agent where to find it. To add your key to ssh-agent, type ssh-add ~/path/to/my_key
.
$ ssh-add ~/.ssh/id_ed25519 |
Adding a new SSH key to your GitHub account
Open Terminal or Git Bash. Copy the SSH public key to your clipboard.
$ pbcopy < ~/.ssh/id_ed25519.pub
# Copies the contents of the id_ed25519.pub file to your clipboardor
$ clip < ~/.ssh/id_ed25519.pub
# Copies the contents of the id_ed25519.pub file to your clipboardor
$ cat ~/.ssh/id_ed25519.pub
# Then select and copy the contents of the id_ed25519.pub file
# displayed in the terminal to your clipboardGitHub.com -> Settings -> Access - SSH and GPG keys -> New SSH key
Testing your SSH connection
After you’ve set up your SSH key and added it to your account on GitHub.com, you can test your connection.
Open Terminal or Git Bash
Enter the following command
$ ssh -T git@github.com
# Attempts to ssh to GitHubIf you see the following message, you have successfully connected GitHub with SSH.
> Hi USERNAME! You've successfully authenticated, but GitHub does not
> provide shell access.