Add a new host to known_hosts

When you ssh into a machine, you get prompted for a confirmation of the key fingerprint. This happens only the first time a machine is sshed into. After that, there will be an entry added in the ~/.ssh/known_hosts file which will be used.


There are two simple ways (both leaves you open to man in the middle attacks) this prompt can be bypassed.

Pass extra parameters to ssh command

By setting the StrictHostKeyChecking to no via -o, we can bypass the prompt by not checking ~/.ssh/known_hosts file.

ssh -o StrictHostKeyChecking=no username@hostname

Use ssh-keyscan command to add entries in ~/.ssh/known_hosts file

ssh-keyscan command can be used to produce entries similar to what ssh does, which can be piped into ~/.ssh/known_hosts file. After doing this, ssh command can be used without any modifications or prompt.

ssh-keyscan -H HostnameOrIP >> ~/.ssh/known_hosts