33

I have 2 EC2 instances running Ubuntu 14.04 and I need to figure out how to transfer files from one to another. I read the FAQs from Amazon and it says that I can do this without incurring any additional costs if I use the private IP but I am not sure how to transfer the files using that.

Right now I use the scp protocol to do this -

scp -i ~/Path-To-Key-File/AAA.gem /path/file  ec2-user@<Elastic IP>:/path/file

I tried replacing the elastic IP with private IP but it doesn't work. Am I doing something wrong here?

Kshitiz
  • 2,852
  • 5
  • 32
  • 41

5 Answers5

52

Actually, I figured it out ... I just needed to replace the Elastic IP with the private IP and configure the security groups properly to allow instances to communicate!

Transferring from Machine A to Machine B

I am running this code on machine A

scp -i ~/Path-To-Key-File/AAA.pem /path/file  ec2-user@<Private IP of Machine B>:/path/file

For security groups, I had to allow SSH protocol over the private IP (from Machine B)!!

Nirav Radia
  • 167
  • 3
  • 12
Kshitiz
  • 2,852
  • 5
  • 32
  • 41
  • it's asking for passphrase to key, I know it but I want a one liner, how can I solve this? – Diego Jan 30 '18 at 23:11
  • @KshitizShankar I'm getting permission denied, I have applied same security group and also using the private ip, can you help me with this? – Rohit Khatri Apr 03 '18 at 07:02
  • 4
    can you please explain how to actually configure the security groups too? Without that explanation the answer is not overly helpful... – Ben Muller Nov 24 '19 at 22:56
  • 2
    @BenMuller Go to your ec2 instance's security group profile. Click on 'edit inbound rules'. Select 'ssh' from the dropdown. Then in 'Source' type in the name of your security group and click on the auto-complete popup. This should allow transfers between all ec2 instances within that security group - it did for me! – Hillary Sanders Mar 03 '20 at 19:58
22

Assuming both of your instances are EC2 linux instances.

suppose you want to transfer file from the second instance(ec2-2) to first instance(ec2-1), the command should be run in ec2-1 is:

scp -i  /Path-To-Key-File-for-ec2-2/key.pem  ec2-user@Elastic-IP-of-ec2-2:/path/filename your/local-path-on-ec2-1/filename

A corresponding discussion you can find here

Hope this help!!

Prasad Khode
  • 6,602
  • 11
  • 44
  • 59
Tapaswi Panda
  • 1,041
  • 7
  • 13
  • Actually, I need to be able to transfer files from ec2-2 to ec2-1 by running the code on ec2-2 – Kshitiz Aug 20 '14 at 13:09
  • Ok, To send files from your ec2-2 instance to ec2-1 being on ec2-2, the command would be: scp -i /Path-To-Key-File-for-ec2-1/key.pem your/local-path-on-ec2-2/filename ec2-user@Elastic-IP-of-ec2-1:/path-on-ec2-1/filename – Tapaswi Panda Aug 20 '14 at 13:31
  • 1
    sorry I edited the comment but it didn't get updated -- My issue was not with copying the files, it was with using Elastic IP and paying extra for data transfer as compared to using private IP and doing that for free – Kshitiz Aug 20 '14 at 13:36
  • Right, you can use private IP instead of EIP. But you need to allow ssh for those IP through corresponding instancs's security group. – Tapaswi Panda Aug 20 '14 at 13:45
  • what if both instances are in different aws accounts? I want to copy from Jenkins workspace to another ec2 directory. Can ssm be used? – DJ_Stuffy_K Mar 12 '21 at 15:13
4

This question is asked about authentication with the .pem file. But accessing without auth could be helpful in some cases. Here, you will authorize another machine instead.

Say, you like to ssh or scp from machine-1 to machine-2.

In machine-1.

  • Check if there is a public key file (id_rsa.pub) in USER_HOME/.ssh/. If not, generate it with ssh-keygen -t rsa command.

In machine-2

  • Uncomment PubkeyAuthentication yes in /etc/ssh/sshd_config.
  • Open file USER_HOME/.ssh/authorized_keys and append contents of id_rsa.pub file from the machine-1.

Now you can copy it with scp as following:

scp username_machine1@ip_machine1:/file/to/copy /destination/path

You are done. Enjoy!!!

For detailed information please check here.

misbah
  • 179
  • 6
  • what if both instances are in different aws accounts? – DJ_Stuffy_K Mar 12 '21 at 08:59
  • 1
    For me, this answer worked great, only the scp command didn’t work as stated. For the scp I used `scp username_machine1@ip_machine1:/file/to/copy /destination/path` and everything worked. – H. Shad Sep 21 '21 at 13:38
  • 1
    Thanks @H.Shad, I've corrected my answer accordingly. – misbah Sep 26 '21 at 11:20
1

scp -i /home/centos/b1.pem centos@ip:/etc/httpd/conf/httpd.conf httpd.conf.j2

  • 2
    Welcome to Stack Overflow! While this code snippet may solve the question, [including an explanation](//meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Please also try not to crowd your code with explanatory comments, this reduces the readability of both the code and the explanations! – Filnor May 04 '18 at 21:17
1

Copy Data from local to ec2 and one ec2 to another(if you are the inside source ec2)

scp -ri <key file path> <copy data file location> <Public DNS (IPv4)>:~/ 

Example:-
scp -ri practical.pem serverdata1.tar
ubuntu@ec2-xx-xxx-xxx-xxx.ap-southeast-1.compute.amazonaws.com:~/
Ankit Kumar Rajpoot
  • 5,188
  • 2
  • 38
  • 32