28

I am trying to transfer a file to an ec2 instance. I followed the Amazon's documentation, this is what my command looked like:

scp -i [the key's location] Documents/[the file's location] ec2-user@[public dns]:[home/[destination]]

where I replaced all the variables with the proper things, I am sure it's the correct key and it has permission 400. When I call the command, it tells me the RSA key fingerprint, asks me if I want to continue connecting. I type yes and it replies with

Permission denied (publickey,gssapi-with-mic)
lost connection

I have looked at many of the other similar questions on stack overflow and can't find a correct way to do it.

Also ssh traffic is enabled on port 22.

Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
Amre
  • 1,630
  • 8
  • 29
  • 41
  • 1
    I found out what I was doing wrong, at first, i had the name of the user (in this case seqware) rather than ec2-user, which gave me the same thing, but when I replaced that with root@publicdns, it worked – Amre Jul 03 '13 at 13:32

6 Answers6

22

The example amazon provided is correct. It sounds like a folder permissions issue. If you created the folder you are trying to copy to with another user or another user created it, chances are you don't have permissions to copy to it or edit it.

If you have sudo abilities, you can try opening access for yourself. Though not recommended to be left this way, you could try this command:

sudo chmod 777 /folderlocation

That gives complete read/write/executable permissions to anyone (hence why you shouldn't leave it at 777) but it will give you the chance to test your scp command to rule out permissions.

Afterwards if you aren't familiar with permissions, I suggest you read up on it. this is an example: http://www.tuxfiles.org/linuxhelp/filepermissions.html It is generally suggested you lock down the folder as much as possible depending on the type of information held within.

If that was not the cause some other things you might want to check:

  • are you in the directory of your key when executing the 'scp -i keyname' command?
  • do you have permissions to use the folder you are transferring from?

Best of luck.

brettwmc
  • 470
  • 3
  • 10
12

The problem may be the user name. I copied a file to my Amazon instance and first tried to use the command:

scp -r -i ../.ssh/Amazon_server_key_pair.pem ./empty.test ec2-user@ec2-xx-yy-zz-tt.compute-1.amazonaws.com:~

and got the error:Permission denied (publickey).

I then realized that my instance is an Ubuntu environment and the user user is then "ubuntu" the correct command that worked for me is then:

scp -r -i ../.ssh/Amazon_server_key_pair.pem ./empty.test ubuntu@ec2-xx-yy-zz-tt.us-west-2.compute.amazonaws.com:~

The file "empty.test" is a text file containing the text "testing ...". Replace the address of your virtual server with the correct address to your instance's Public DNS. I have replaced the ip to my instance with xx.yy.zz.tt.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
Lars Silen
  • 121
  • 1
  • 2
7

I have to use ubuntu@ instead of ec2-user@ because when i ssh i was seeing ubuntu@ in my terminal, try changing to the name you see at your terminal

Also you have to set permission for pem file in your computer

chmod 400 /path/my-key-pair.pem

The below code will copy file from your computer to Ec2 instance.

scp -i ~/location_of_your_ec2_key_pair.pem ~/location_of_transfer_file/sample.txt ubuntu@ec2_your_ec2_instance.compute.amazonaws.com:~/folder_to_which_it_needs_to_be_copied

The below code will copy file from Ec2 instance to your computer

scp -i ~/location_of_your_ec2_key_pair.pem   ubuntu@ec2_your_ec2_instance.compute.amazonaws.com:~/location_of_transfer_file/sample.txt ~/folder_to_which_it_needs_to_be_copied
Shinto Joseph
  • 2,809
  • 27
  • 25
1

I was facing the same problem. Hope this will work for you.

scp -rp -i yourfile.pem ~/local_directory username@instance_url:directory

Permission should also be correct to make this work.

iCPSoni
  • 101
  • 6
0

Might be ones uses wrong username. Happened to me, was the same error msg -> Permission denied (publickey,gssapi-keyex,gssapi-with-mic). lost connection

Aleks Tkachenko
  • 704
  • 7
  • 6
0

scp -i [the key's location] Documents/[the file's location] ec2-user@[public dns]:[home/[destination]]/** you can add ** at the last and try changing the permissions of the destination directory

This worked for me

pndey
  • 71
  • 4