Add server to known_hosts

I would like to add github.com to the known_hosts file from the command line as I am creating a puppet manifest to provision a remote server.

I have tried:

"ssh-keyscan -H github.com > /home/ubuntu/.ssh/known_hosts"

But when the server tries to access github:

Failed to add the RSA host key for IP address '207.97.227.239' to the list 
of known hosts (/home/ubuntu/.ssh/known_hosts).

I've also tried:

"ssh-keyscan -H github.com,207.97.227.239 > /home/ubuntu/.ssh/known_hosts"`

But accessing github throws:

Host key verification failed.

I'm sure this is of no additional use but if I ssh my server and then ssh github and follow the steps I get the following message Permanently added 'github.com,207.97.227.239' (RSA) to the list of known hosts and then it will work perfectly.

Thanks


We handle this problem by putting the known_hosts file on the puppet server and serving the file directly out of puppet:

file{
  "/home/appuser/.ssh/known_hosts":
  owner => "appuser",
  group => "appuser",
  mode => 755,
  source => "puppet:///modules/ssh/known_hosts",
  require => File["/home/appuser"];
}

This copies the correctly formatted known_hosts file from the puppet repo, sets the user accordingly, and ensures it has correct permissions. Works well for us.


This is mentioned in the comments to the accepted answer, but I just ran into this and thought that this much cleaner solution deserves its own answer.

Puppet's core sshkey type installs keys into the server-wide /etc/ssh/ssh_known_hosts file without having to clobber the whole file. For this case:

sshkey {'github':
  name => 'github.com',
  ensure => present,
  key => '[GitHub key (just the part after ssh-rsa, starting with AAA)]',
  type => 'ssh-rsa',
}

Will install it nicely.