When working with GitHub, using HTTPS for pushing changes often asks for your username and password or personal access token every time. A better and more secure way is to use SSH authentication. This guide will show you how to generate an SSH key, add it to your GitHub account, and change your Git remote URL from HTTPS to SSH.
β Step 1: Check for Existing SSH Keys
Before generating a new SSH key, check if one already exists:
ls -al ~/.ssh
If you see files like id_rsa
and id_rsa.pub
or id_ed25519
and id_ed25519.pub
, you already have SSH keys. You can skip to Step 3 if you want to use an existing key.
π οΈ Step 2: Generate a New SSH Key
If no key exists or you want to generate a new one, use the Ed25519 algorithm (recommended) or RSA as a fallback:
For Ed25519 (recommended):
ssh-keygen -t ed25519 -C "your_email@example.com"
For RSA (if Ed25519 isn't supported):
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
If ssh-keygen
is not found, install OpenSSH:
# For Ubuntu/Debian
sudo apt update && sudo apt install openssh-client
# For Arch-based distros (e.g., EndeavourOS)
sudo pacman -S openssh
# For CentOS/RHEL/Fedora
sudo dnf install openssh-clients
# or for older versions: sudo yum install openssh-clients
When prompted:
- Press
Enter
to accept the default location (~/.ssh/id_ed25519
or~/.ssh/id_rsa
) - Optionally enter a secure passphrase for extra protection
π Step 3: Add the SSH Key to the SSH Agent
Start the ssh-agent in the background:
eval "$(ssh-agent -s)"
Then add your SSH private key to the agent:
For Ed25519:
ssh-add ~/.ssh/id_ed25519
For RSA:
ssh-add ~/.ssh/id_rsa
π Step 4: Add the Public Key to GitHub
Copy your public key to the clipboard:
For Ed25519:
cat ~/.ssh/id_ed25519.pub
For RSA:
cat ~/.ssh/id_rsa.pub
Add to GitHub:
- Go to GitHub.com β Click your profile picture β Settings
- In the sidebar, click SSH and GPG keys
- Click New SSH key
- In the "Title" field, add a descriptive label (e.g., "My Linux Dev Machine")
- Select Authentication Key as the key type
- Paste your public key into the "Key" field
- Click Add SSH key
- If prompted, confirm access to your GitHub account
π§ͺ Step 5: Test Your SSH Connection
Verify that your SSH key is working correctly:
ssh -T git@github.com
Write Yes
Expected successful output:
Hi your_username! You've successfully authenticated, but GitHub does not provide shell access.
Troubleshooting common issues:
- If you get a "Permission denied" error, double-check that your key was added correctly to GitHub
- If you get a "Host key verification failed" error, run:
ssh-keyscan github.com >> ~/.ssh/known_hosts
π Step 6: Change Git Remote from HTTPS to SSH
Check your current remote URL:
git remote -v
If it shows HTTPS format:
origin https://github.com/your_username/your_repo.git (fetch)
origin https://github.com/your_username/your_repo.git (push)
Change it to SSH:
git remote set-url origin git@github.com:your_username/your_repo.git
Verify the change:
git remote -v
Should now show SSH format:
origin git@github.com:your_username/your_repo.git (fetch)
origin git@github.com:your_username/your_repo.git (push)
For multiple remotes:
If you have multiple remotes, you can change them individually:
git remote set-url upstream git@github.com:original_owner/original_repo.git
π Step 7: Push Your Project
Now you can push your project securely over SSH:
git add .
git commit -m "Initial commit"
git push -u origin main
You won't be asked for a username or password again (unless you set a passphrase for your SSH key).
π Security Best Practices
- Use a strong passphrase for your SSH key
- Keep your private key secure - never share it or commit it to a repository
- Regularly rotate your SSH keys (recommended every 1-2 years)
- Use different SSH keys for different services or environments
- Enable two-factor authentication on your GitHub account for additional security
Good Luck
Top comments (0)