How to Access GitHub from a Linux Server Using SSH Keys (Step-by-Step Guide)

Stephen NdegwaStephen Ndegwa
·
3 min read

How to Access GitHub from a Linux Server Using SSH Keys (Step-by-Step Guide)

Accessing GitHub securely from a Linux server is a common requirement for developers, DevOps engineers, and system administrators. The recommended and most secure method is using SSH key authentication instead of usernames and passwords.

This guide explains what SSH keys are, why they’re needed, and exactly how to set them up and use them on a Linux server.


Why Use SSH Keys for GitHub?

SSH keys provide:

  • 🔐 Strong security (cryptographic authentication)
  • 🚫 No need to type passwords for every Git operation
  • 🤖 Ideal for servers, CI/CD pipelines, and automation
  • 🔑 Fine-grained access control (you can revoke keys anytime)

GitHub no longer supports password authentication for Git operations, making SSH (or tokens) mandatory.


Prerequisites

Before starting, ensure:

  • You have SSH access to your Linux server
  • Git is installed:git --version If not:sudo apt install git -y # Debian/Ubuntu sudo yum install git -y # RHEL/CentOS

Step 1: Check for Existing SSH Keys

First, see if your server already has SSH keys:

ls -al ~/.ssh

Look for files such as:

  • id_ed25519
  • id_ed25519.pub
  • id_rsa
  • id_rsa.pub

If none exist (or you want a new one), proceed to the next step.


Step 2: Generate a New SSH Key

Recommended (Modern & Secure)

ssh-keygen -t ed25519 -C "[email protected]"

Fallback (Older Systems)

ssh-keygen -t rsa -b 4096 -C "[email protected]"

When prompted:

  • File location → Press Enter (default path is best)
  • Passphrase → Optional but strongly recommended for security

This creates:

  • Private key: ~/.ssh/id_ed25519
  • Public key: ~/.ssh/id_ed25519.pub

⚠️ Never share your private key.


Step 3: Start the SSH Agent and Add the Key

The SSH agent manages your keys in memory.

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

Verify the key is loaded:

ssh-add -l

Step 4: Copy the Public Key

Display the public key:

cat ~/.ssh/id_ed25519.pub

Copy the entire output (it starts with ssh-ed25519).


Step 5: Add the SSH Key to GitHub

  1. Log in to GitHub
  2. Go to Settings → SSH and GPG keys
  3. Click New SSH key
  4. Fill in:
    • Title: e.g. Linux Server – Production
    • Key type: Authentication Key
    • Key: Paste the public key
  5. Click Add SSH key

Step 6: Test the SSH Connection

From the Linux server:

ssh -T [email protected]

Expected output:

Hi <your-username>! You've successfully authenticated, but GitHub does not provide shell access.

This confirms your server can securely communicate with GitHub.


Step 7: Use GitHub via SSH

Clone a Repository

git clone [email protected]:username/repository.git

Switch an Existing Repo from HTTPS to SSH

git remote set-url origin [email protected]:username/repository.git

Verify:

git remote -v

Best Practices for Servers

1. Use One SSH Key per Server

This makes it easy to revoke access if a server is compromised.

2. Use Deploy Keys for Production

For read-only or single-repo access:

  • Repository → Settings → Deploy keys
  • Add the public key
  • Optionally allow write access

3. Secure SSH Permissions

chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_ed25519
chmod 644 ~/.ssh/id_ed25519.pub

Common Troubleshooting

❌ Permission denied (publickey)

  • Ensure the public key is added to GitHub
  • Check:ssh-add -l

❌ Using HTTPS Instead of SSH

Check remote:

git remote -v

If it shows https://github.com/..., switch to SSH.

❌ Multiple GitHub Accounts

Create an SSH config file:

nano ~/.ssh/config

Example:

Host github-work
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_ed25519_work

Then clone with:

git clone git@github-work:username/repo.git

Summary

By using SSH keys:

  • Your Linux server authenticates securely with GitHub
  • You avoid passwords and tokens
  • You enable safe automation and deployments

Core steps recap:

  1. Generate SSH key
  2. Add key to SSH agent
  3. Add public key to GitHub
  4. Test connection
  5. Use SSH URLs for Git operations
Share:

Related Guides

Automating JNLP Downloads with PowerShell Using Session Cookies

When managing remote servers or BMC interfaces, some resources such as JNLP (Java Network Launch Protocol) files require authentication via cookies and session handling. Manually downloading these files can be cumbersome. PowerShell provides a way to automate this process using web sessions and cookie management. Creating a Persistent Web Session A web session in PowerShell [&hellip;]

Stephen Ndegwa
·

Complete Guide to Downloading Files with PowerShell

Introduction PowerShell provides powerful tools for downloading files from web servers, with Invoke-WebRequest being the primary cmdlet for making HTTP requests. This guide covers everything from basic downloads to advanced scenarios involving authentication, cookies, and custom headers. Basic File Downloads Simple Download The most straightforward way to download a file: Download with Progress Bar PowerShell [&hellip;]

Stephen Ndegwa
·

The Complete Guide to Installing StorCLI on Linux and Windows

StorCLI (Storage Command Line Tool) is Broadcom&#8217;s powerful command-line utility for managing LSI MegaRAID and PRAID controllers. Whether you&#8217;re managing hardware RAID arrays on servers or workstations, StorCLI provides comprehensive control over your storage infrastructure. This guide will walk you through the complete installation process on both Linux and Windows systems. What is StorCLI? StorCLI [&hellip;]

Stephen Ndegwa
·