How to Add a Server to known_hosts using ssh – Dev’s Guide

Welcome Dev! In this article, we are going to show you how to add a server to your known_hosts file using ssh. This guide is intended for those who are familiar with ssh and want to add a new server to their connections list. Let’s get started!

What is known_hosts file?

The known_hosts file is a text file that contains a list of SSH server host keys. When you connect to an SSH server, the server presents its host key to your SSH client. The client then compares the key to the keys in the known_hosts file. If the key matches one of the keys in the file, the connection is allowed. If the key does not match, the client warns you that the key has changed, and you must either accept the new key or cancel the connection.

Why do we need to add a server to known_hosts?

When you connect to a new SSH server for the first time, your SSH client checks the server’s host key against the keys in the known_hosts file. If the key is not in the file, the client cannot verify the server’s identity, and you may be subject to a Man-in-the-Middle (MITM) attack.

Adding the key to the known_hosts file prevents this type of attack by allowing your SSH client to verify the server’s identity. Once the key is in the file, your SSH client will use it to verify the server’s identity every time you connect to it.

How to add a server to known_hosts using ssh

Step 1: Connect to the server

The first step is to connect to the server using ssh.

ssh user@server_ip_address

Replace “user” with the username you use to log in to the server, and “server_ip_address” with the IP address of the server you want to connect to.

Step 2: Verify the server’s fingerprint

After you connect to the server, you will be prompted to verify the server’s fingerprint.

Example:

The authenticity of host 'server_ip_address (server_ip_address)' can't be established.ECDSA key fingerprint is SHA256:/h5Sn6UfZ6UwIakgth+BQFk8f1W32jvzlzgKTtsWaFA.Are you sure you want to continue connecting (yes/no)?

This message indicates that the server’s host key is not in your known_hosts file. To add it, type “yes” and press enter.

Step 3: Add the key to known_hosts

After you type “yes” and press enter, the server’s host key will be added to your known_hosts file.

Example:

Warning: Permanently added 'server_ip_address' (ECDSA) to the list of known hosts.

That’s it! The server is now added to your known_hosts file, and you can connect to it without being prompted to verify its fingerprint in the future.

FAQ

Question
Answer
What if I accidentally accept a fake fingerprint?
If you accidentally accept a fake fingerprint, you are vulnerable to a MITM attack. To prevent this, you should always verify the server’s fingerprint before accepting it.
What if the server’s fingerprint changes?
If the server’s fingerprint changes, your SSH client will warn you that the key has changed, and you must either accept the new key or cancel the connection. You can also manually remove the old key from your known_hosts file.
Can I add multiple servers to known_hosts at once?
Yes, you can add multiple servers to your known_hosts file by connecting to each server using ssh. The server’s host key will be added to your known_hosts file automatically when you connect to it.
READ ALSO  How to Host an Ark Server and Play

We hope this guide has been helpful in showing you how to add a new server to your known_hosts file using ssh. If you have any questions, feel free to leave them in the comments section below.