Search code examples
amazon-web-servicesubuntuamazon-ec2ssh-keysssh-keygen

Could not ssh into AWS EC2 server


I am facing interesting problem. I have launched an ec2 instance which is ubuntu 14.04. I can ssh into it by providing key file like below.

ssh -i "xxxxx.pem" ubuntu@xxxxxxxxxxxx.ap-south-1.compute.amazonaws.com

But I thought of making another account in instance rather than using ubuntu(root) always which is not safe. So I have created another account on my name in server. And for more security I thought of creating private(id_rsa) and public(id_rsa.pub) key files. And put the public key in server .ssh/authorized_keys and I should be able to ssh from my new account from my local machine. Which is also worked. now I can ssh into server like below.

ssh naroju@XXXXXXXXXXXXXXX.ap-south-1.compute.amazonaws.com

Now the problem comes. Although I can ssh into to it from my new account, I cannot ssh into server from my ubuntu(root) account. It gives below error.

Permission denied (publickey).
  1. Why is it giving me this error?
  2. I wonder why did it ssh into server from my new account, does not it require private key file(.pem file of AWS) ?

Solution

  • To create a new user (eg naroju) on the instance, you should create a .ssh/authorized_keys file in the new user's home directory:

    $ sudo adduser naroju
    $ sudo su - naroju
    $ mkdir .ssh
    $ chmod 700 .ssh
    $ touch .ssh/authorized_keys
    $ chmod 600 .ssh/authorized_keys
    

    Then, edit the authorized_keys file and add the public key.

    You can then login to the new user:

    $ ssh -i naroju.pem naroju@IPADDRESS
    

    Since you have modified the public key in the ubuntu user's home directory, you will need to login as ubuntu using the private half of the keypair you selected. You can then run the above commands.

    See Amazon EC2 documentation: Managing User Accounts on Your Linux Instance