Search code examples
linuxamazon-web-servicessshamazon-ec2ssh-keys

authorized_keys does not present for new user


I want to setup an ssh key in a machine of Linux running under AWS in EC2 cloud.

For that firstly, I installed cygwin, then I followed the following steps :

  1. ssh-keygen -t dsa -f ~/.ssh/<key name> -C "<username of remote server>@<ip>"
  2. cat ~/.ssh/<key name>.pub | ssh <username of remote server>@<ip> "cat >> ~/.ssh/authorized_keys"

Now the 1st statement executes successfully but the 2nd statement shows

bash: /home/<username of server>/.ssh/authorized_keys: No such file exists

Prior to this, I connected to the remote machine in root mode and created the user, that I am specifying at the command 1, 2 (username)

And I saw that the file is not present in the remote server for the user I created explicitly, but it is present for the user root.


Solution

  • When you create a new linux instance, you specify a key pair that you want to use. You have a choice of creating a key pair, and downloading the public key, or uploading a private key.

    In your steps, you never reference the key pair you specified when you created the instance. So the 2nd command should be something like:

    cat ~/.ssh/<key name>.pub | ssh -i ~/.ssh/<key specified when launching instance> ec2-user@<public id> ...
    

    ec2-user may be different depending on what AMI you used to create your instance - ubuntu is the default user for ubuntu instances, for example.