SnapOverflow

SnapOverflow Logo SnapOverflow Logo

SnapOverflow Navigation

  • Home
  • Server
  • Ubuntu

Mobile menu

Close
  • Home
  • System Administrators
    • Hot Questions
    • New Questions
    • Tags
  • Ubuntu
    • Hot Questions
    • New Questions
    • Tags
  • Help
Home / server / Questions / 132970
Accepted
gareth_bowles
gareth_bowles
Asked: 2010-04-16 20:15:11 +0800 CST2010-04-16 20:15:11 +0800 CST 2010-04-16 20:15:11 +0800 CST

Can I automatically add a new host to known_hosts?

  • 772

Here's my situation: I'm setting up a test harness that will, from a central client, launch a number of virtual machine instances and then execute commands on them via ssh. The virtual machines will have previously unused hostnames and IP addresses, so they won't be in the ~/.ssh/known_hosts file on the central client.

The problem I'm having is that the first ssh command run against a new virtual instance always comes up with an interactive prompt:

The authenticity of host '[hostname] ([IP address])' can't be established.
RSA key fingerprint is [key fingerprint].
Are you sure you want to continue connecting (yes/no)?

Is there a way that I can bypass this and get the new host to be already known to the client machine, maybe by using a public key that's already baked into the virtual machine image ? I'd really like to avoid having to use Expect or whatever to answer the interactive prompt if I can.

linux ssh known-hosts
  • 24 24 Answers
  • 490112 Views

24 Answers

  • Voted
  1. yar
    2011-09-28T12:51:21+08:002011-09-28T12:51:21+08:00

    IMO, the best way to do this is the following:

    ssh-keygen -R [hostname]
    ssh-keygen -R [ip_address]
    ssh-keygen -R [hostname],[ip_address]
    ssh-keyscan -H [hostname],[ip_address] >> ~/.ssh/known_hosts
    ssh-keyscan -H [ip_address] >> ~/.ssh/known_hosts
    ssh-keyscan -H [hostname] >> ~/.ssh/known_hosts
    

    That will make sure there are no duplicate entries, that you are covered for both the hostname and IP address, and will also hash the output, an extra security measure.

    • 275
  2. Best Answer
    Ignacio Vazquez-Abrams
    2010-04-16T20:34:11+08:002010-04-16T20:34:11+08:00

    Set the StrictHostKeyChecking option to no, either in the config file or via -o :

    ssh -o StrictHostKeyChecking=no [email protected]

    • 190
  3. fivef
    2014-09-26T02:03:53+08:002014-09-26T02:03:53+08:00

    For the lazy ones:

    ssh-keyscan -H <host> >> ~/.ssh/known_hosts
    

    -H hashes the hostname / IP address

    • 124
  4. ysawej
    2012-03-07T01:00:20+08:002012-03-07T01:00:20+08:00

    As mentioned, using key-scan would be the right & unobtrusive way to do it.

    ssh-keyscan -t rsa,dsa HOST 2>&1 | sort -u - ~/.ssh/known_hosts > ~/.ssh/tmp_hosts
    mv ~/.ssh/tmp_hosts ~/.ssh/known_hosts
    

    The above will do the trick to add a host, ONLY if it has not yet been added. It is also not concurrency safe; you must not execute the snippet on the same origin machine more than once at the same time, as the tmp_hosts file can get clobbered, ultimately leading to the known_hosts file becoming bloated...

    • 42
  5. Alex
    2010-04-16T21:09:40+08:002010-04-16T21:09:40+08:00

    You could use ssh-keyscan command to grab the public key and append that to your known_hosts file.

    • 19
  6. Zart
    2016-02-03T19:12:46+08:002016-02-03T19:12:46+08:00

    This is how you can incorporate ssh-keyscan into your play:

    ---
    # ansible playbook that adds ssh fingerprints to known_hosts
    - hosts: all
      connection: local
      gather_facts: no
      tasks:
      - command: /usr/bin/ssh-keyscan -T 10 {{ ansible_host }}
        register: keyscan
      - lineinfile: name=~/.ssh/known_hosts create=yes line={{ item }}
        with_items: '{{ keyscan.results | map(attribute='stdout_lines') | list }}'
    
    • 11
  7. Chris
    2016-11-24T05:51:31+08:002016-11-24T05:51:31+08:00

    this would be a complete solution, accepting host key for the first time only

    #!/usr/bin/env ansible-playbook
    ---
    - name: accept ssh fingerprint automatically for the first time
      hosts: all
      connection: local
      gather_facts: False
    
      tasks:
        - name: "check if known_hosts contains server's fingerprint"
          command: ssh-keygen -F {{ inventory_hostname }}
          register: keygen
          failed_when: keygen.stderr != ''
          changed_when: False
    
        - name: fetch remote ssh key
          command: ssh-keyscan -T5 {{ inventory_hostname }}
          register: keyscan
          failed_when: keyscan.rc != 0 or keyscan.stdout == ''
          changed_when: False
          when: keygen.rc == 1
    
        - name: add ssh-key to local known_hosts
          lineinfile:
            name: ~/.ssh/known_hosts
            create: yes
            line: "{{ item }}"
          when: keygen.rc == 1
          with_items: '{{ keyscan.stdout_lines|default([]) }}'
    
    • 8
  8. cjs
    2016-12-12T18:27:10+08:002016-12-12T18:27:10+08:00

    To do this properly, what you really want to do is collect the host public keys of the VMs as you create them and drop them into a file in known_hosts format. You can then use the -o GlobalKnownHostsFile=..., pointing to that file, to ensure that you're connecting to the host you believe you should be connecting to. How you do this depends on how you're setting up the virtual machines, however, but reading it off the virtual filesystem, if possible, or even getting the host to print the contents of /etc/ssh/ssh_host_rsa_key.pub during configuration may do the trick.

    That said, this may not be worthwhile, depending on what sort of environment you're working in and who your anticipated adversaries are. Doing a simple "store on first connect" (via a scan or simply during the first "real" connection) as described in several other answers above may be considerably easier and still provide some modicum of security. However, if you do this I strongly suggest you change the user known hosts file (-o UserKnownHostsFile=...) to a file specific for this particular test installation; this will avoid polluting your personal known hosts file with test information and make it easy to clean up the now useless public keys when you delete your VMs.

    • 8
  9. Felipe Alcacibar
    2017-04-19T13:01:35+08:002017-04-19T13:01:35+08:00

    I do a one-liner script, a bit long but useful to make this task for hosts with multiples IPs, using dig and bash

    (host=github.com; ssh-keyscan -H $host; for ip in $(dig @8.8.8.8 github.com +short); do ssh-keyscan -H $host,$ip; ssh-keyscan -H $ip; done) 2> /dev/null >> .ssh/known_hosts
    
    • 7
  10. Amadu Bah
    2017-07-03T03:41:47+08:002017-07-03T03:41:47+08:00

    The following avoid duplicate entries in ~/.ssh/known_hosts:

    if ! grep "$(ssh-keyscan github.com 2>/dev/null)" ~/.ssh/known_hosts > /dev/null; then
        ssh-keyscan github.com >> ~/.ssh/known_hosts
    fi
    
    • 6

