- π SSH keys consist of a public key and a private key
- π Keys are typically stored in
~/.ssh/
- π Private keys should be kept secure and never shared
- π€ Public keys are added to remote servers or services
Command | Description | Example |
---|---|---|
ssh-keygen -t rsa -b 4096 -C "comment" |
π¨ Generate RSA key pair | ssh-keygen -t rsa -b 4096 -C "[email protected]" |
ssh-keygen -t ed25519 -C "comment" |
π¬ Generate Ed25519 key pair (more secure) | ssh-keygen -t ed25519 -C "[email protected]" |
- π‘ Use meaningful names for your key files, e.g.,
id_rsa_work
,id_ed25519_personal
-
π Create separate directories for different contexts:
~/.ssh/work/ ~/.ssh/personal/ ~/.ssh/project1/
-
π·οΈ Use descriptive names for your keys:
~/.ssh/work/id_rsa_github ~/.ssh/personal/id_ed25519_gitlab ~/.ssh/project1/id_rsa_staging
- π Edit or create
~/.ssh/config
- ποΈ Add host-specific configurations:
# Work GitHub
Host github-work
HostName github.com
User git
IdentityFile ~/.ssh/work/id_rsa_github
# Personal GitLab
Host gitlab-personal
HostName gitlab.com
User git
IdentityFile ~/.ssh/personal/id_ed25519_gitlab
# Project1 Staging Server
Host project1-staging
HostName staging.project1.com
User deploy
IdentityFile ~/.ssh/project1/id_rsa_staging
Command | Description | Example |
---|---|---|
ssh user@hostname |
π₯οΈ Connect using the config file | ssh project1-staging |
ssh -i ~/.ssh/keyfile user@hostname |
π Specify key file directly | ssh -i ~/.ssh/work/id_rsa_github [email protected] |
git clone git@github-work:username/repo.git |
π Use with Git (configured host) | git clone git@github-work:company/project.git |
Command | Description |
---|---|
ssh-add -l |
π List added keys |
ssh-add ~/.ssh/keyfile |
β Add a key to the agent |
ssh-add -d ~/.ssh/keyfile |
β Remove a key from the agent |
ssh-add -D |
π§Ή Remove all keys from the agent |
- π Regularly generate new keys (e.g., annually)
- π Update keys on remote servers/services
- ποΈ Safely delete old keys after rotation
- π Use strong passphrases for your keys
- π« Never share private keys
- π Use different keys for different contexts (work, personal, projects)
- π΄ Store backup copies of your keys securely offline
- π Set appropriate permissions:
chmod 600 ~/.ssh/id_rsa
- π Use
ssh -v
for verbose output to debug connection issues - π Check key permissions (should be 600 for private keys)
- π§ͺ Test connections with
ssh -T [email protected]
for GitHub - π Ensure the correct key is being offered with
ssh-add -l
-
π Setting up multiple GitHub accounts:
# In ~/.ssh/config Host github-personal HostName github.com User git IdentityFile ~/.ssh/personal/id_rsa_github_personal Host github-work HostName github.com User git IdentityFile ~/.ssh/work/id_rsa_github_work
Usage:
git clone git@github-personal:username/repo.git
-
π₯οΈ Connecting to different environments:
# In ~/.ssh/config Host prod HostName production.example.com User produser IdentityFile ~/.ssh/work/id_rsa_production Host staging HostName staging.example.com User staginguser IdentityFile ~/.ssh/work/id_rsa_staging
Usage:
ssh prod
orssh staging
-
π Automatically add keys to ssh-agent: Add to your
~/.bashrc
or~/.zshrc
:ssh-add -q ~/.ssh/personal/id_rsa_github_personal ssh-add -q ~/.ssh/work/id_rsa_github_work
-
π Create a key management script:
#!/bin/bash case $1 in "list") ssh-add -l ;; "add") ssh-add ~/.ssh/$2 ;; "remove") ssh-add -d ~/.ssh/$2 ;; "clear") ssh-add -D ;; *) echo "Usage: $0 {list|add|remove|clear}" ;; esac
- π Use meaningful comments when generating keys to easily identify them later
- π Consider using different key types (RSA, Ed25519) for different services
- π§° Familiarize yourself with
ssh-keygen
options for key management - π Use a password manager to securely store key passphrases
- π Keep a secure inventory of your keys and their purposes
- π Set up a key rotation schedule and stick to it
- π§ͺ Regularly test your SSH configurations to ensure they're working correctly
Remember, proper SSH key management is crucial for maintaining security across your various accounts and services. Always prioritize security and follow best practices when handling SSH keys. Happy secure connecting! ππ₯οΈ