How to use ssh-key in a site with one or multiple sites (virtual hosts) to clone / pull your Git repository

Dagoberto Medina
2 min readJun 16, 2021

--

Normally, we need to deploy the code of our git repositories into our servers, for development or production environments.

Other reason to be here, is if you are receiving the error [ Git : fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists ]

In this case, the safe method to do this is using ssh keys, and clone or pull changes in anytime.

If you are using a unique site in your host, the process will be more standard and simple to solve, but, if you need to deploy multiple repositories in the same servers (virtual hosts), there exists some considerations to take.

First steps:

1. Generate a new ssh key

ssh-keygen -m PEM -t rsa -b 4096 -C “your@mail.com”

2. Edit and add your private key url:

/home/user/.ssh/git_key

3. Open the agent

eval $(ssh-agent -s)

4. Add the key to the agent

ssh-add ~/.ssh/git_key

5. Copy the public key

cat /home/user/.ssh/git_key.pub

7. Paste it to your repo settings in deploy keys section

You repository -> Settings -> Add deploy key

8. Copy the key to authorized keys (if not, you need to repeat steps 3 & 4 every time you need to pull changes from repository)

cat /home/user/.ssh/git_key.pub >> ~/.ssh/authorized_keys

9. Add the key to ssh config file

sudo nano /home/user/.ssh/config
# paste: IdentityFile /home/user/.ssh/git_key into the file

Now you are ready to git clone or git pull your repo into the folder of your web server. But, what if you has multiple virtual hosts? In this case:

Let’s move again to the step 9

sudo nano /home/user/.ssh/config

Edit the file with a format like this:

#Github Alpha
Host github.com-alpha
HostName github.com
IdentityFile ~/.ssh/git_alpha

#Github Beta
Host github.com-beta
HostName github.com
IdentityFile ~/.ssh/git_beta

#Github Gamma
Host github.com-gamma
HostName github.com
IdentityFile ~/.ssh/git_gamma

In every IdentityFile you need to use the previous created key for every repo. If not, repeat all steps from 1 to 8 for everyone of your repositories to add to your current server.

Finally, you need to edit the git config file from every repository you clone into your server folder.

a. Go to your repo

cd /var/www/your-repo

b. Edit your git config file

sudo nano .git/config

c. You get something like this

[remote “origin”]

url = git@github.com:YourUser/your-alpha-repo.git

d. Change github.com to your previous Host defined in the ssh config file

[remote “origin”]

url = git@github.com-alpha:YourUser/your-alpha-repo.git

e. Repeat for every project / repo as you need

Done! We are ready to work with multiple repositories in the same server!

Thanks for read it! Let your comments or questions!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Dagoberto Medina
Dagoberto Medina

Written by Dagoberto Medina

Full Stack & DevOps. Cybersecurity enthusiast and continuous improvement.

No responses yet