Search code examples
gitsshssh-keyssshdssh-agent

How to configure ssh agent service and keys for git deployments using non-root user?


I had a problem when doing deployments (using non-root user):

$> git pull
Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

After some digging here and there I've figured out the workaround that works for me:

$> eval $(ssh-agent)
Agent pid 61080
$> ssh-add ~/.ssh/deploymapr
Identity added: /home/mapr/.ssh/deploymapr (/home/mapr/.ssh/deploymapr)
$> git pull
remote: Counting objects: 10, done.
remote: Compressing objects: 100% (10/10), done.
remote: Total 10 (delta 8), reused 0 (delta 0)
Unpacking objects: 100% (10/10), done.
⋮

Now that I do the deployments quite often - I need to repeat steps above each and every time I log on to the deployment server which is a bit of a hassle.

I'm sure there's better, elegant way to configure this for a regular user (and I'm not talking about adding these two lines to ~/.bashrc…). Any ideas?

Update: As per answers below, I've configured my ~/.ssh/config file:

stat -c "%A %a %n" ~/.gitconfig ~/.ssh/deploymapr ~/.ssh/config
-rw-rw-r-- 664 /home/mapr/.gitconfig
-rw------- 600 /home/mapr/.ssh/deploymapr
-rw-r--r-- 644 /home/mapr/.ssh/config

but I still get error (when not running workaround=the two commands above, manually):

$ git pull
Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

Solution

  • Store the information in ~/.ssh/config:

    Host hostname
     IdentityFile ~/.ssh/deploymapr
    

    Or move the key to the standard location (~/.ssh/id_rsa or respective by manual page).