简体   繁体   中英

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.

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).

Create a SSH config file

Create a SSH config file mechanisms to create aliases for your various identities.

You can construct a SSH config file using many parameters and different approaches.

The format for the alias entries use in this example is:

 Host alias HostName bitbucket.org IdentityFile ~/.ssh/identity 

To create a config file for two identities (workid and personalid), you would do the following:

Open a terminal window.
Edit the ~/.ssh/config file. 

If you don't have a config file, create one.
Add an alias for each identity combination for example:

Host workid
HostName bitbucket.org
IdentityFile ~/.ssh/workid

Host personalid
HostName bitbucket.org
IdentityFile ~/.ssh/personalid

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM