2

On my EC2 instance I create a rails project (my_app) and then a repository of it :

deploy@ip-ec2: ~/my_app$ git init  

(then git add-A, then git commit -m "initialized", and everything is ok)

also in this server add the public key of my local machine: id_rsa.pub, to the file authorized_keys in server side.

Now in local machine, I want to clone the created repository that is in the ec2:

local@machine:~/repos$ sudo git clone ssh://[email protected]/~/my_app

output in local machine:

Clonar en «my_app»...
Permission denied (publickey).
fatal: Could not read from remote repository.

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

What I am missing? Should I have to change some permission in server project?

1 Answer 1

2

When you SSh to the EC2 node are you using ssh -i /path/to/key? To use the private key on the EC2 node, you will need to setup the ssh-agent and forward it via the SSH connection by using -A switch.

If you run: ssh-add -l, it should show all keys known to the agent.

$ ssh-add -l
4096 SHA256:xxxxxxxxxxxxxx /home/matt/.ssh/id_rsa (RSA)

If you run this from your EC2 node without forwardng the agent, you will see that the agent is not found, or that there are no known keys.

On your first host, check to see if the agent is running using the above command.

If it is running and had your key, then all you need to do is forward. If you do not have the agent running, you will need start a new instance by simply running ssh-agent, however this will print some commands you will need to run:

$ ssh-agent
SSH_AUTH_SOCK=/tmp/ssh-jZBbs4yRG03w/agent.6708; export SSH_AUTH_SOCK;
SSH_AGENT_PID=16496; export SSH_AGENT_PID;
echo Agent pid 16496;

The simplest way to handle this, is to start the agent and evaluate at the same time with one command:

$ eval `ssh-agent`
Agent pid 16496

Now add your keys, if they are the default path / name:

ssh-add
or
ssh-add /path/to/key

Now you are good to forward the agent to your EC2 node.

Sign up to request clarification or add additional context in comments.

6 Comments

excuse me my english is not good, the command ssh-add -l must be run in the ec2 or in local machine? and to the first question, yes i connect to ec2 like this ssh -i /path/mykey.pem ...
The agent will run local, but will then be forwarded to the EC2 node. So first local, then remote.
ok i run in local : ssh-add -l and returns 4096 SHA256:uQ.... , then run eval ssh-agent and return Agent pid 5555 then in the folder where is my ec2 key (~/aws/mykey.pem) run ssh-add is that correct?
If ssh-add -l has your key listed, then all you need to do is SSH to your EC2 node using -A, ssh -A ec2-user@ip From there, you will still have access to your key, ssh-add -l
bus how I use ssh -A in git clone? this is the command I try: git clone ssh://[email protected]/~/my_app
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.