DevOps / Containerization

How to Set Up SSH for GitHub on Your Server: A Beginner's Guide

Abhishek Ghimire · 5 min read
3 views

If you've just got a Linux server (like an Oracle Cloud, AWS, or DigitalOcean VM) and want to pull your code from GitHub, you've probably hit this: GitHub no longer accepts your password for Git operations. The cleanest fix is SSH keys. Once set up, your server can talk to GitHub securely without asking for a password every time.

This guide assumes zero prior knowledge. Just follow along step by step.

What is SSH, in simple words?

Think of SSH keys as a lock-and-key pair:

  • Private key stays on your server. Never share it with anyone.

  • Public key is given to GitHub. It's safe to share.

When your server connects, GitHub checks whether the private key matches the public key it has. If it matches, you're in.

Step 1: Log in to your server

From your computer's terminal:

ssh username@your-server-ip

Replace username with your server user (for example ubuntu or opc) and your-server-ip with your server's IP address.

Step 2: Check whether you already have a key

ls -la ~/.ssh

If you see files like id_ed25519 and id_ed25519.pub, you already have a key and can skip to Step 4. If the folder is empty or doesn't exist, continue.

Step 3: Generate a new SSH key

ssh-keygen -t ed25519 -C "your-email@example.com"

Use the email linked to your GitHub account. You'll see a few prompts:

  1. "Enter file in which to save the key": just press Enter to accept the default.

  2. "Enter passphrase": you can press Enter for no passphrase (common for servers that pull code automatically), or set one for extra security.

This creates two files:

  • ~/.ssh/id_ed25519 (private key, keep secret)

  • ~/.ssh/id_ed25519.pub (public key, goes to GitHub)

Tip: ed25519 is the modern, recommended key type. If your system is very old and doesn't support it, use ssh-keygen -t rsa -b 4096 -C "your-email@example.com" instead.

Step 4: Start the SSH agent and add your key

The SSH agent holds your key in memory so you don't have to keep pointing to it.

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

You should see something like Identity added: /home/ubuntu/.ssh/id_ed25519.

Step 5: Copy your public key

cat ~/.ssh/id_ed25519.pub

It prints one long line starting with ssh-ed25519 and ending with your email. Select and copy the entire line.

⚠️ Make sure you copy the file ending in .pub. Never paste your private key anywhere.

Step 6: Add the key to GitHub

  1. Go to GitHub → click your profile picture → Settings.

  2. In the left sidebar, open SSH and GPG keys.

  3. Click New SSH key.

  4. Title: give it a name you'll recognize, such as My Oracle Server.

  5. Key type: Authentication Key.

  6. Key: paste the line you copied.

  7. Click Add SSH key.

Step 7: Test the connection

Back on your server:

ssh -T git@github.com

The first time, you'll see a message asking if you trust GitHub's host. Type yes and press Enter.

If everything worked, you'll see:

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

That "does not provide shell access" part is normal. It means success. 🎉

Step 8: Clone your repository using SSH

On your GitHub repo page, click Code → SSH and copy the link. It looks like this:

git clone git@github.com:your-username/your-repo.git

Notice it starts with git@github.com: rather than https://. That's how you know it's using SSH.

Already cloned with HTTPS? Switch to SSH

If you cloned earlier using an https:// link, you don't need to re-clone. Just change the remote:

cd your-repo
git remote -v
git remote set-url origin git@github.com:your-username/your-repo.git
git remote -v

The second git remote -v should now show the git@github.com: address.

Step 9 (Optional but useful): Set your Git identity

So your commits show the right name and email:

git config --global user.name "Your Name"
git config --global user.email "your-email@example.com"

Troubleshooting common problems

"Permission denied (publickey)"
GitHub didn't accept your key. Check that:

  • You pasted the .pub file content into GitHub, not the private key.

  • The key was added to the agent: run ssh-add -l to list loaded keys.

  • File permissions are correct:

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

Connection times out on port 22
Some cloud providers or networks block port 22 for outgoing connections. GitHub lets you use port 443 instead. Create or edit ~/.ssh/config:

nano ~/.ssh/config

Add this:

Host github.com
    HostName ssh.github.com
    Port 443
    User git
    IdentityFile ~/.ssh/id_ed25519

Save (Ctrl+O, Enter, Ctrl+X), then test again with ssh -T git@github.com.

Key works now but stops after reboot or re-login
The SSH agent doesn't always survive a logout. Adding the IdentityFile line in ~/.ssh/config (shown above) tells SSH which key to use automatically, so you don't need to run ssh-add every time.

Bonus: Deploy keys for a single repository

If your server only needs access to one repository (common for production servers), a deploy key is safer than adding the key to your whole account.

  1. Open the repository on GitHub → Settings → Deploy keys → Add deploy key.

  2. Paste the same public key.

  3. Leave Allow write access unchecked if the server only needs to pull code.

Now that server can access only that one repo, and nothing else on your account.

Note: GitHub won't let you use the same public key as both an account key and a deploy key. If you go the deploy-key route, either remove it from your account's SSH keys first or generate a separate key for it.

Quick recap

Step Command / Action Generate key ssh-keygen -t ed25519 -C "email" Start agent & add key eval "$(ssh-agent -s)" then ssh-add ~/.ssh/id_ed25519 Show public key cat ~/.ssh/id_ed25519.pub Add to GitHub Settings → SSH and GPG keys → New SSH key Test ssh -T git@github.com Clone git clone git@github.com:user/repo.git

That's it! Your server is now connected to GitHub over SSH. From here you can git pull to update your code on the server without typing passwords, which is the first step toward automated deployments.

Written by

Abhishek Ghimire

Writes here about engineering, technology, and the things worth building.