SSH keys provide a secure way to authenticate yourself to GitLab without the need for a username and password. In this step-by-step guide, we'll walk you through the process of generating an SSH key and attaching it to your GitLab account.
Before we begin, ensure you have the following:
- A GitLab account: You must have a GitLab account to link your SSH key to.
- Git installed: Git is a version control system and is usually already installed on most systems. You can check if it's installed by running `git --version` in your terminal. If it's not installed, download and install it from the official website “https://git-scm.com/”.
To get started, open your terminal or command prompt. You'll need to use the terminal to generate your SSH key.
1. To generate an SSH key, use the `ssh-keygen` command. You can specify the type of key and an optional email address for easy identification:
ssh-keygen -t rsa -b 4096 -C "[email protected]"
Note : Replace `[email protected]` with your actual email address.
2. You'll be prompted to choose a location to save the key. The default location is usually fine. Press `Enter` to accept the default.
3. You can also set a passphrase to add an extra layer of security. If you choose to set a passphrase, make sure to remember it. If you don't want a passphrase, press `Enter` twice.
Your SSH key is now generated.
1. To ensure your SSH key has been generated successfully, navigate to your SSH directory:
cd ~/.ssh
This is where your SSH keys are stored.
2. List the contents of this directory to see your SSH keys:
# Linus User
ls
#Windows User
dir
You should see two files: `id_rsa` (your private key) and `id_rsa.pub` (your public key).
If you've set a passphrase for your SSH key in Step 2, you can add your key to the SSH-Agent for automatic authentication:
1. Start the SSH-Agent:
eval "$(ssh-agent -s)"
2. Add your SSH key:
ssh-add ~/.ssh/id_rsa
You'll need to enter your passphrase when prompted.
Now that you have your SSH key, you can add it to your GitLab account:
1. Log in to your GitLab account.
2. Click on your profile picture in the top-right corner and select "Settings."
3. In the left sidebar, click on "SSH Keys."
4. In the "Key" field, paste the content of your public key (`~/.ssh/id_rsa.pub`).
5. Optionally, give your SSH key a title to help you identify it later.
6. Click the "Add key" button to save your SSH key to your GitLab account.
To make sure everything is set up correctly, you can test your SSH key by running the following command:
ssh -T [email protected]
You should see a message confirming that you've successfully authenticated.
Congratulations! You've generated an SSH key and attached it to your GitLab account. This will allow you to securely access your GitLab repositories without the need for a username and password, enhancing the security of your version control activities.