D
D
Denis Titusov2016-05-24 14:14:27
Python
Denis Titusov, 2016-05-24 14:14:27

How to tell git which ssh key to use when logging in?

Good afternoon!
There is the following task. Some web service has a library of scripts that I want to version.
The solution looks like this:

  • Create an empty folder
  • Clone the repository into it from the server
  • Downloading the script library via API
  • Checking if there are changes in files
  • If there are changes, then send to the server

Everything is good, except for interacting with the git server. I want the script to be authorized through an ssh key. Internet searches did not give any other solution than to write the key to ~/.ssh. Unfortunately, this solution is not suitable, since the script will be executed on TeamCity agents and I would like to be able to simply put a key on it.
The question arose because:
  • In ssh, you can specify which key to use.ssh -i $path_to_key
  • In SourceTree, you can specify a third-party key for git
  • teamcity has its own library of keys with which it is authorized
  • However, I did not find the git key to change the ssh key

Perhaps this task can be solved not through a standard utility, but through third-party libraries for programming languages? Any option will do.
Thank you in advance!
UPD1: Editing the file ~/.ssh/configwould be fine if not for:
  1. We are talking about teamcity agents. In this case, it turns out that on all agents where the script is launched, the config must be corrected, or I must limit the list of agents on which the script is launched. This solution is not much different from putting a file~/.ssh/id_rsa
  2. The script will most likely run on Windows machines. ~/.ssh/configThis means that it will be problematic to enclose centrally .

Answer the question

In order to leave comments, you need to log in

4 answer(s)
D
Denis Titusov, 2016-05-25
@denis-titusov

Unexpectedly, the solution was found in the Ansible code
. It is done like this:

  1. create a file ssh_git.sh with the content:
    #!/bin/sh
    if [ -z "$GIT_SSH_OPTS" ]; then
        BASEOPTS=""
    else</li>
        BASEOPTS=$GIT_SSH_OPTS
    fi
    
    if [ -z "$GIT_KEY" ]; then
        ssh $BASEOPTS "[email protected]"
    else
        ssh -i "$GIT_KEY" $BASEOPTS "[email protected]"
    fi</li>

D
Dmitry Murzinov, 2016-05-24
@iDoka

$ cat ~/.ssh/config
Host         my_github
Hostname     github.com
IdentityFile ~/.ssh/github.rsa

to login via ssh, the syntax is something like this:
I think for git, specifying the host is superfluous

C
chupasaurus, 2016-05-24
@chupasaurus

Create a file ~/.ssh/config. In it for each user:

Host git-as-user1
  HostName git.company.com
  User git
  IdentityFile /home/user1/.ssh/id_rsa.user1
  IdentitiesOnly yes

Then git remote add user1 [email protected]:repo.git

S
spotifi, 2016-05-24
@spotifi

If you are a reader only - use the Deploy Key. Maybe one common.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question