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

  1. Go to GitHub.com
  2. Click your profile > Settings
  3. Navigate to SSH and GPG keys
  4. Click New SSH key
  5. Paste your key and click Add SSH key

🦊 Add SSH Key to GitLab

  1. Go to GitLab.com
  2. Click your avatar > Edit profile
  3. Go to SSH Keys
  4. Paste your key and click Add key

🧺 Add SSH Key to Bitbucket

  1. Go to Bitbucket.org
  2. Click your avatar > Personal settings
  3. Choose SSH keys
  4. 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.


More Articles

STAY IN TOUCH

Get notified when I publish something new, and unsubscribe at any time.