DEV Community

Mohammed Chami
Mohammed Chami

Posted on

How to Authenticate GitHub Using SSH and Push Code Securely

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
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

For RSA (if Ed25519 isn't supported):

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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)"
Enter fullscreen mode Exit fullscreen mode

Then add your SSH private key to the agent:

For Ed25519:

ssh-add ~/.ssh/id_ed25519
Enter fullscreen mode Exit fullscreen mode

For RSA:

ssh-add ~/.ssh/id_rsa
Enter fullscreen mode Exit fullscreen mode

πŸ”‘ Step 4: Add the Public Key to GitHub

Copy your public key to the clipboard:

For Ed25519:

cat ~/.ssh/id_ed25519.pub
Enter fullscreen mode Exit fullscreen mode

For RSA:

cat ~/.ssh/id_rsa.pub
Enter fullscreen mode Exit fullscreen mode

Add to GitHub:

  1. Go to GitHub.com β†’ Click your profile picture β†’ Settings
  2. In the sidebar, click SSH and GPG keys
  3. Click New SSH key
  4. In the "Title" field, add a descriptive label (e.g., "My Linux Dev Machine")
  5. Select Authentication Key as the key type
  6. Paste your public key into the "Key" field
  7. Click Add SSH key
  8. 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
Enter fullscreen mode Exit fullscreen mode

Write Yes

Expected successful output:

Hi your_username! You've successfully authenticated, but GitHub does not provide shell access.
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

Change it to SSH:

git remote set-url origin git@github.com:your_username/your_repo.git
Enter fullscreen mode Exit fullscreen mode

Verify the change:

git remote -v
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

πŸš€ 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
Enter fullscreen mode Exit fullscreen mode

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)