Learn to Set Up Access to Private Repositories Using Git
When your code is stored in a private repository, it is protected for a reason. Only selected users should get access, and for that, Git needs proper authentication. Git works best with SSH when it comes to secure access. The process to set this up helps Git understand that it is you trying to fetch or push code, and not a hacker or intruder.
The steps are mostly the same for GitHub or another platform that hosts private repositories. So, though this article will take GitHub as our main example, if you are using something like GitLab, Bitbucket, or any other similar service, you can follow along with small changes.
Moreover, this process assumes that you have access to cPanel or a similar control panel (WHM v70) with shell access. For a more recent version of cPanel (version 72 or later), you will be able to utilise the Terminal feature to run commands directly from the cPanel interface. This makes it easier to generate keys and clone repositories without needing to leave the platform.
Let’s go step-by-step and see how to set this up properly.
Guide to Set Up SSH Access to Private Repositories
Create Your SSH Key
The first step is to create an SSH key pair on your local machine (or in the terminal of your cPanel). You will need to generate it in case you do not have one. With this key, you can establish a secure connection between your local system and the private repository host.
Open your terminal and enter the following command:
ssh-keygen -t rsa -b 4096 -C "username@example"
Here, replace [email protected] with your actual email address.
Note – You can leave the passphrase field empty by pressing Enter. However, without one, anyone with access to your private key file can use it.
The system will generate a public and private key pair, stored in the .ssh directory of your home folder.
Check if the SSH Key is Created Properly
Now, confirm that your SSH key is sitting in the right place.
Use this command:
cat ~/.ssh/id_rsa.pub
This should print out a long string starting with ssh-rsa and a bunch of characters after that. If you see something like that, your key is ready to go.
Note the full key; you will need to copy and paste this into your repository host to register it.
If the system tells you something like:
cat: /home/username/.ssh/id_rsa.pub: No such file or directory
That means the key is missing or the path was typed wrong. Double-check the command, and if needed, just go back and create the key again using the first command.
Also, if you accidentally run the command on the private key, and you see something like:
—–BEGIN RSA PRIVATE KEY—–
Then stop there. You are looking at the wrong file. Make sure you are using the public key file (id_rsa.pub), not the private one.
Add Your SSH Key to GitHub
Next, you will need to take the public key that you got from the last step and paste it into your GitHub account.
Follow the steps below on GitHub –
- Log in to GitHub and navigate to the private repository where you want to give access.
- Go to Settings from the top-right corner.
- On the left menu, under the Deploy keys, click on the Add deploy key button.
- Give your key a name in the Title box. You can name it something like “cPanel Access” or anything else.
- Paste the full SSH key into the Key field.
- If you want to push code from your cPanel to GitHub, make sure you tick the box that says Allow write access.
- Finally, click on Add key to save it.
GitHub now knows it is safe to let your cPanel connect.
You will still be able to pull data from GitHub if you don’t tick the write access box. However, you won’t be allowed to push any changes back from your side. This setting determines whether you can push changes to the repository or simply clone it. Always check the box if you plan on pushing updates from your local machine.
Remember, refer to the documentation for how to add SSH keys if you are using a different hosting service like Bitbucket or GitLab.
Test the SSH Connection
To make sure everything is working, you should test your SSH setup.
Run this command:
ssh -T [email protected]
Replace example.com with your actual GitHub hostname or the host of the private repository.
You will get a message saying something like ‘You’ve successfully authenticated, but GitHub does not provide shell access.’ This confirms that your SSH connection to GitHub is working.
Clone the Private Repository
You are now ready to clone the private repository to your local system. To do this, use the following Git command:
git clone [email protected]:username/private-repository.git
Replace username with your GitHub username and private-repository with the actual name of your repository.
After this, your code should start downloading, and you will be all set.
So that’s how you can connect your cPanel server with a private Git repository using SSH keys. Once it is done, your setup will be secure and smooth to use.
Security Note: Never share your private SSH key. Only the public key should be shared when registering it with a repository host. If your private key is exposed, it compromises the security of your SSH access.