Sidebar

Stats

  • Questions 681965
  • Answers 980273
  • Best Answers 280204
  • Users 287326
  • Popular
  • Answers
  • Marko Smith

    Ping a Specific Port

    • 18 Answers
  • Marko Smith

    How do I tell Git for Windows where to find my private RSA key?

    • 30 Answers
  • Marko Smith

    How do you restart php-fpm?

    • 18 Answers
  • Marko Smith

    What's the default superuser username/password for postgres after a new install?

    • 5 Answers
  • Marko Smith

    What port does SFTP use?

    • 6 Answers
  • Marko Smith

    Resolve host name from IP address

    • 8 Answers
  • Marko Smith

    How can I sort du -h output by size

    • 30 Answers
  • Marko Smith

    Command line to list users in a Windows Active Directory group?

    • 9 Answers
  • Marko Smith

    What is a Pem file and how does it differ from other OpenSSL Generated Key File Formats?

    • 3 Answers
  • Marko Smith

    How to determine if a bash variable is empty?

    • 15 Answers
  • Martin Hope
    Davie Ping a Specific Port 2009-10-09 01:57:50 +0800 CST
  • Martin Hope
    binaryorganic How do I tell Git for Windows where to find my private RSA key? 2010-10-26 08:45:39 +0800 CST
  • Martin Hope
    tobym What exactly do the colors in htop status bars mean? 2010-09-14 12:22:43 +0800 CST
  • Martin Hope
    MikeN In Nginx, how can I rewrite all http requests to https while maintaining sub-domain? 2009-09-22 06:04:43 +0800 CST
  • Martin Hope
    Tom Feiner How can I sort du -h output by size 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    0x89 What is the difference between double and single square brackets in bash? 2009-08-10 13:11:51 +0800 CST
  • Martin Hope
    Kyle Brandt How does IPv4 Subnetting Work? 2009-08-05 06:05:31 +0800 CST
  • Martin Hope
    Noah Goodrich What is a Pem file and how does it differ from other OpenSSL Generated Key File Formats? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent How to determine if a bash variable is empty? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus How do you find what process is holding a file open in Windows? 2009-05-01 16:47:16 +0800 CST

Related Questions

Trending Tags

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • Home
  • Questions
    • Hot Questions
    • New Questions
  • Tags
  • Help

Footer

SnapOverflow

About Us

  • About Us
  • Contact Us

Legal Stuff

  • Privacy Policy

Help

© 2022 SOF-TR. All Rights Reserve