Tag Archives: transfer files to ec2 linux instance

How to download files from EC2 to local machine using winSCP

Learn how to transfer files between desktop and EC2 using WinSCP. Using key-based authentication, winSCP can be connected to EC2 to download/upload files from/to the server.

Transfer data to/from EC2 using winSCP

While working on the EC2 server hosted on AWS, one of the basic requirements you come across is to transfer data between your desktop/laptop and EC2 instance. Since EC2 uses key-based authentication, for beginners it’s hard to understand how to transfer data from desktop to EC2 cloud.

Normally, programs like WinSCP are used to transfer data between the Linux server and the windows machine. In this article we will walk you through how to add key-based authentication in WinSCP. Later how to download files from EC2 to the local machine.

Other EC2 related posts which might interest you :

Step 1: Know your DNS name

Make sure your EC2 instance is spun up. You have the Public DNS name of your EC2 instance. You can see it under instance description in your AWS EC2 console.

Take our AWS CSA exam quiz! Check your knowledge.

Refer screenshot below :

Public DNS of EC2 instance

Step 2 : Set private key for authentication

Open WinSCP tool. Click on Advanced to open settings of tool.

Open winSCP settings

Under settings, click on Authentication under SSH in the left panel. This will open up authentication settings on the right panel.

Authentication settings in winSCP

Under, Authentication parameters tick ‘Allow agent forwarding‘ and browse your private key file in it. This private key file is key the same file you use to authenticate to EC2 when connecting via PuTTY

Click OK and close settings.

Step 3 : Connect

Copy public DNS of your EC2 instance, username as ec2-user for RedHat (different Linux distro has diff default logins in AWS. List of all is here) and hit connect. It will pop up to accept the key if you are connecting for the first time via WinSCP. Accept it and you will be connected to the EC2 server!

I have created small GIF which shows whole above process. Have a look .

Connect EC2 using winSCP

Now you can download or upload files from EC2 to local like you normally do!

How to transfer data between two EC2 Linux instances

Small tutorial to learn how to transfer data between two EC2 Linux instances on AWS. Explains the use of key authentication in the SCP command.

Copy data between two EC2 Linux instances

EC2 instance in AWS is a server instance that uses key-based authentication for login. Now, beginners or first time EC2 users wonder how to copy files from one EC2 server to another? or how to transfer data between two EC2 instances? You can achieve it using key files in scp command.

Few pre-requisites are :

  • Source and destination EC2 instances should be in the same region (can be in different availability zones)
  • Both instances should be able to communicate over port 22. Configure security groups accordingly.
  • EC2 instances built with specifying key pairs at the time of launch.

Lets get into actual stuff now. For example consider below setup :

  1. Two EC2 servers in the same region installed with Red Hat Linux
  2. Security groups allow port 22 both direction communication
  3. Key pairs used at the time of launch for both servers.
  4. The private key file is ready with me (.pem file). It’s part of the key pair used during launch.

In this tutorial, we will be copying file testfile.tar from server kerneltalks1 to kerneltalks2. Look at the file on kerneltalks1.

[root@kerneltalks1 ~]# ls testfile.tar
-rw-r--r--. 1 root root 39198720 Dec 19  2016 testfile.tar

Now, you need to use secure copy scp command with key file for authentication like below :

[root@kerneltalks1 ~]# scp -i /root/mykey.pem testfile.tar ec2-user@172.31.24.59:/tmp

Here,

  • -i switch used to specify the key file
  • Then source file path
  • followed by destination in userid@hostname/ip:/path/ format.

You have to use the Private IP of the destination EC2 instance. You can get this from your AWS EC2 console web page. Here kerneltalks2 has private IP 172.31.24.59. You can use hostname if you make an entry in /etc/hosts file.  Remember different Linux distros on AWS have different login id to be used.

# scp -i /root/mykey.pem testfile.tar ec2-user@172.31.29.79:/tmp
The authenticity of host '172.31.29.79 (172.31.29.79)' can't be established.
RSA key fingerprint is 66:c4:ce:37:c6:e6:a1:6c:2f:f9:9b:f2:f5:05:e3:38.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.31.29.79' (RSA) to the list of known hosts.
testfile.tar                                                                                                               100%   37MB  37.4MB/s   00:00

In the above output, you can see kerneltalks2 authenticated using key file and file transfer was completed.

[root@kerneltalks2 ~]# ls /tmp/testfile.tar
-rw-r--r--. 1 root root 39198720 Dec 19  2016 testfile.tar

Rsync is another good way to copy data between two EC2 instances. You can learn about it in our other article: Rsync for EC2 on AWS.

Conclusion :

Files and directories can be transferred between two ec2 instances using the same Linux scp command. Only the authentication part is to be done using a key file with -i switch.