Connecting to Git repo on own web server with SSH

2019-06-05 12:05发布

I have 2 web servers. On server A I have a GitLab installed.

On server B I just initialized a bare Git repo.

Now I want to automatically push the GitLab repo to server B using GitLab's remote repository mirror function

For this I need an HTTPS or SSH url, and I want to do it with SSH.

Question 1: What is the url I need to insert there? I guess it should be something like ssh://ssh-user@server-b.com:path-to-git-repos/project.git. Is that right?

Question 2: Since the user connecting to the url is GitLab's Git user, I guess I need to create an own SSH key for this user and add the public key to my web server. How can I do this since the Git user has no permissions to do anything except from Git commands on terminal level? I guess I need to create it with the root user of my GitLab server, but how do I do that? I can't even find the Git user's home dir when I'm connected as root.

标签: git ssh gitlab
1条回答
萌系小妹纸
2楼-- · 2019-06-05 12:15

ssh://ssh-user@server-b.com:path-to-git-repos/project.git is the correct URL if path-to-git-repos/project.git is owned by user ssh-user, and if there is no other Git hosting services.

Since the user connecting to the url is GitLab's Git user, I guess I need to create an own SSH key for this user and add the public key to my web server.

Yes, a dedicated private/public ssh key pair needs to be created, with the public one going to serverB ~ssh-user/.ssh/authorized_keys: you can copy it yourself.
Check ps -eaf|grep gitlab on server A: you will see which account is running GitLab.
Creates your SSH keys in the account ~/.ssh folder.

From the comments, the trick was:

  • to generate ssh key pairs from the root account, and move the keys to ~git/.ssh
  • to copy ~git/.ssh/id_rsa.pub over to serverB, ~ssh-user/.ssh/authorized_keys
  • to test the ssh connection from the root account (since the git account is limited to GitLab command)

    ssh -i ~git/.ssh/id_rsa ssh-user@serverB
    

That has the side effect to fill out the ~root/.ssh/known_hosts file, that can then be copied over to ~git/.ssh/known_hosts

  • to use the SSH URL:

    ssh://ssh-user@server-b.com/absolute/path-to-git-repos/project.git
    

And the mirroring can start working.

查看更多
登录 后发表回答