Connect to a remote server with SSH

This exercise walks through connecting to a remote server with SSH

Objective

Connect to the EC2 instance from your local machine using SSH and the private key file.

Description

In the previous exercises, you connected to your EC2 instance using the EC2 instance connect in the AWS console. But it is very likely that you would want to connect to your server from your local machine using SSH. The SSH stands for ‘Secure Shell’. It is a data transfer protocol designed to securely encrypt and transmit data between two systems. So, in this exercise, connect to your EC2 instance using SSH.

 

Acceptance criteria

Part 1:

  • Use the private key (created and downloaded while setting up the EC2 instance) and connect to the EC2 instance from your local machine.
  • Download a file from a remote server to your local machine
  • Upload a file from your local machine to the remote server (EC2 instance)

 

Hints

  • To connect to a remote machine, you need its IP address or name, and a private key file if it is created.

  • You can connect to a remote server with the following command from your SSH client.

    Eg. ssh -i private_key_file.pem 192.168.1.1

    Here 192.168.1.1 is the IP address

  • Use scp command to upload or download content from a remote server.

 

Solution

  • Open an SSH client (terminal).

  • Locate your private key file. This key was created and downloaded while setting up and launching the EC2 instance (ecomm-proj.pem).

  • Run this command, if necessary, to ensure your key is not publicly viewable.

    chmod 400 ecomm-proj.pem

  • Connect to your instance using its Public DNS (ec2-18-191-19-164.us-east-2.compute.amazonaws.com). E.g:

    ssh -i ecomm-proj.pem ubuntu@ec2-18-191-19-164.us-east-2.compute.amazonaws.com
    
  • The first time you connect you will see the following message:

    The authenticity of host 'ec2-18-191-19-164.us-east-2.compute.amazonaws.com (18.191.19.164)' can't be established.
    ECDSA key fingerprint is SHA256:wr6Jok7qeQGn1bgc9gvn3dB7dVCwN6ltaV+ocVWELH0.
    Are you sure you want to continue connecting (yes/no)? yes
    
  • Type yes and press enter. This should log in to your remote server.

Logged into server:with SSH

  • Type ls command to see the files and directories on the server. You should see the previously created espress-app folder.

 

Download a folder from the remote server to local machine

scp -i ecomm-proj.pem ubuntu@ec2-18-191-19-164.us-east-2.compute.amazonaws.com:express-app /Users/userName/desktop/express-app
  • scp command: It is used to securely copy files and directories between remote hosts. The SCP command uses SSH to transfer data.

  • ecomm-proj.pem: This is the private key that was created. It is used for authentication.

  • ubuntu@ec2-18-191-19-164.us-east-2.compute.amazonaws.com/express-app: This is in the format username@source_host:directory

  • /Users/userName/desktop/express-app: This is the location where the remote folder will be downloaded on your local machine. It will create a folder express-app if it does not exists.

Copy a file from your local machine to a remote server

  • You can use the scp command to copy a file from your local machine to a remote server
    scp -i ecomm-proj.pem /Users/userName/desktop/express-app ubuntu@ec2-18-191-