How to Add an SSH Fingerprint to Your known_hosts File in Linux

How to Add an Ssh Fingerprint to Your Known_hosts File in Linux

How to Add an SSH Fingerprint to Your known_hosts File in Linux

Home ยป News ยป How to Add an SSH Fingerprint to Your known_hosts File in Linux
Table of Contents

Letโ€™s say youโ€™ve written a bash script that scans your whole Linux servers in your information middle for uptime. Such a file may comprise the next contents:

ssh $1 "uptime"

Whenever you run your script, it might get foiled by a difficulty the place it’s stopped by a server that has but to have its SSH key fingerprint added to the known_hosts file. When this occurs, your script is rendered ineffective.

SEE: The best way to View Your SSH Keys in Linux, macOS, and Home windows (roosho)

SSH key fingerprint

What’s an SSH key fingerprint? Easy: The important thingโ€™s fingerprint is verified while you attempt to log in to a distant laptop utilizing SSH. Whenever you log into an SSH server for the primary time, youโ€™ll see one thing like that proven beneath.

Logging into an Ssh Server for the First Time.
logging into an ssh server for the primary time picture jack wallen

If you happen to donโ€™t settle for the fingerprint, the connection will likely be instantly damaged. So what occurs while youโ€™re working with a bash script that can’t settle for enter with the intention to okay the addition of the distant SSH fingerprint?

Thankfully, the builders of SSH considered this and added a command that lets you simply add SSH fingerprints to the known_hosts file.

SEE: The best way to Create and Copy SSH Keys with 2 Easy Instructions (roosho)

Including the fingerprint

Iโ€™ll show including the fingerprint from a distant server to a neighborhood machine. Letโ€™s say the distant server is at 192.168.1.162. So as to add that fingerprint, the command can be:

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

The command will run and add the distant SSH fingerprint to the native machine with out your enter, as proven beneath.

How to Run and Add the Remote Ssh Fingerprint to the Local Machine.
the best way to run and add the distant ssh fingerprint to the native machine picture jack wallen

So an addition to the bash script may appear to be:

ssh-keyscan $1 >> ~/.ssh/known_hosts

The above addition would take the argument from the command (say, for instance, ./script 192.168.1.118) and add the fingerprint to ~/.ssh/known_hosts earlier than it then strikes to the subsequent line โ€” thereby avoiding the lacking SSH fingerprint concern. In fact the above would solely work correctly when you’ve got ssh key authentication setup. In any other case, youโ€™d should enter the distant machineโ€™s password.

SEE: The best way to Mount Distant Directories with SSH (roosho)

The easy issues

Typically, itโ€™s the easy issues that journey up our bash scripts. If that key fingerprint concern has been inflicting you complications along with your scripts, now you can keep away from the problem.

author avatar
roosho Senior Engineer (Technical Services)
I am Rakib Raihan RooSho, Jack of all IT Trades. You got it right. Good for nothing. I try a lot of things and fail more than that. That's how I learn. Whenever I succeed, I note that in my cookbook. Eventually, that became my blog.ย 
share this article.

ADVERTISEMENT

ADVERTISEMENT

Enjoying my articles?

Sign up to get new content delivered straight to your inbox.

Please enable JavaScript in your browser to complete this form.
Name