So you have a ec2 amazon server instance set up and you plan to set up git into the server.
When you use git to push and pull requests from the repository you are always required to enter your username and password for the repository. You probably would get Error: Permission denied (publickey). In case of your ec2 instance you dont have a username and password attached to your git repository so how do you push/pull the repo. The repo is not public after all.
We have recently moved our git repositories over to Amazon Web Services. We ran into one issue with it which was that now our git requests needed to have a key attached. The community was not very kind on explaining how to do this very well so I’m documenting it here.
The key that you want to use is the one generated for you by AWS. The key may be a pem, psk or pub file. It is the same one you use when you ssh into your AWS EC2 instances. Take this key file,say ec2-key.pem, and configure our git to use it.
First copy the ec2-key.pem into your .ssh folder or to any other folder that you with to put your pem files. I will put them in the .ssh folder for now.
$ cp /path/to/file/ec2-key.pem ~/.ssh/ec2-key.pem
Also make sure that the pem file has a permission access of 400 i.e. -r———, if not then
$ sudo chmod 400 ~/.ssh/ec2-key.pem
Then open your .ssh/config file. Create one if you dont have one. If you are on linux then nano will create the file if you don’t have one and lets you edit the file.
$ nano ~/.ssh/config
Make sure the config file has permission of 600.
$ sudo chmod 600 ~/.ssh/config
Then in the config file add these lines
Host git
Hostname EC2PublicDNS
User username
IdentityFile /home/username/.ssh/ec2-key.pem
Let me brief you with the parameters,
The Host is the name that you will use later to identify the server. The Hostname is the IP address of your amazon server. The User is the username you use to ssh the server and finallt the IdentityFile is the path to your pem file.
Finally we are set to make git transactions with our server.
To clone the repository use the following
$ git clone git:path/to/projects/PROJECT_NAME.git
and to add existing project to the bare repo
$ git remote add origin git:path/to/projects/PROJECT_NAME.git
To update existing projects on your machine we need to use the new address in the git config. So we need to override the previously existing repo address.
Open your project’s git config file
$ nano ~/path/to/project/.git/config
Change the url line to be
git:path/to/projects/PROJECT_NAME.git
That’s it.