SSH-KEYSCAN(1)

๐Ÿ’ป SSH-KEYSCAN(1) โ€” the gather-SSH-keys baddie

okay so bestie, welcome to the ssh-keyscan(1) page from the General Commands Manual โ€” yes it's a real section, yes it's iconic, and yes we're gonna read it together. grab a snack ๐Ÿ“


โœจ NAME โœจ

ssh-keyscan โ€” literally just gather SSH public keys from servers. that's it. that's the whole job. ๐Ÿ’…


๐Ÿฆ‹ SYNOPSIS ๐Ÿฆ‹

ssh-keyscan [-46cDHqv] [-f file] [-O option] [-p port] [-T timeout]
            [-t type] [host | addrlist namelist]

๐ŸŒธ DESCRIPTION ๐ŸŒธ

okay so ssh-keyscan is a utility for gathering the public SSH host keys of a number of hosts. it was designed to aid in building and verifying ssh_known_hosts files โ€” the format of that is documented over in sshd(8), go look, iykyk. and it provides a minimal interface that's suitable for use by shell and perl scripts. script girlies, this one's for you ๐Ÿซถ

ssh-keyscan uses non-blocking socket I/O to contact as many hosts as possible in parallel, so it is very efficient. the keys from a domain of 1,000 hosts can be collected in tens of seconds โ€” even when some of those hosts are down or do not run sshd(8). not me being fast AND reliable ๐Ÿ˜ญโœจ

for scanning, one does not need login access to the machines that are being scanned, nor does the scanning process involve any encryption. no login, no crypto, no stress, no cap ๐Ÿ’€

hosts to be scanned may be specified by hostname, address, or by CIDR network range (e.g. 192.168.16/28). if a network range is specified, then all addresses in that range will be scanned. every single one. main character sweep ๐Ÿ‘‘

the options are as follows:


๐ŸŽ€ OPTIONS ๐ŸŽ€

  • -4 โ€” force ssh-keyscan to use IPv4 addresses only.
  • -6 โ€” force ssh-keyscan to use IPv6 addresses only.
  • -c โ€” request certificates from target hosts instead of plain keys. fancy โœจ
  • -D โ€” print keys found as SSHFP DNS records. the default is to print keys in a format usable as an ssh(1) known_hosts file.
  • -f file โ€” read hosts or "addrlist namelist" pairs from file, one per line. details below because there's lore ๐Ÿ‘‡
  • -H โ€” hash all hostnames and addresses in the output. hashed names may be used normally by ssh(1) and sshd(8), but they do not reveal identifying information should the file's contents be disclosed. privacy queen ๐Ÿ”’
  • -O option โ€” specify a key/value option. at present, only a single option is supported:
    • hashalg=algorithm โ€” selects a hash algorithm to use when printing SSHFP records using the -D flag. valid algorithms are "sha1" and "sha256". the default is to print both.
  • -p port โ€” connect to port on the remote host.
  • -q โ€” quiet mode: do not print server host name and banners in comments. shhh ๐Ÿคซ
  • -T timeout โ€” set the timeout for connection attempts. if timeout seconds have elapsed since a connection was initiated to a host or since the last time anything was read from that host, the connection is closed and the host in question considered unavailable. the default is 5 seconds.
  • -t type โ€” specify the type of the key to fetch from the scanned hosts. the possible values are "ecdsa", "ed25519", "ecdsa-sk", "ed25519-sk", or "rsa". multiple values may be specified by separating them with commas. the default is to fetch all the above key types.
  • -v โ€” verbose mode: print debugging messages about progress. talk to me bestie ๐Ÿ—ฃ๏ธ

๐Ÿงš -f file โ€” the deep dive ๐Ÿงš

read hosts or "addrlist namelist" pairs from file, one per line. if '-' is supplied instead of a filename, ssh-keyscan will read from the standard input. stdin stans rise ๐Ÿ™ˆ

names read from a file must start with an address, hostname or CIDR network range to be scanned. addresses and hostnames may optionally be followed by comma-separated name or address aliases that will be copied to the output. for example:

192.168.11.0/24
10.20.1.1
happy.example.org
10.0.0.1,sad.example.org

like, the file format really said "here's my range, here's my host, here's happy.example.org living her best life, and here's 10.0.0.1 with her alias sad.example.org" ๐Ÿ˜ญ iconic lineup


โš ๏ธ okay this part is IMPORTANT, listen up. if an ssh_known_hosts file is constructed using ssh-keyscan without verifying the keys, users will be vulnerable to man in the middle attacks. like, genuinely. not a vibe. ๐Ÿ’€ on the other hand, if the security model allows such a risk, ssh-keyscan can help in the detection of tampered keyfiles or man in the middle attacks which have begun after the ssh_known_hosts file was created. so it's giving "useful but read the warning" energy.

๐Ÿ—‚๏ธ FILES ๐Ÿ—‚๏ธ

  • /etc/ssh/ssh_known_hosts

๐ŸŒˆ EXAMPLES ๐ŸŒˆ

print the RSA host key for machine hostname:

$ ssh-keyscan -t rsa hostname

search a network range, printing all supported key types:

$ ssh-keyscan 192.168.0.64/25

find all hosts from the file ssh_hosts which have new or different keys from those in the sorted file ssh_known_hosts:

$ ssh-keyscan -t rsa,ecdsa,ed25519 -f ssh_hosts | \
        sort -u - ssh_known_hosts | diff ssh_known_hosts -

not the pipe and the diff and the trailing -... she ate ๐Ÿ”ฅ


๐Ÿ‘€ SEE ALSO ๐Ÿ‘€


๐Ÿ‘‘ AUTHORS ๐Ÿ‘‘

David Mazieres <dm@lcs.mit.edu> wrote the initial version, and Wayne Davison <wayned@users.sourceforge.net> added support for protocol version 2. legends, both of them ๐Ÿซถ


and that's the whole page, bestie โ€” no notes, periodt. ๐Ÿ’–

this lil rewrite is brought to you by the Debian edition, dated October 4, 2025, and it's still filed under SSH-KEYSCAN(1) in the General Commands Manual. ๐ŸŽ€

๐Ÿ“ฆ where this page comes from