Guide to Git™ - Set Up Access to Private Repositories


Last modified: February 21, 2024

Overview

This document describes how to set up SSH access so you can clone a local cPanel repository to a remote private repository. You must generate and copy SSH keys to the remote repository server before you can clone a local repository to the remote server.

Important:
  • This tutorial uses GitHub as an example host for a private repository. However, most of the steps in this tutorial are similar to the steps for any other private repository host.
  • The steps in this tutorial require the SSH Access & Terminal feature.

Set up SSH access to private repositories

To set up access to private repositories, perform the following steps.

Connect to your server via SSH or the cPanel Terminal

For information on how to connect via SSH, review our SSH Access documentation. For information on how to connect to the cPanel Terminal, review our Terminal for cPanel documentation. Once you’ve successfully connected, continue following this guide, running the commands either in your SSH-connected command line interface or the cPanel Terminal.

Generate an SSH key

Run the following command to generate an SSH key file:

ssh-keygen -t rsa -f ~/.ssh/repo -b 4096 -C "[email protected]"
Note:

Replace repo with the name of the remote repository, username with your cPanel username, and example.com with your cPanel domain name.

For example, if your repository name is testing, your cPanel username is cptest, and your cPanel domain name is cptest.tld, run this command:

ssh-keygen -t rsa -f ~/.ssh/testing -b 4096 -C "[email protected]"

Warning:

This command contains several parts. If you alter any part of the command, you may affect the performance of your SSH key.

  • The -t flag specifies the type of algorithm for your SSH key.
  • The -f flag determines the name for your public and private keys. With the -f flag, there is no need to specify the public key name as it will always be the same name as the private key, but with .pub as the suffix.
  • The -b flag specifies the size of the SSH key in bits.
  • The -C flag specifies a comment to add to your public key. This is helpful when identifying which public keys you have authorized in a remote system, so it is common practice to add your email address as the comment.

After you run this command, the system will prompt you to enter a passphrase. Do not enter a passphrase, and press Enter to continue.

Create the SSH Configuration File

To create the SSH configuration file, perform the following steps:

  1. Run the following command to create the SSH configuration file:
    touch ~/.ssh/config
  2. Update the permissions of the ~/.ssh/config file to 0600:
    chmod 0600 ~/.ssh/config
  3. Run the following command to verify the cPanel user account:
    chown cpanelusername:cpanelusername ~/.ssh/config
  4. Open ~/.ssh/config with the text editor of your choice, and add the following lines where testing is the name of your repository:
    Host remote-git-repo-domain.tld
        IdentityFile ~/.ssh/testing
    Note:
    • If you want to use this private key when connecting to any remote host via SSH, you may use an asterisk character (*) as the host instead of a specific domain name. Otherwise, use the domain name of your remote Git repository provider. You can find this domain name within the SSH repository URL.
    • You must use the path to the private key generated above for the IdentityFile.
  5. Save the file.

Register your SSH key with the private repository host

Note:
  • For information about how to register your SSH key with another private repository host, consult that host’s website or documentation.
  • Some repository hosts, such as Bitbucket, do not allow you to configure write access for your access keys.

To register an SSH key with GitHub, perform the following steps:

  1. Log in to your GitHub account.
  2. Navigate to your private repository.
  3. In the top right corner of the page, click Settings. A new page will appear.
  4. In the left side menu, click Deploy keys. A new page will appear.
  5. In the top right corner of the page, click Add deploy key. A new page will appear.
  6. In the Title text box, enter a display name for the key.
  7. In the Key text box, paste the entire public SSH key from the ~/.ssh/repo.pub file you created in the previous step. For example, if you created a key with ~/.ssh/testing as the key file name, the public key would be in ~/.ssh/testing.pub.
  8. If you want to push code from your cPanel account to your GitHub account, select the Allow write access checkbox.
    Note:

    If you do not select this checkbox, you can only deploy changes from your GitHub repository to the cPanel-hosted repository.

  9. Click Add key.

Test the SSH key

To test your SSH key, run the following command:

ssh -i ~/.ssh/repo -T git@example.com
Note:

Replace repo with the name of the repository and example.com with the private repository’s domain name you input when you created the SSH key.

For example, if you input testing as the repo and github.com as the repository host’s domain name in the previous step, run this command:

ssh -i ~/.ssh/testing -T git@github.com

Set up access to multiple repositories

To create an SSH key for each of your repositories, follow the steps outlined above. After you have added the keys to the remote repositories, create a local ~/.ssh/config file to alias each of the keys to their corresponding repository names.

For example, if you have two repos configured on GitHub, testing and testing2, and both your cPanel and GitHub usernames are cptest, create or modify the ~/.ssh/config file with these contents:

Host github.com-testing
        Hostname github.com
        IdentityFile=/home/cptest/.ssh/testing

Host github.com-testing2
        Hostname github.com
        IdentityFile=/home/cptest/.ssh/testing2

Clone a repository: single repository on the remote host

To clone a repository when you have a single repository configured on the remote repository host, run this command:

git clone git@example.com:username/repo.git
Note:

Replace example.com with the domain name of the remote private repository, username with your username on the remote repository host, and repo with the name of the repository.

For example, if your GitHub username is cptest, and the repository name is testing, run this command:

git clone git@github.com:cptest/testing.git

Clone a repository: multiple repositories on the remote host

To clone a repository when you have multiple repositories configured on the remote repository host and have created separate SSH keys and the ~/.ssh/config file as referenced above, run this command:

git clone git@Host:username/repo.git

For example, if your GitHub username is cptest, and the repository name is testing2, run this command:

git clone git@github.com-testing2:cptest/testing2.git

Additional Documentation