Create the Key
Log in as the user that will run Git, for example deploy, not root. Then:
ssh-keygen -t ed25519 -C "deploy key for example.com" -f ~/.ssh/github_app
This creates two files: ~/.ssh/github_app, the private key that never leaves the server, and ~/.ssh/github_app.pub, the public key you give to GitHub. The -C text is a label that helps you recognize the key later.
ssh-keygen asks for a passphrase. A passphrase protects the key if the file is copied, but an automated deploy cannot type it. For a deploy key that only reads one repository, many administrators leave it empty; for a key with write access to your account, use one.
Copy the Public Key
cat ~/.ssh/github_app.pub
The output is one line starting with ssh-ed25519. Copy the whole line. Never paste the private key file anywhere.
Add It to GitHub
You have two choices:
| Key type | Where to add it | Access |
|---|---|---|
| Account key | Account settings, SSH and GPG keys | Every repository you can access |
| Deploy key | Repository settings, Deploy keys | One repository, read-only by default |
For a deploy key, open the repository, go to Settings, then Deploy keys, and add the public key. Leave the Allow write access box unchecked unless the server must push. GitHub accepts a deploy key on one repository only, so each repository needs its own key.
Tell SSH Which Key to Use
Create or edit ~/.ssh/config:
Host github.com
User git
IdentityFile ~/.ssh/github_app
IdentitiesOnly yes
User git is required: GitHub accepts SSH connections only for the user git, whatever your own account is called. Set safe permissions:
chmod 700 ~/.ssh
chmod 600 ~/.ssh/config ~/.ssh/github_app
Test the Connection
ssh -T github.com
On the first connection SSH shows the server's key fingerprint and asks whether to continue. Compare it with the fingerprints GitHub publishes in its documentation before you type yes. A working key returns a greeting with your user name, or the repository name for a deploy key, and a note that shell access is not provided. The command exits with status 1 even when it works, because no shell is opened.
Several Repositories on One Server
Because each deploy key fits one repository, give each key its own host alias:
Host github-shop
HostName github.com
User git
IdentityFile ~/.ssh/github_shop
IdentitiesOnly yes
Host github-blog
HostName github.com
User git
IdentityFile ~/.ssh/github_blog
IdentitiesOnly yes
Then use the alias in place of the host name:
git clone github-shop:USER/SHOP.git
ssh -T github-blog
For an existing clone, change the address as shown in how to change a Git remote URL.
When Port 22 Is Blocked
If outgoing SSH is blocked by a firewall, GitHub also answers SSH on port 443 at ssh.github.com. In the host entry, set HostName ssh.github.com and add Port 443. SSH then asks once more to confirm the host key; compare it with GitHub's published fingerprints again. This is about outgoing connections; changing the port your own server listens on is covered in how to change the SSH port.
Troubleshooting
- Permission denied (publickey): run
ssh -vT github.comand check which key files SSH offers. - Key already in use: that public key is already a deploy key on another repository or an account key; create a new key.
- Bad permissions warning: the private key is readable by other users; set it to 600.
- Works as one user, fails as another: keys and
~/.ssh/configbelong to one user. Run Git as the user who owns them.
SSH keys work the same way for logging in to your own server; see how to use PuTTY to SSH into a server.