All the Important Information You Need to Know About Linux’s known_hosts file
A known_hosts file is a crucial component of the SSH protocol and can be found in the.ssh directory. Study up on it more.
A known_hosts file can be found in your home directory’s.ssh folder along with other files.
|
|
Your private SSH key is id_ed25519 in this case.The public SSH key is pub. In SSH, profiles are created in the config file to facilitate easy connections to different servers. I specifically created it; it is not a common file.
This article focuses on known_hosts, the final file. A crucial component of SSH configuration files for clients is the ~/.ssh/known_hosts file.
Permit me to elaborate on it.
What does SSH’s known hosts file mean?
The public keys of the hosts that a user has accessed are kept in the known_hosts file. This is a crucial file that saves the user’s identification to your local machine, ensuring that they are connecting to a real server. Additionally, it aids in preventing
man-in-the-middle
attacks.
You are asked if you wish to add the remote hosts to the known_hosts file whenever you establish an SSH connection to a new remote server.
|
|
In essence, the message asked if you would like to add the remote system’s details to your own.
If you select “yes,” your system saves the server’s identity.
Steer clear of man-in-the-middle attacks
Assume you have added a server to the known_hosts file and you connect to it on a regular basis.
Your system will detect any changes to the remote server’s public key if they occur because of the data kept in the known_hosts file. You will receive immediate notification of this change:
|
|
Before accepting this new key in such a situation, you can get in touch with the administrator of the remote server. You can make sure that the host or remote server hasn’t been compromised in this way.
The host or server’s key may occasionally be purposefully changed by the administrator or as a result of a server reinstallation.
For whatever reason this change occurred, in order to reconnect to the remote server, you must first remove the previous key from the known_hosts file. The client host will generate a new host key for this server when you connect to it again.
Handling Several Authorized Users
As previously indicated, the public key of the remote server is appended to the client host’s known_hosts file after a successful connection.
There are situations when you want to authenticate a server to several users simultaneously without asking them to verify the server key. For instance, you don’t want the client host to request server key verification if you are using Ansible or another configuration management tool.
Therefore, there are three ways to get around the SSH interactive prompt if you have multiple users:
- Manually adding the server’s public key to each user’s known_hosts file.
- When connecting to the server via SSH, use the command-line option -o StrictHostKeyChecking=no with each client (not recommended)
- Create a master or primary ssh_known_hosts file, register all of your hosts there, and then distribute this file to all of your client hosts. Additionally, the ssh-keyscan command can be used to make this function:
|
|
The StrictHostKeyChecking=no option can be used as shown in the screenshot below:
|
|
|
|
Compared to the other two methods, the first method of managing multiple users for server authentication is the most laborious.
Accessing the known_hosts file to obtain remote system information
This task is anything but simple and easy.
Nearly all Linux systems have the SSH configuration file’s HashKnownHosts parameter set to Yes. It’s a safety measure.
This indicates that the known_hosts file’s information is hashed. Although random numbers are visible to you, you are unable to interpret them.
|
|
If you are aware of the system’s hostname or IP address, you can obtain the relevant entries from the known_hosts.
|
|
However, it isn’t feasible to have a single command that could provide a clear text list of all the servers and their details.
The known_hosts can be deciphered using specially designed tools and scripts, but that is outside the purview of the average user like you and me.
Take out a piece of the known_hosts
If you are aware of the hostname or IP address of the remote system, you can delete a particular entry from the known_hosts file.
|
|
This is far more efficient than using the rm command to manually remove the entries associated with a server after finding them.
In summary
Understanding the different SSH configuration files gives you a greater grasp of system security. One essential component of these files is “known_hosts.”