Connect GitHub on a remote server with SSH

This exercise walks through connecting your GitHub account on a remote server with SSH.

Objective

Connect your GitHub on your AWS EC2 instance with SSH

Description

In this exercise, you will generate an SSH key pair on a remote server (EC2 instance) and add it to your GitHub account. This will allow you to push or pull the code from a repository into your server. Then you will clone a remote repository on your EC2 instance and start the application.

Then you will use the following repo to clone it on your EC2 instance and start the application.


Scenario

GitHub is used as a remote repository for both the backend and frontend code. Our backend APIs are hosted on an EC2 instance of AWS and thus we have to move the code to the server on EC2. There are many ways to do that but GitHub serves as a great way to pull the latest code or clone a repository to the server.

And when working with GitHub you will need to identify yourself to GitHub, this can be done using your username and password or with SSH keys. An SSH key is an alternate way to identify yourself that doesn’t require you to enter your username and password every time.

 

Acceptance criteria

  • Connect and login to an EC2 instance
  • Generate an SSH key pair on an EC2 instance
  • Add the SSH key to your GitHub account
  • Clone the following GitHub repo and start the application

 

Hints

  • Use the following command to generate an SSH key pair:
    ssh-keygen -t ed25519 -C "your_email@example.com"

     

    Solution

Introduction

SSH, or Secure Shell, is a protocol for securely accessing a remote computer. SSH uses encryption to protect data from being intercepted by third parties, making it an essential tool for anyone working with sensitive information. SSH also provides authentication, meaning that only authorized users can access the protected data.

In order to use SSH, you need to have a client program installed on your computer. This program will connect to a server running SSH and allow you to issue commands or transfer files. There are many different ssh clients available, so you can choose the one that best suits your needs. Once you have a client installed, using SSH is usually as simple as entering the desired command or file path into the program.

There are many different ssh clients available, each with its own unique features and functions. Some of the most popular ssh clients include PuTTY, OpenSSH, and WinSCP.

You can also use the terminal on Mac or Ubuntu for SSH.

– PuTTY is an ssh client for Windows systems that is known for its small size and high level of flexibility. It offers users a variety of customization settings, including the ability to customize keyboard shortcuts and control various aspects of the terminal layout.
– OpenSSH is a more robust ssh client that is commonly used on Unix-based systems. It offers advanced security features like public key authentication and allows users to easily transfer files between machines using ssh remote commands.

SSH protocol allows you to connect and authenticate to remote servers and services. However, SSH protocol requires SSH keys, which is an access credential. SSH keys come in pairs, a public key that gets shared with services like GitHub, and a private key that is stored only on your computer. If these keys match, you’re granted access thus allowing you to connect to GitHub without supplying your username and password every time.

Solution

Connect to your EC2 instance. You can do so by either using EC2 instance Connect or with an SSH client (connect to a remote server with SSH).

Once you login to your instance try cloning the following GitHub repository:

git clone git@github.com:url

You will be first asked to confirm if you want to connect to GitHub.

Permission to connect

Permission to connect with ssh

However, If you enter yes, permission will be denied and you will get the following message.

Permission denied

Permission to connect with ssh

The git clone fails because GitHub requires a way to authenticate you. You can identify yourself either with your username password or with SSH keys. In this example, we will be identifying using SSH keys.

Generating SSH key pairs: The first step in using SSH authorization with GitHub is to generate your own key pair.

You might already have an SSH key pair on your machine. You can check to see if one exists by going to your .ssh directory and listing the contents with the following commands.

 cd ~/.ssh

  ls

If you see id_rsa.pub, you already have a key pair and don’t need to create a new one.

If you don’t see id_rsa.pub, use the following command to generate a new key pair. Make sure to replace your@email.com with your own email address.

$ ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

You will then be asked to provide an optional passphrase. This can be used to make your key even more secure, but for this lesson, you can skip it by hitting enter twice.

When the key generation is complete, you should see the following message:

Confirmation message

keygen generated

Add your public key to GitHub

We now need to tell GitHub about your public key. Display the contents of your new public key file with cat:

$ cat ~/.ssh/id_rsa.pub

Copy the contents of the output to your clipboard.

Login to github.com and bring up your account settings by clicking the tools icon.

Select SSH Keys from the side menu, then click the Add SSH key button.

Finally, hit Add key to save. Enter your GitHub password if prompted.

Then test your SSH connection

Try to clone the repo once again and this time it will be successful. you can use the SSH clone URL when copying a repo to your local machine.