How to Configure SSH for GitHub Authentication on Linux
Secure Shell (SSH) authentication is the recommended way to securely connect to GitHub repositories without needing to enter your credentials every time. In this guide, you'll learn how to configure SSH authentication for GitHub on Linux step by step.
Step 1: Check for Existing SSH Keys
Before generating a new SSH key, check if you already have one:
ls -al ~/.ssh
If you see files like id_rsa
and id_rsa.pub
, you already have an SSH key. You can use the existing key or generate a new one.
Step 2: Generate a New SSH Key
If you don’t have an SSH key or want a new one, generate it using:
ssh-keygen -o -a 100 -t ed25519 -f ~/.ssh/id_ed25519_github -C "ssh key for github"
If your system doesn’t support ed25519
, use:
ssh-keygen -o -a 100 -t rsa -b 4096 -m PEM -f ~/.ssh/id_rsa_github -C "ssh key for github"
After running the command, you will be prompted to enter a strong passphrase. Type your passphrase, press Enter, then confirm it when prompted to enhance security.
Step 3: Add Your SSH Key to the SSH Agent
Start the SSH agent:
eval "$(ssh-agent -s)"
Then add your key:
ssh-add ~/.ssh/id_ed25519_github
If you used RSA, replace id_ed25519_github
with id_rsa_github
.
Step 4: Persist SSH Key Across Sessions
To ensure your SSH key is always loaded when you open a new terminal session, there are two main methods. The recommended method is to configure your SSH settings, while the alternative is to modify your .bashrc
file.
Recommended: Configure SSH to Automatically Add the Key
Edit (or create) your SSH config file:
nano ~/.ssh/config
Add the following lines:
Host github.com
IdentityFile ~/.ssh/id_ed25519_github
AddKeysToAgent yes
This ensures that your SSH key is automatically loaded by the SSH agent when needed.
Alternative: Load the SSH Key in .bashrc
If you want to explicitly load the SSH key every time a new terminal session starts, add the following command to your ~/.bashrc
(or ~/.bash_profile
on macOS):
echo 'eval "$(ssh-agent -s)" && ssh-add ~/.ssh/id_ed25519_github' >> ~/.bashrc
Then reload your shell configuration:
source ~/.bashrc
This ensures the SSH agent starts automatically and the key is added every time you open a new terminal session.
Step 5: Add the SSH Key to Your GitHub Account
cat ~/.ssh/id_ed25519_github.pub
If you used RSA, replace id_ed25519_github.pub
with id_rsa_github.pub
.
Now, go to GitHub > Settings > SSH and GPG Keys and click New SSH Key. Paste the copied key and save it.
Step 6: Test the SSH Connection
Verify that your SSH key is correctly added by running:
ssh -T git@github.com
If prompted with:
This key is not known by any other names.
Are you sure you want to continue connecting (yes/no/[fingerprint])?
Type yes to proceed.
You should see a message like:
Hi username! You've successfully authenticated, but GitHub does not provide shell access.
Step 7: Configure Git to Use SSH (Optional)
If you're still using HTTPS for Git operations, switch to SSH:
git remote set-url origin git@github.com:username/repository.git
Conclusion
You've successfully configured SSH authentication for GitHub on Linux! This setup allows you to securely connect to your repositories without entering your password repeatedly. Happy coding!
If you encounter any issues, check GitHub’s SSH troubleshooting guide.
Top comments (0)