7

I can SSH into the EC2 instance:

ssh -i "my_key.pem" ec2-user@my-public-ip

However, scp doesn't work:

scp -r –i "my_key.pem" ./my_file ec2-user@my-public-ip:/home/ec2-user/my_file

Permission denied (publickey).
lost connection

I've also tried using public instance DNS, but nothing changes.

Any idea why is this happening and how to solve it?

Kenster
  • 23,465
  • 21
  • 80
  • 106
borjagvo
  • 1,802
  • 2
  • 20
  • 35

4 Answers4

4

The only way for this to happen is the private key mykey.pem is not found in the current directory. It is possible you tried ssh from a directory different than scp.

Try the following with full path to your key:

scp -r –i /path/to/my_key.pem ./my_file ec2-user@my-public-ip:/home/ec2-user/my_file

If it fails, post the output with -v option. It will tell you exactly where the problem is

scp -v -r –i /path/to/my_key.pem ./my_file ec2-user@my-public-ip:/home/ec2-user/my_file
helloV
  • 50,176
  • 7
  • 137
  • 145
  • It turns out that placing scp options in different order than the one stated by @helloV won't make it work. Thanks!! – borjagvo Jan 12 '17 at 08:55
2

I am bit late but this might be help full to someone.
Do not use the /home/ec2-user. Rather directly use the file name or folder name E.g. the following command will put your my_file at the home folder (i.e. /home/ec2-user)

scp -r –i "my_key.pem" ./my_file ec2-user@my-public-ip:my_file

Or Say if you have a folder at /home/ect-user/my_data
Then use the following command to copy your file to the folder

scp -r –i "my_key.pem" ./my_file ec2-user@my-public-ip:my_data
er.bhargav.vyas
  • 301
  • 2
  • 6
0

Stupidly late addendum:

To avoid specifying the private key every time, just add to the .ssh/config file (create it if not already there) the following (without comments):

Host testserver                  // a memorable alias
Hostname 12.34.56.67             // your server ip
User ec2-user                    // user to connect
IdentityFile /path/to/key.pem    // path to the private key
PasswordAuthentication no

Then a simple ssh testserver should work from anywhere (and consequently your scp too).

I use it to connect with Vim via scp using:

vim scp://testserver/relative/file/path

or

vim scp://testserver//absolute/file/path

and

vim scp://testserver/relative/dir/path/ (note the trailing slash)

to respectively edit files and browse folders directly from local (thus using my precious .vimrc <3 configuration).

Solution found here

Hope this helps! :)

Bruno Belotti
  • 2,414
  • 1
  • 31
  • 28
0

I was facing this issue today and found solution for me (not elegant but one which worked). - this solution is good if you want to download something once and rollback all settings afterwards.

Solution: When I specified -v option while using scp I noticed the certificate is being denied for some reason so I went to /etc/ssh/sshd_config and set PasswordAuthentication yes. Then I used systemctl restart sshd.

After this procedure I went to my local machine and used:

scp -v -r myname@VPC:/home/{user}/filename.txt path/on/local/machine 

provided PWD and file transmission has been successful.

Hope this helps to someone :)

battle44
  • 55
  • 9