Alpha Coder

How to Git push to an AWS EC2 remote using a PEM file

AWS provides you with a .pem file when creating an EC2 instance. You can use this file to generate SSH keys for accessing your server without the need for the PEM, as well as push to a remote Git repository on the server. Here’s how…

1. Copy private key in PEM to .ssh folder

$ cp /path/to/my-aws-ec2-instance.pem ~/.ssh/id_rsa_ec2

2. Generate and save public key

$ ssh-keygen -y -f /path/to/my-aws-ec2-instance.pem > ~/.ssh/

3. Add private key to ssh-agent

Start ssh-agent

$ eval "$(ssh-agent -s)"

Then add your key to the agent

$ ssh-add ~/.ssh/id_rsa_ec2

At this point, you should be able to access your instance without the PEM file. Instead of:

$ ssh -i my-aws-ec2-instance.pem


$ ssh

4. Add remote and push

Change directory to your local Git repository and add the remote URL of your server

$ git remote add ec2server

Now push…

$ git push ec2server

Need help getting something to work in your project? Try Alpha Coder Support!

Subscribe to the Alpha Coder Newsletter!

Get timely updates on new articles, courses and more from Nicholas Kajoh. Unsubscribe anytime.

Enjoy the content on Alpha Coder? Please buy me a coffee. 😊

Previous post: 2 Git workflows you should know

Next post: Check domain availability from your terminal

comments powered by Disqus