Securing Your SSH Key, How to Generate SSH Keys

3 minute read

Published:

๐Ÿ” Securing Your SSH Key

SSH keys are a powerful way to authenticate securely with services like GitHub, servers, and remote systems. If youโ€™ve generated an SSH key without a passphrase, this guide shows you how to add one afterward โ€” no need to regenerate the key!


๐Ÿงพ What Is SSH and Why Do Developers Use It?

SSH (Secure Shell) is a cryptographic protocol that lets you securely log into remote machines and services. Itโ€™s widely used for:

  • Connecting to remote servers (e.g., via ssh user@hostname)
  • Running remote commands and file transfers (e.g., scp, rsync)
  • Authenticating with Git hosting services (like GitHub or GitLab) without entering your username and password every time

In Git, SSH keys allow you to push and pull from repositories securely and seamlessly. Instead of using HTTPS and typing credentials, Git can authenticate using your private key.


๐Ÿ› ๏ธ How to Generate SSH Keys with ssh-keygen

If you donโ€™t have an SSH key yet, or want to create a new one, you can use the ssh-keygen command:

ssh-keygen -t ed25519 -C "your_email@example.com"

Explanation of Options:

  • -t (type): Specifies the type of key to create.
    Common types:
    • ed25519 โ€” modern, secure, and faster
    • rsa โ€” older, widely supported, typically 2048 or 4096 bits
  • -C (comment): Adds a label/comment to your public key to help identify it later (usually your email).

What Happens When You Run This?

  • Youโ€™ll be prompted for a file location (default is ~/.ssh/id_ed25519 or ~/.ssh/id_rsa).
  • Youโ€™ll be asked to enter a passphrase (recommended for security) or leave it empty for no passphrase.

๐Ÿค” Why Add a Passphrase?

Your SSH private key is like a master key to your digital identity. If someone gets access to it, they can impersonate you on any system or service that trusts your key.

A passphrase encrypts your private key file. This means:

  • Even if your key is stolen, itโ€™s useless without the passphrase
  • You gain an extra layer of security with minimal inconvenience (especially when used with ssh-agent)

๐Ÿ› ๏ธ How to Check If Your Key Has a Passphrase

Try adding your key with:

ssh-add ~/.ssh/id_ed25519
  • If youโ€™re prompted for a passphrase, the key is already encrypted โœ…
  • If it adds silently, the key is unencrypted โŒ

๐Ÿ”„ Add a Passphrase to an Existing SSH Key

To encrypt your existing key with a passphrase without regenerating a key, run:

ssh-keygen -p -f ~/.ssh/id_ed25519

Explanation:

  • -p โ€” Change (or set) the passphrase
  • -f โ€” Path to the private key file

Youโ€™ll see:

Enter old passphrase:         โ† Just press Enter if none exists
Enter new passphrase (empty for no passphrase): ********
Enter same passphrase again:  ********

Done! Your key is now encrypted.


โœ… Confirm It Worked

Try again:

ssh-add ~/.ssh/id_ed25519

You should now see:

Enter passphrase for /home/you/.ssh/id_ed25519:

Perfect โ€” itโ€™s working!


๐Ÿง  Bonus: Use ssh-agent for Convenience

Typing your passphrase every time can be tedious. Start the SSH agent and load your key once per session:

eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519

Now your key stays unlocked in memory for the rest of your session.


๐Ÿ”š Final Thoughts

๐Ÿ” A passphrase is your last line of defense if your private key falls into the wrong hands.
๐Ÿ”ง Itโ€™s never too late to secure it โ€” and now you know how!

If youโ€™re using Git or logging into servers regularly, this is a simple step that adds significant protection to your workflow.

Happy coding! ๐Ÿš€