To clone, pull or push GitHub repositories from a server without typing a token, you generate an SSH key on the server and give its public half to GitHub. This guide shows how to generate an SSH key for GitHub on a VPS, test it, and fix the usual "Permission denied (publickey)" error.
Quick answer: On the VPS, run ssh-keygen -t ed25519, add the contents of ~/.ssh/id_ed25519.pub to GitHub, then run ssh -T git@github.com to confirm GitHub accepts the key.
- SSH into the VPS as the user that will run Git.
- Run
ssh-keygen -t ed25519 -C "your_email@example.com"and accept the default file name. - Print the public key with
cat ~/.ssh/id_ed25519.puband copy the whole line. - Paste it in GitHub under Settings > SSH and GPG keys, or under a repository's Deploy keys.
- Run
ssh -T git@github.comand look for "You've successfully authenticated".
In this guide
- Before You Start
- Step 1: Check Whether the VPS Already Has a Key
- Step 2: Generate an Ed25519 SSH Key on the VPS
- Step 3: Load the Key into ssh-agent (Only If It Has a Passphrase)
- Step 4: Copy the Public Key
- Step 5: Add the Key to GitHub (Account Key or Deploy Key)
- Step 6: Test the Connection to GitHub
- Step 7: Clone the Repository or Switch It to SSH
- Step 8: Set Your Git Name and Email
- Several Repositories on One Server
- Common Errors and Fixes
- FAQ
- Next Steps

