title: How to select the private key used by a remote machine to create the known_hosts entry given to my local server
body: <p>Passwordless ssh has two components, user authentication and server authentication. My user authentication is fine. I have created a public private key pair and place my public key in the authorized_keys file. My question is about the public key my local machine obtains from the remote machine which is used to authenticate the remote machine I'm connecting to:</p>
  
  <blockquote>
    <p>How do I select the private key used by the remote server that goes into my local server's known_hosts?</p>
  </blockquote>
  
  <p>I am constantly creating and deleting remove VMs on a cloud provider on demand to save money. Unfortunately, the new VM which replaces the delete one has generated a new private-public key pair used in the known_hosts</p>
  
  <p>I do not want to have to manually type <code>ssh-keygen -R &lt;host&gt;</code> for each host. I thought the easiest would be if I have a hardcoded private key on the remote server already.</p>
  
  <p>Please note this is related to previous public-private key questions like <a href="https://stackoverflow.com/questions/20840012/ssh-remote-host-identification-has-changed">ssh remote host identification has changed</a> , but is not duplicated! I know that you can manually fix the issue with <code>ssh-keygen -R &lt;host&gt;</code>. I am looking for a more automatic approach.</p>
  
  <p>Diagram:</p>
  
  <pre><code>--------------------                          -----------------------
  | My machine       |                          |  Remote Machine     |
  | - - - - - - - - -|                          | - - - - - - - - - - |
  | Host Public Key  |&lt;---host-authentication---| ** Host Private Key |
  | (known_hosts)    |                          |                     |
  | - - - - - - - - -|                          | - - - - - - - - - - |
  | User Private Key |----user-authentication--&gt;| User Public Key     |
  |                  |                          | (authorized_hosts)  |
  --------------------                          -----------------------
  </code></pre>
  
  <p>** : How do I change this part?</p>