-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 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