How to login in EC2 instance with non ec2-user

amazon ec2

How to login with non ec2-user account in amazon linux ami?

#useradd -m sample
#passwd sample
set password

#cat /etc/passwd
sample:x:508:1002::/home/monitor/sample:/bin/bash

# cat /home/monitor/sample/.ssh/authorized_keys
command="echo 'Please login as the ec2-user user rather than root
user.';echo;sleep10"    ssh-rsa xxxxxxxxx

# chown -R sample /home/monitor/sample/.ssh

I copied /root/.ssh/authorized_keys to the new user's .ssh directory also

At login time server says 'server refused our key' and no supported authentication method available popup.

How to associate sample with my existing key pair?

Best Answer

Instead of copying /root/.ssh/authorized_keys you should be copying /home/ec2-user/.ssh/authorized_keys to the new user's authorized_keys file. You'll also want to make sure the permissions are set correctly.

For example:

rsync -a /home/ec2-user/.ssh /home/monitor/sample/
chown -R sample:sample /home/monitor/.ssh