How to Access GitHub from a Linux Server Using SSH Keys (Step-by-Step Guide)
Set up SSH key authentication for GitHub on a Linux server with ssh-keygen (ed25519), the ssh-agent, and an ssh -T test, plus deploy-key best practices.

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 then Press Enter (default path is best)
- Passphrase then 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 then 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 then Settings then 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