Search code examples
amazon-web-servicesubuntusshubuntu-16.04scp

Permission denied (publickey) error while copying to EC2


I'm trying to copy file from my local to Aws EC1 or EC2.

my detailed setup.

  1. Created two EC2 instances.

  2. Got .pem file (Private key) from AWS consle.

  3. Generated private key from .pem file (step2).

  4. Able to connect EC1 and EC2 using private key generated in step 3.

  5. Copied .pem file in EC1 and EC2 servers, by doing that am able to copy files in between EC1 and 2 aws servers.

Now I want to copy the file from my local machine to AWS EC1 and EC2 serves.

But when am trying to do below command (using public DNS):

ssh ubuntu@ec2-18-222-170-204.us-east-2.compute.amazonaws.com 

getting below error

Permission denied (publickey)

And when I do it using Private DNS:

ssh ubuntu@ip-172-31-43-162.us-east-2.compute.internal

getting below error:

ssh: Could not resolve hostname ip-172-31-43-162.us-east-2.compute.internal: Name or service not known

what should I do to fix this or make it work?

Output

OpenSSH_7.2p2 Ubuntu-4ubuntu2.8, OpenSSL 1.0.2g  1 Mar 2016
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug2: resolving "ip-172-31-43-162.us-east-2.compute.internal" port 22
ssh: Could not resolve hostname ip-172-31-43-162.us-east-2.compute.internal: Name or service not known

Solution

  • First thing, ssh ubuntu@ip-172-31-43-162.us-east-2.compute.internal this will only work within VPC, as its private IP and your local system will never able to resolve this DNS until you connect with some VPN from that VPC.

    It's not a security group issue, your instance is reachable.

    You should post debugs log for ssh ubuntu@ec2-18-222-170-204.us-east-2.compute.amazonaws.com, you can get debug log using ssh -v ubuntu@ec2-18-222-170-204.us-east-2.compute.amazonaws.com

    But I am sure it's not picking the pem file from right location. Two possible reason

    1. set chmod 400 your.pem and then try to connect

    2. specify the key in your ssh command

      ssh -i path_to_key/private.pem ubuntu@ec2-18-222-170-204.us-east-2.compute.amazonaws.com

    3. Might be possible user ubuntu not exist in that case you can try with root and AWS will return the user which can ssh to that server.

    You can also get ssh command for AWS ec2 console. -> Select Instance and click connect, copy the command and try to connect.