Using the SSH protocol, you can connect and authenticate to remote servers and services. With SSH keys, you can connect to GitHub without supplying your username and personal access token at each visit.
Algorithm | Public key | Private key |
---|---|---|
ED25519 (preferred) | id_ed25519.pub |
id_ed25519 |
RSA (at least 2048-bit key size) | id_rsa.pub |
id_rsa |
DSA (deprecated) | id_dsa.pub |
id_dsa |
ECDSA | id_ecdsa.pub |
id_ecdsa |
-
Open Terminal.
-
Enter ls -al ~/.ssh to see if existing SSH keys are present:
$ ls -la ~/.ssh # Lists the files in your .ssh directory, if they exist
-
Check the directory listing to see if you already have a public SSH key. By default, the filenames of the public keys are one of the following:
- id_rsa.pub
- id_ecdsa.pub
- id_ed25519.pub
-
Open a terminal.
-
Type
ssh-keygen -t
followed by the key type and an optional comment. This comment is included in the.pub
file that's created. You may want to use an email address for the comment, but it is optional.For example, for ED25519:
ssh-keygen -t ed25519 -C "<comment>"
-
Press Enter. Output similar to the following is displayed:
Generating public/private ed25519 key pair. Enter file in which to save the key (/home/user/.ssh/id_ed25519):
-
Accept the suggested filename and directory, unless you want to save in a specific directory where you store other keys.
-
Specify a passphrase, or leave empty to not be prompted for a password on every commit (less secure, but more convenient)
Enter passphrase (empty for no passphrase): Enter same passphrase again:
-
A confirmation is displayed, including information about where your files are stored.
To use SSH with GitLab, copy your public key to your GitLab account.
-
Copy the contents of your public key file. You can do this manually or use a script. For example, to copy an ED25519 key to the clipboard:
macOS:
tr -d '\n' < ~/.ssh/id_ed25519.pub | pbcopy
Linux (requires the
xclip
package):xclip -sel clip < ~/.ssh/id_ed25519.pub
Git Bash on Windows:
cat ~/.ssh/id_ed25519.pub | clip
Replace
id_ed25519.pub
with your filename. For example, useid_rsa.pub
for RSA. -
Sign in to GitHub.
-
In the top right corner, select your avatar.
-
Select Settings.
-
From the left sidebar, select SSH and GPG Keys.
-
In the top right, select New SSH key
-
In the Key box, paste the contents of your public key. If you manually copied the key, make sure you copy the entire key, which starts with
ssh-ed25519
orssh-rsa
, and may end with a comment. -
In the Title text box, type a description, like Work Laptop or Home Workstation.
-
Select Add SSH key.
- Open the terminal.
- Change directories to where your ssh keys are stored.
cd ~/.ssh/
- Create a
config
file and insert this into it.Host github.com HostName github.com User git IdentityFile ~/.ssh/id_ed25519 PreferredAuthentications publickey PasswordAuthentication no IdentitiesOnly yes ProxyCommand /usr/bin/nc -x 127.0.0.1:3129 %h %p
- Save the file and exit