SSH
Check for existing SSH keys¶
Before you generate an SSH key, you can check to see if you have any existing SSH keys.
1. Open Terminal
1. Enter ls -al ~/.ssh
to see if existing SSH keys are present:
ls -al ~/.ssh
# Lists the files in your .ssh directory, if they exist
By default, the filename of the public keys is the following: - id_rsa.pub
If you don't have an existing public and private key pair, or don't wish to use any that are available to connect to GitHub, then generate a new SSH key.
Generating a new SSH key¶
- Open Terminal.
- Paste the text below, substituting in your GitHub email address.
This creates a new ssh key, using the provided email as a label.
ssh-keygen -t rsa -b 4096 -C "[email protected]"
Generating public/private rsa key pair.
- When you're prompted to "Enter a file in which to save the key," press Enter. This accepts the default file location.
Enter a file in which to save the key (/Users/you/.ssh/id_rsa): [Press enter]
- At the prompt, type a secure passphrase. For more information, see "Working with SSH key passphrases".
Enter passphrase (empty for no passphrase): [Type a passphrase] Enter same passphrase again: [Type passphrase again]
Setup a non standard port for our GitLab in ~/.ssh/config¶
- Run
edit ~/.ssh/config
and add the following# Qloud.io GitLab server Host gitlab.com Port 22 User git EOL
Adding your SSH key to the ssh-agent¶
Before adding a new SSH key to the ssh-agent, you should have checked for existing SSH keys and generated a new SSH key.
- Ensure ssh-agent is enabled:
# start the ssh-agent in the background eval "$(ssh-agent -s)" Agent pid 59566
- Add your SSH key to the ssh-agent. If you used an existing SSH key rather than generating a new SSH key, you'll need to replace id_rsa in the command with the name of your existing private key file.
$ ssh-add ~/.ssh/id_rsa
- Add the SSH public key to your GitLab account.
Working with non-default SSH key pair paths¶
- If you used a non-default file path for your GitLab SSH key pair, you must configure your SSH client to find your GitLab SSH private key for connections to your GitLab server
- For OpenSSH clients this is configured in the ~/.ssh/config file. Below are two example host configurations using their own key:
# Our GitLab server Host gitlab.com User git Port 22 IdentityFile ~/.ssh/config/private-key-filename
Testing your SSH connection¶
After you've set up your SSH key and added it to your GitLab account, you can test your connection.
Before testing your SSH connection, you should have: - Checked for existing SSH keys - Generated a new SSH key - Added a new SSH key to your GitLab account
When you test your connection, you'll need to authenticate this action using your password, which is the SSH key passphrase you created earlier. 1. Open Terminal. 1. Enter the following:
ssh -T [email protected] # Attempts to ssh to GitLab
You may see this warning:
The authenticity of host 'gitlab.com (35.231.145.151)' can't be established. ECDSA key fingerprint is SHA256:HbW3g8zUjNSksFbqTiUWPWg2Bq1x8xdGUrliXFzSnUw. Are you sure you want to continue connecting (yes/no)?
- Verify that the fingerprint in the message you see matches the following
SHA256:HbW3g8zUjNSksFbqTiUWPWg2Bq1x8xdGUrliXFzSnUw
, then type:yes
- You should then get a response like this:
Warning: Permanently added 'gitlab.com' (ECDSA) to the list of known hosts. Welcome to GitLab, @USERNAME!
- On the next try you should get a message where you see your name and surname:
Welcome to GitLab, @USERNAME!
- If you see this error message:
You have probably missed the step to add the ssh key to GitLab
Permission denied (publickey).