I struggled with the same permission denied error apparently due to
key_parse_private2: missing begin marker
In my situation the cause was the ssh config file of the current user (~/.ssh/config).
Using the following:
ssh -i ~/myKey.pem ec2-user@<IP address> -v 'exit'
The initial output showed:
debug1: Reading configuration data /home/ec2-user/.ssh/config
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 56: Applying options for *
debug1: Hostname has changed; re-reading configuration
debug1: Reading configuration data /home/ec2-user/.ssh/config
debug1: Reading configuration data /etc/ssh/ssh_config
... many debug lines cut here ...
debug1: Next authentication method: publickey
debug1: Trying private key: /home/ec2-user/somekey.pem
debug1: key_parse_private2: missing begin marker
debug1: read PEM private key done: type RSA
debug1: Authentications that can continue: publickey
debug1: No more authentication methods to try.
The third line above is where the problem actual was identified; however, I looked for at the debug message four lines from the bottom (above) and was misled. There isn't a problem with the key but I tested it and compared other configurations.
My user ssh config file reset the host via an unintended global setting as shown below. The first Host line should not have been a comment.
$ cat config
StrictHostKeyChecking=no
#Host myAlias
user ec2-user
Hostname bitbucket.org
# IdentityFile ~/.ssh/somekey
# IdentitiesOnly yes
Host my2ndAlias
user myOtherUser
Hostname bitbucket.org
IdentityFile ~/.ssh/my2ndKey
IdentitiesOnly yes
I hope someone else finds this helpful.
admin
. At least for 6.5 and 7.0 versions.ec2-user
, make sure you're not usingec2_user
:)$HOME/.ssh/authorized_keys
file.