Learn different ways to manage SSH keys on your servers when you automate deployment scripts and which way is best for you.
You can manage SSH keys on your servers when automating deployment scripts using SSH agent forwarding, HTTPS with OAuth tokens, deploy keys, or machine users.
SSH agent forwardingIn many cases, especially in the beginning of a project, SSH agent forwarding is the quickest and simplest method to use. Agent forwarding uses the same SSH keys that your local development computer uses.
Pros of SSH agent forwardingssh -A serverA 'bash -s' < deploy.sh
If you don't want to use SSH keys, you can use HTTPS with OAuth tokens.
Pros of HTTPS cloning with OAuth tokensSee our guide on creating a personal access token.
Deploy keysYou can launch projects from a repository on GitHub.com to your server by using a deploy key, which is an SSH key that grants access to a single repository. GitHub attaches the public part of the key directly to your repository instead of a personal account, and the private part of the key remains on your server. For more information, see Delivering deployments.
Deploy keys with write access can perform the same actions as an organization member with admin access, or a collaborator on a personal repository. For more information, see Repository roles for an organization and Permission levels for a personal account repository.
For enhanced security and fine-grained control over repository access and permissions, we recommend using a GitHub App instead. See Deciding when to build a GitHub App.
Pros of deploy keysRun the ssh-keygen
procedure on your server, and remember where you save the generated public and private rsa key pair.
On GitHub, navigate to the main page of the repository.
Under your repository name, click Settings. If you cannot see the "Settings" tab, select the dropdown menu, then click Settings.
In the sidebar, click Deploy Keys.
Click Add deploy key.
In the "Title" field, provide a title.
In the "Key" field, paste your public key.
Select Allow write access if you want this key to have write access to the repository. A deploy key with write access lets a deployment push to the repository.
Click Add key.
You can also use the REST API to create deploy keys. For more information, see REST API endpoints for deploy keys.
Using multiple repositories on one serverIf you use multiple repositories on one server, you will need to generate a dedicated key pair for each one. You can't reuse a deploy key for multiple repositories.
In the server's SSH configuration file (usually ~/.ssh/config
), add an alias entry for each repository. For example:
Host github.com-repo-0
Hostname github.com
IdentityFile=/home/user/.ssh/repo-0_deploy_key
Host github.com-repo-1
Hostname github.com
IdentityFile=/home/user/.ssh/repo-1_deploy_key
Host github.com-repo-0
- The repository's alias.Hostname github.com
- Configures the hostname to use with the alias.IdentityFile=/home/user/.ssh/repo-0_deploy_key
- Assigns a private key to the alias.You can then use the hostname's alias to interact with the repository using SSH, which will use the unique deploy key assigned to that alias. For example:
git clone git@github.com-repo-1:OWNER/repo-1.git
GitHub App installation access tokens
If your server needs to access repositories across one or more organizations, you can use a GitHub App to define the access you need, and then generate tightly-scoped, installation access tokens from that GitHub App. The installation access tokens can be scoped to single or multiple repositories, and can have fine-grained permissions. For example, you can generate a token with read-only access to a repository's contents.
Since GitHub Apps are a first class actor on GitHub, the installation access tokens are decoupled from any GitHub user, which makes them comparable to "service tokens". Additionally, installation access tokens have dedicated rate limits that scale with the size of the organizations that they act upon. For more information, see Rate limits for GitHub Apps.
Pros of installation access tokensid
.installation_id
that represents the connection between your GitHub App and the organization repositories it can access. Each GitHub App and organization pair have at most a single installation_id
. You can identify this installation_id
via Get an organization installation for the authenticated app. This requires authenticating as a GitHub App using a JWT, for more information see Authenticating as a GitHub App.For more information, see Generating an installation access token for a GitHub App.
Machine usersIf your server needs to access multiple repositories, you can create a new account on GitHub.com and attach an SSH key that will be used exclusively for automation. Since this account on GitHub.com won't be used by a human, it's called a machine user. You can add the machine user as a collaborator on a personal repository (granting read and write access), as an outside collaborator on an organization repository (granting read, write, or admin access), or to a team with access to the repositories it needs to automate (granting the permissions of the team).
Tip
Our terms of service state:
Accounts registered by "bots" or other automated methods are not permitted.
This means that you cannot automate the creation of accounts. But if you want to create a single machine user for automating tasks such as deploy scripts in your project or organization, that is totally cool.
Pros of machine usersssh-keygen
procedure on your server and attach the public key to the machine user account.RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4