How to Set Up and SSH key (for GitHub, GitLab & Bitbucket) and Why You Need It?
When working with platforms like GitHub, GitLab, or Bitbucket, SSH keys allow you to authenticate securely without using your username and password every time. This guide covers what SSH keys are, how to generate them, and how to add them to popular Git hosting services.
π What Is an SSH Key?
SSH (Secure Shell) keys are a pair of cryptographic keys used to authenticate your computer with a remote server. They come in two parts:
- Public Key: Shared with the server (e.g., GitHub, GitLab, Bitbucket).
- Private Key: Stays on your computer and should be kept secure.
βοΈ How to Generate an SSH Key
Open your terminal (Linux/macOS) or Git Bash (Windows), and run:
ssh-keygen -t ed25519 -C "your_email@example.com"
If your system doesnβt support ed25519
, use:
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
- Press
Enter
to accept the default file location:~/.ssh/id_ed25519
- Optionally, add a passphrase for extra security.
π Where Are SSH Keys Stored?
By default:
- Public Key:
~/.ssh/id_ed25519.pub
- Private Key:
~/.ssh/id_ed25519
β οΈ Never share your private key with anyone.
π How to Copy Your SSH Public Key
Run the following command:
cat ~/.ssh/id_ed25519.pub
Or use:
pbcopy < ~/.ssh/id_ed25519.pub # macOS
xclip -sel clip < ~/.ssh/id_ed25519.pub # Linux
clip < ~/.ssh/id_ed25519.pub # Windows (Git Bash)
π Add SSH Key to GitHub
- Go to GitHub.com
- Click your profile > Settings
- Navigate to SSH and GPG keys
- Click New SSH key
- Paste your key and click Add SSH key
π¦ Add SSH Key to GitLab
- Go to GitLab.com
- Click your avatar > Edit profile
- Go to SSH Keys
- Paste your key and click Add key
π§Ί Add SSH Key to Bitbucket
- Go to Bitbucket.org
- Click your avatar > Personal settings
- Choose SSH keys
- Click Add key, paste your key, and save
β Test Your SSH Connection
Use this command to test:
ssh -T git@github.com # for GitHub
ssh -T git@gitlab.com # for GitLab
ssh -T git@bitbucket.org # for Bitbucket
If successful, you'll see a message like:
Hi username! You've successfully authenticated.
π Final Tips
- Use one SSH key per device.
- Back up your private key in a secure location.
- Rotate your keys periodically for added security.
Feel free to bookmark this guide and share it with your team or friends starting with Git platforms.