Rsync to Amazon Linux EC2 instance - Permission deniend
Asked Answered
A

2

5

Just for running a test I want to put an image file into one of my instance folders from my Desktop.

I've tried solutions provided at this same topic question: Rsync to Amazon Ec2 Instance

So I've tried:

sudo rsync  -azv --progress -e "ssh -i ~/.ssh/MyKeyPair.pem" \ ~/Desktop/luffy.jpg  \[email protected]:/home/ec2-user/myproject/mysite/mysite/media

~/.ssh/ is where MyKeyPair.pem is located. In fact, to enter via ssh I do first cd ~/.ssh and then I run the ssh -i ... command.

But I'm getting this error:

Warning: Identity file ~/.ssh/MyKeyPair.pem not accessible: No such file or directory.
Permission denied (publickey).
rsync: connection unexpectedly closed (0 bytes received so far) [sender]
rsync error: error in rsync protocol data stream (code 12) at io.c(600) [sender=3.0.6]

I've read on another Q&A page someone who got this same error reporting he solved it by just installing rsync via yum. In my case it is already installed (version 3.0.6).

I would be grateful if anyone can help!

Apollonius answered 29/9, 2014 at 18:19 Comment(0)
B
8
  1. For copying local files to EC2, the rsync command should be run on your local system, not on the EC2 instance.

  2. The tilde (~) will not be shell expanded to your home directory if it is inside quotes. Try using $HOME instead.

  3. If you are using sudo on the local side, then you probably want to use sudo on the remote (e.g., to copy over file ownerships). This can be done with the appropriate --rsync-path option.

  4. I recommend including the options -SHAX to more closely preserve the files on the target system.

  5. If "media" is supposed to be a subdirectory, then a trailing slash will help avoid some oddities if it does not currently exist.

End result:

sudo rsync  -azv -SHAX --progress -e "ssh -i $HOME/.ssh/MyKeyPair.pem" \
  --rsync-path "sudo rsync" \
  ~/Desktop/luffy.jpg \
  [email protected]:/home/ec2-user/myproject/mysite/mysite/media/

Here's an old article where I write about using rsync with EC2 instances. You can replace "ubuntu" with "ec2-user" for Amazon Linux.

http://alestic.com/2009/04/ubuntu-ec2-sudo-ssh-rsync

If this not solve your problem, please provide more details about what exact command you are running where and what exact error messages you are getting.

Boo answered 30/9, 2014 at 4:30 Comment(4)
Hi @Eric, thanks for the try! But using $HOME does not seem to work neither. I think it's because it is getting $HOME as /home/ec2-user/ --> not my "macbook home" but my "ec2 instance home". Error message remains the same except the first line, which is the one below. Warning: Identity file /home/ec2-user/.ssh/MyKeyPair.pem not accessible: No such file or directory.Apollonius
@Apollonius If $HOME is expanding to /home/ec2-user, then you are running the command on the EC2 instance. Try it on your local computer instead if you want to copy files from the local computer to the EC2 instance.Boo
That was it! :D Running the command on my local system worked (no need for sudo btw). Thank you!!Apollonius
@Apollonius If you use sudo on both sides (answer expanded to include example) then you can preserve file ownerships on the EC2 instance. For this to work well, you need to have the same users on both the local and remote systems.Boo
S
-1

Great! This worked with a slight modification. Removed sudo:

sudo rsync -azv --progress -e "ssh -i $HOME/<path_to>" \
  --rsync-path "rsync" \
  <source> \
  <target>
Sennar answered 7/3, 2016 at 5:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.