I have AWS codebuild setup where want to access the EC2 ubuntu instance both are in same VPC and private subnet. Want to execute the command from codebuild to instance using SSH but getting below error, while trying to execute the command on the remote instance:
$ ssh -o 'PubkeyAuthentication no' ubuntu@X.X.X.X uname
Host key verification failed.
I also tried to create the .pem file and pasted the public key to the new pem file, changed its permission and used that. But no success.
$ssh -i "remote-instance.pem" ubuntu@X.X.X.X uname
Host key verification failed.
Is it possible to achieve the same with some options?
Best Answer
As you know the first time you
ssh
to a remote host it asks you whether you'd like to store the remote host key to~/.ssh/known_hosts
. Every time you access the remote host afterwardsssh
verifies received host key against~/.ssh/known_hosts
.However when
ssh
runs in CodeBuild or some other non-interactive setup it can't ask whether to accept the remote key (because there's no one to ask in a non-interactive session) and to be on the safe side it fails with Host key verification failedYou've got two options how to fix it:
More secure is to provide the Ubuntu server's host key to your CodeBuild and store it to a
known_hosts
file wheressh
can verify it. Make sure it's in the right path with the correct ownership and permissions.Less secure is to disable the check:
Hope that helps :)