2

I created an ec2 instance with an Ubuntu 16.04 AMI. I created a sudo user for this AMI with:

sudo adduser myuser
sudo usermod -aG sudo myuser

Then I attempted to give the user ssh access with an rsa keypair that I already had. I created an .ssh directory for the new user:

mkdir ~/.ssh
chmod 700 ~/.ssh
vim ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys

I copied and pasted my public key into 'authorized_keys'. Then I exited ec2 and tested my connection with:

cd ~/.ssh
ssh -i "mypem.pem" myuser@ec2-xx-xx-xxx-xxx.us-west-1.compute.amazonaws.com

I got an error that read 'Access denied (public key)'

So I scrapped the user and created another user. This time a created the 'authorized_keys' file with nano instead of Vim and it worked.

Anyone experience this?

3 Answers3

1

If something like this happens, I would recommend tho check fileencryption , fileformat and bomb in ViM. Compare both files, the one you edited with nano and the one you edited with vim. Open them with vim and check this:

:set bomb? This is the byte order mark. Usually set when you edit a UTF-8 file with notepad.exe on windows. I often had problems with that.

:set ff? Fileformat dos or unix. dos have different line endings than unix.

:set fenc? The fileencoding: perhaps one editor changes to latin1 instead of UTF-8.

chloesoe
  • 335
  • 1
  • 17
1

Probably you pasted it wrong.

  • Does it start with ssh-rsa or with ssh-ed25519?
  • Does it have a space before (it should NOT)
  • Is upper/lower case preserved?
  • is there a space before any comment at the end of the line?

before pasting anything in vim, you should use :set paste, as it switches off any possible active autocompletion/autoindent/... macros.

Another thing is, that you wrote mykey.pem, which is the usual extension for openssl files, not ssh-keys. Are you sure, you used the id_rsa.pub / id_ed25519.pub as ssh-pubkey?

allo
  • 1,524
  • 1
  • 19
  • 35
0

myuser must has access to .ssh and keyfile. Check who owns them.