This exercise walks through connecting to a remote server with SSH
Connect to the EC2 instance from your local machine using SSH and the private key file.
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.
- 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)
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.
ssh -i private_key_file.pem 192.168.1.1
Here 192.168.1.1 is the IP address
scpcommand to upload or download content from a remote server.
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 firstname.lastname@example.org
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 (22.214.171.124)' 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.
lscommand to see the files and directories on the server. You should see the previously created
Download a folder from the remote server to local machine
scp -i ecomm-proj.pem email@example.com:express-app /Users/userName/desktop/express-app
scpcommand: 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.
firstname.lastname@example.org/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-appif it does not exists.
Copy a file from your local machine to a remote server
- You can use the
scpcommand 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-