Let’s walk through setting up SSH key authentication between your server and GitHub. SSH authentication is one of GitHub’s recommended secure methods that allows you to work with repositories without entering credentials for each operation.
Unlike the deprecated password authentication over HTTPS, SSH keys remain fully supported and provide a secure, convenient way to access private repositories.
Make Sure Git is Installed
First, let’s check if Git is already on your server. If not, we’ll install it:
sudo apt update
sudo apt install git -y
Let’s confirm everything installed correctly:
git --version
Great! You should see the Git version displayed. Now let’s move on.
Generate an SSH Key
SSH keys will be your server’s “digital ID card” for GitHub.
Check for existing SSH keys
First, let’s see if you already have SSH keys set up:
ls -al ~/.ssh
If you see files like id_ed25519
and id_ed25519.pub
, you already have keys! Feel free to use them or create new ones.
Generate a new SSH key
If you need a new key, let’s create one using the modern Ed25519 algorithm.
ED25519 is a modern, highly secure, and efficient public-key algorithm used in SSH for authentication. It is an alternative to traditional RSA and ECDSA keys.
ssh-keygen -t ed25519 -C "your-email@example.com"
Simply press Enter to accept the default file location (~/.ssh/id_ed25519
)
When asked for a passphrase, you can add one for extra security or press Enter twice for no passphrase (convenient for servers, but slightly less secure)
Let’s make sure your SSH directory has the right permissions:
chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_ed25519
chmod 644 ~/.ssh/id_ed25519.pub
Start the SSH agent and add your key
Let’s get your SSH agent running with your new key:
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
Add Your SSH Key to GitHub
Now let’s tell GitHub about your server’s new key.
Display and copy your public key
cat ~/.ssh/id_ed25519.pub
Copy the entire output (it starts with ssh-ed25519
and ends with your email).
Add the key to your GitHub account
- Head over to GitHub’s SSH Keys Settings at: https://github.com/settings/keys
- Click the green “New SSH Key” button
- Give it a descriptive title like “Production Server”
- Paste your copied key into the “Key” field
- Click “Add SSH Key”
Test Your Connection
Let’s make sure everything’s working correctly:
ssh -T git@github.com
Success looks like this:
Hi username! You've successfully authenticated, but GitHub does not provide shell access.
Clone Your Private Repository
Now for the moment you’ve been waiting for – cloning your private repo!
Navigate to where you want your repository:
cd /var/www/your_project_directory
Note: If you’re cloning to a system directory like /var/www/
, you might need sudo permissions depending on your server setup.
Clone your repository:
git clone git@github.com:your-username/your-private-repo.git
That’s it! Your repository should clone without asking for a password. 🎉