Using ssh-keyscan (with examples)
The ssh-keyscan
command is a powerful utility that allows users to retrieve the public ssh keys of remote hosts. This information is useful for various purposes, such as verifying the authenticity of a remote host or adding the host’s public key to the known_hosts
file for future connections.
In this article, we will explore four different use cases of the ssh-keyscan
command and provide code examples for each case. We will also explain the motivation behind each use case, the meaning of the command’s arguments, and provide example outputs.
Use Case 1: Retrieve all public ssh keys of a remote host
ssh-keyscan host
Motivation: When connecting to a remote host for the first time, it is important to verify its identity. By retrieving and examining the host’s public ssh key, we can ensure that we are connecting to the intended host and not a malicious entity.
Explanation: In this use case, we simply provide the hostname or IP address of the remote host as the argument to the ssh-keyscan
command. This will retrieve all the public ssh keys associated with the host.
Example Output:
# host: example.com
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDBHbDorGNKVbDqJgWwDh0Oanmgywe... (rest of the public key)
The output will display the public ssh key in the ssh-rsa format, followed by the rest of the key. The host’s hostname or IP address will also be mentioned.
Use Case 2: Retrieve all public ssh keys of a remote host listening on a specific port
ssh-keyscan -p port host
Motivation: Remote hosts can run SSH servers on different ports. In some cases, the default SSH port (port 22) may not be used. By specifying the port using the -p
flag, we can ensure that we retrieve the correct public ssh keys from the remote host.
Explanation: In this use case, we provide the -p
flag followed by the desired port number and then the hostname or IP address of the remote host. This allows ssh-keyscan
to connect to the host using the specified port and retrieve the public ssh keys.
Example Output:
# example.com SSH-2.0-OpenSSH_7.6
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDBHbDorGNKVbDqJgWwDh0Oanmgywe... (rest of the public key)
The output will include the SSH version of the remote host, followed by the public ssh key. The host’s hostname or IP address will also be mentioned.
Use Case 3: Retrieve certain types of public ssh keys of a remote host
ssh-keyscan -t rsa,dsa,ecdsa,ed25519 host
Motivation: Different types of public ssh keys can be used for authentication purposes. By specifying the desired key types, we can narrow down the retrieved ssh keys to only those that are relevant for our use case.
Explanation: In this use case, we provide the -t
flag followed by a comma-separated list of key types (e.g., rsa, dsa, ecdsa, ed25519) and then the hostname or IP address of the remote host. This will retrieve only the specified key types from the host.
Example Output:
# host: example.com
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDBHbDorGNKVbDqJgWwDh0Oanmgywe... (rest of the rsa public key)
ssh-dsa AAAAB3NzaC1kc3MAAACBAP5D... (rest of the dsa public key)
The output will display the specified key types’ public ssh keys in the respective formats. The host’s hostname or IP address will also be mentioned.
Use Case 4: Manually update the ssh known_hosts file with the fingerprint of a given host
ssh-keyscan -H host >> ~/.ssh/known_hosts
Motivation: The known_hosts
file is used to store the public keys of remote hosts that have been previously connected to. By manually updating this file with the fingerprint of a given host, we can prevent potential man-in-the-middle attacks and establish a secure connection.
Explanation: In this use case, we provide the -H
flag followed by the hostname or IP address of the remote host. This command will retrieve the public ssh keys of the host and append them to the known_hosts
file located at ~/.ssh/known_hosts
.
Example Output:
No output is displayed when updating the known_hosts
file. However, once the command is executed successfully, the known_hosts
file will contain the fingerprint of the given host.
Conclusion
The ssh-keyscan
command is a valuable tool for retrieving the public ssh keys of remote hosts. In this article, we explored four different use cases that demonstrate the versatility of this command. Whether you need to verify the identity of a remote host, connect to a host on a non-default port, retrieve specific key types, or manually update the known_hosts
file, ssh-keyscan
provides a reliable and straightforward solution.
By utilizing the examples provided in this article, you can now confidently leverage the ssh-keyscan
command to enhance the security and reliability of your SSH connections.