How to Access GitHub from a Linux Server Using SSH Keys (Step-by-Step Guide)
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 --versionIf 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_ed25519id_ed25519.pubid_rsaid_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
- Log in to GitHub
- Go to Settings β SSH and GPG keys
- Click New SSH key
- Fill in:
- Title: e.g.
Linux Server β Production - Key type: Authentication Key
- Key: Paste the public key
- Title: e.g.
- 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:
- Generate SSH key
- Add key to SSH agent
- Add public key to GitHub
- Test connection
- Use SSH URLs for Git operations
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 […]
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 […]
The Complete Guide to Installing StorCLI on Linux and Windows
StorCLI (Storage Command Line Tool) is Broadcom’s powerful command-line utility for managing LSI MegaRAID and PRAID controllers. Whether you’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 […]