Understanding Secure Copy Protocol in Linux (Copying the file from Local to EC2 machine)

Understanding Secure Copy Protocol in Linux 
(Copying the file from Local to EC2 machine)

Last Blog Review

In the last blog we understood, how tail command makes it simple to read large file by saving time, to detect issue quickly, and efficient troubleshooting. Along with different arguments which allows to display end lines, bytes. Hope it was great.

What is scp →

It’s a command which we always run from the source server & directed towards the destination server. scp is used to copy the data from source server to destination server. Always remember the scp command is used on the local machine and not on the EC2 machine, because when we do scp we specify the private key file (Mihir.pem) which we have (on local machine) and public key file (on EC2 machine) can be matched also we have the EC2 machine address(ubuntu@ec2-3-94-96-214.compute-1.amazonaws.com:/home/ubuntu) as destination.

But if we want to copy something from the EC2 machine to local machine, then writing scp command at the source i.e. on the EC2 machine is not possible because even if we have the public key file on EC2 machine to be mapped to the private key file (Mihir.pem) on local machine but we don't have the name of the local machine, because there are a lot of local machine's so we are not sure that the copy from the EC2 machine will happen correctly to our local machine or not, and if not then the public key file on EC2 machine can be exposed to other local machines. So, if we want to copy something from the EC2 machine to our local machine use the scp command on the local machine only just change the source and destination. Look at point 2 in below answer

  1. Copy the files from local machine to EC2 machine.

Let's say that there is file on my local machine(Windows) named "Local.txt" with content as "This is from local". Now i want to copy this file from my local machine(Windows) to the EC2 machine. So, I will use "scp" command in my local machine(Windows). Lets see how

On my local machine(Windows) first I will go to the directory where the "Local" file is there. Its in the downloads so i will go to the downloads directory. Make sure that the file you want to transfer (Local) and the private key file (Mihir.pem) of the EC2 machine can be present in the same directory as it will be easy to be in same place in cmd instead of changing the directory again and again. So, both are in downloads folder.

Local Machine CMD (Windows)
---------------------------
C:\Users\hp>cd Downloads
C:\Users\hp\Downloads>

Then I will use "scp" command as "scp -i Mihir.pem Local ubuntu@ec2-3-94-96-214.compute-1.amazonaws.com:/home/ubuntu" means "scp" to copy from source to destination, "-i" is identity file, "Mihir.pem" file for allowing the matching of the private key file(Mihir.pem) & public key file on EC2 machine to allow the secure connection, next "Local" is the source data, next "ubuntu@ec2-3-94-96-214.compute-1.amazonaws.com:/home/ubuntu" is the destination where the data has to be copied.

Local Machine CMD (Windows) 

--------------------------- 

C:\Users\hp\Downloads>scp -i "Mihir.pem" Local.txt ubuntu@ec2-3-94-96-214.compute-1.amazonaws.com:/home/ubuntu 

Local.txt 100% 20 0.1KB/s 00:00 

C:\Users\hp\Downloads>

As you see that the it shows 100% complete of copying with a speed of 0.1kb/s.

Now we will go to our EC2 machine and check if the file has been copied to the given location or not properly.

i-0c8dd7a6179312bf1 (EC2) 
-------------------------
ubuntu@ip-172-31-47-80:~$ ls 
Local.txt fil.txt log.txt 
ubuntu@ip-172-31-47-80:~$ cat Local.txt 
This is from local
ubuntu@ip-172-31-47-80:~$

See its successfully copied from one server i.e. our local machine to other server i.e. our EC2 machine.

Conclusion -

So, here we understood how to copy the files from the local to EC2 machine using scp. scp is actually helpful when you want the files from the local machine to the EC2 machine and vice versa because when you have your code or data which is present at the local machine and that needs to be on the EC2 so scp actually helps alot instead of writing the same stuff back.

💡
That’s a wrap for today’s post! I hope this has given you some valuable insights. Be sure to explore more articles on our blog for further tips and advice. See you in the next post!