-2

I have an EC2 instance that I can ping from my Mac but can't SCP

Using below command

scp file.txt ubuntu@<PUBLIC_IP_EC2_INSTANCE>:DESTINATION_DIR

I get error

ubuntu@<PUBLIC_IP_EC2_INSTANCE>: Permission denied (publickey).
lost connection

I followed Sunsetquest's answers in this post but doesn't help.

nad
  • 2,640
  • 11
  • 55
  • 96
  • 2
    The error is stating clearly "Permission denied (publickey)", do you have the correct public key? also remember that if it is Windows, you need to convert the key from PEM to PPK format https://linuxacademy.com/howtoguides/posts/show/topic/17385-use-putty-to-access-ec2-linux-instances-via-ssh-from-windows – rekiem87 Jun 26 '18 at 22:19
  • 2
    Can you SSH to the instance? How are you providing the Private Key? – John Rotenstein Jun 26 '18 at 22:57
  • @rekiem87 yes i have the correct private key and already did chmod 400 my_pem.pem before posting – nad Jun 27 '18 at 02:40
  • @JohnRotenstein yes I can ssh. – nad Jun 27 '18 at 02:57
  • When you SSH, are you supplying a private key (eg `ssh -i key.pem ubuntu@1.2.3.4) in the command line, or via `ssh-add`? The same key would be required by `scp`. Can you show the SSH command you use to successfully connect? – John Rotenstein Jun 27 '18 at 03:14
  • @JohnRotenstein yes I use `ssh -i key.pem ubuntu@1.2.3.4` – nad Jun 27 '18 at 13:18
  • 1
    In that case, you should also use `scp -i key.pem ubuntu@1.2.3.4` -- you have not been providing a keypair to `scp`. – John Rotenstein Jun 27 '18 at 13:22

2 Answers2

0

Had same issue and it is clearly fault of:

  • Wrong private key (or wrong public-private keypair)
  • Wrong password for private key

Before trying to scp, try ssh to server, as mentioned in one of comments - if you have set password for key, you will be asked to provide it:

ssh -i /path/to/private/key username@ip_address

If that won't work, then You need to check if public key set on EC2 under directory $HOME/.ssh/authorized_key is proper one for private key used to login. It should look like that:

ssh-rsa public_key_string username@hostname

If that still won't help (and you have set password for keypair), then make sure that password for keypair used when login is the right one.

-1

The problem is having the wrong permissions on the file.

Execute:

chmod 400 my_pem.pem

From the docs:

Your key file must not be publicly viewable for SSH to work. Use this command if needed: chmod 400 mykey.pem

Chris Pollard
  • 1,625
  • 8
  • 11