Before You Start
- SSH access to the VPS as the Linux user that will run Git. Use your normal deploy user, not root, unless root really runs your deploys.
- A GitHub account with access to the repository. If the server only needs one repository, you can use a deploy key instead of an account key (explained in Step 5).
- About 10 minutes. Nothing here touches your website, but overwriting an existing key in
~/.sshcan break other connections, so check what is there first.
Step 1: Check Whether the VPS Already Has a Key
Connect to the VPS from your local terminal with ssh your-user@your-server-ip. Then list the files in the server's .ssh folder:
ls -al ~/.sshIf you see id_ed25519 and id_ed25519.pub, a key already exists. You can reuse it, or give the new key another file name in Step 2 so you do not overwrite it. "No such file or directory" just means there are no keys yet.
Step 2: Generate an Ed25519 SSH Key on the VPS
Still on the server, run ssh-keygen. Ed25519 is the key type GitHub recommends. The -C flag adds a label, usually your email, so you can recognise the key later.
ssh-keygen -t ed25519 -C "your_email@example.com"Press Enter to accept the default file (~/.ssh/id_ed25519). Then type a passphrase, or press Enter twice for none. When it finishes, ssh-keygen prints the key's fingerprint and a small "randomart" picture.
If the server runs an old OpenSSH that rejects ed25519 as an unknown key type, create an RSA key instead:
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"The rest of this guide uses id_ed25519. Replace it with id_rsa if you made an RSA key.
Passphrase or No Passphrase?
A passphrase encrypts the private key, so a stolen copy is useless on its own. The catch on a server is that something has to type that passphrase. For a key you use by hand, set a passphrase and load it into ssh-agent (Step 3). For a key that cron jobs or deploy scripts use while you are not there, a common choice is no passphrase plus a read-only deploy key for a single repository, which limits the damage if the server is breached.
Step 3: Load the Key into ssh-agent (Only If It Has a Passphrase)
ssh-agent is a small background program that holds decrypted keys in memory. You type the passphrase once per login session instead of on every git pull. Run these on the server:
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519The first command prints the agent's process ID (Agent pid …). The second asks for your passphrase and then prints Identity added. The agent stops when you log out, so repeat this in each new session.
Step 4: Copy the Public Key
Print the public key on the server. It is the file ending in .pub. Never paste the private key (the file without .pub) anywhere.
cat ~/.ssh/id_ed25519.pubThe output is a single line that starts with ssh-ed25519 and ends with your label. Select the whole line in your terminal and copy it.
Step 5: Add the Key to GitHub (Account Key or Deploy Key)
Decide where the key goes first. An account key works for every repository your GitHub account can reach. A deploy key works for one repository only and is read-only unless you allow writes.
Option A: Add an Account Key
- In GitHub, click your profile picture (top right) and choose Settings.
- Select SSH and GPG keys in the sidebar, then click New SSH key.
- Give the key a title such as "VPS production", leave the key type as Authentication Key, and paste the public key into the Key field.
- Click Add SSH key. GitHub may ask you to confirm your password or two-factor code.
Option B: Add a Deploy Key for One Repository
- Open the repository on GitHub and go to Settings > Deploy keys.
- Click Add deploy key, enter a title and paste the public key.
- Tick Allow write access only if the server needs to push. Leave it unticked for pull-only deploys.
- Click Add key.
Deploy keys are the safer choice for servers. If the VPS is compromised, the attacker gets one repository, read-only by default, instead of everything your account can touch. The trade-off is that GitHub attaches each key to only one repository; see "Several Repositories on One Server" below.
Step 6: Test the Connection to GitHub
Back on the VPS, run:
ssh -T git@github.comThe first time, SSH asks whether you trust github.com and shows its key fingerprint. Compare it with the fingerprints GitHub publishes, then type yes.
If the key works, you should see Hi your-username! You've successfully authenticated, but GitHub does not provide shell access. With a deploy key, the greeting names the repository instead. "Permission denied (publickey)" means GitHub did not accept the key; see Common Errors below.
Step 7: Clone the Repository or Switch It to SSH
Git only uses the key when the repository's remote is the SSH form (git@github.com:owner/repo.git), not an https:// URL. To clone a repository on the server over SSH:
git clone git@github.com:your-username/your-repo.gitFor a repository you already cloned over HTTPS, run this inside its folder to change the remote, then check it:
git remote set-url origin git@github.com:your-username/your-repo.git
git remote -vgit remote -v should now list git@github.com:… for both fetch and push. From now on, git pull and git push stop asking for a username and password.
Step 8: Set Your Git Name and Email
If this server will create commits, not just pull them, tell Git who you are. Run this once on the server:
git config --global user.name "Your Name"
git config --global user.email "your_email@example.com"git config --global --list shows the saved values. Pull-only deploy servers can skip this step.
Several Repositories on One Server
A deploy key can belong to only one repository, so a server that pulls several repositories needs one key per repository. Host aliases in ~/.ssh/config on the server tell SSH which key to use for which repository:
Host github-app
HostName github.com
User git
IdentityFile ~/.ssh/app_deploy_key
IdentitiesOnly yes
Host github-api
HostName github.com
User git
IdentityFile ~/.ssh/api_deploy_key
IdentitiesOnly yes- Create each key with its own file name, for example
ssh-keygen -t ed25519 -f ~/.ssh/app_deploy_key. The-fflag sets the file name. - Add each
.pubfile as a deploy key on its own repository. - Clone with the alias in place of
github.com, for examplegit clone git@github-app:your-username/app.git.
Common Errors and Fixes
"Permission denied (publickey)"
GitHub did not receive a key it recognises. Check these in order:
- The public key is really saved in GitHub, under SSH and GPG keys or under the repository's Deploy keys.
- You run Git as the Linux user who owns the key.
sudo git pullruns as root and looks in/root/.ssh, not in your home folder. - SSH offers the right file. Run
ssh -vT git@github.comand look for lines that name your key file. If it is missing, add anIdentityFileline for it in~/.ssh/config.
"Key is already in use"
GitHub refuses a public key that is already attached to another account or repository. Generate a new key for the second use instead of reusing the first one.
"UNPROTECTED PRIVATE KEY FILE"
SSH ignores a private key that other users can read. Tighten the permissions on the server:
chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_ed25519Connection Times Out on Port 22
Some networks block outgoing connections on port 22. GitHub also accepts SSH on port 443 at ssh.github.com. Test it with ssh -T -p 443 git@ssh.github.com. If that works, add this to ~/.ssh/config on the server so Git uses port 443 automatically:
Host github.com
HostName ssh.github.com
Port 443
User gitFAQ
Can I copy my laptop's SSH key to the VPS instead?
You can, but you should not. If the server is ever breached, the key you use everywhere else is exposed with it. Generating a separate key on each machine lets you revoke one in GitHub without touching the others.
Should I use Ed25519 or RSA for GitHub?
Use Ed25519 unless something stops you. The keys are short, fast and supported by GitHub and current OpenSSH versions. Fall back to a 4096-bit RSA key only when an older system cannot create or read Ed25519 keys.
How do I revoke a key if the server is compromised?
Delete the key in GitHub straight away. Account keys are under Settings > SSH and GPG keys; deploy keys are under the repository's Settings > Deploy keys. Once the key is deleted in GitHub, a stolen copy of the private key file no longer gets in.
Does an SSH key replace a GitHub personal access token?
Only for Git over SSH. Clone, pull and push through a git@github.com: remote use the key. You still need a token for HTTPS remotes and for scripts that call the GitHub API.
Next Steps
Your VPS can now pull code from GitHub without a password. Next, put that code to work: deploy a Node.js app on a Hostinger VPS behind Nginx, or, if the app runs in containers, deploy a Docker Compose app with DNS and SSL. To keep a Node.js process running after you log out, set up PM2 with a startup script and log rotation.


You must be logged in to post a comment.