大网数据广告活动
服务器运维问答

What is the good practice for adding known keys/fingerprints to known_hosts

· 52CE 我爱测

There are plenty of answers to questions similar to this one but I can't seem to find one with a clear answer. They all either grab the public key at least once, ignore the key altogether, or write directly to the known_hosts fil…

问题原文

原帖链接 · D.Mill · CC BY-SA 3.0

There are plenty of answers to questions similar to this one but I can't seem to find one with a clear answer. They all either grab the public key at least once, ignore the key altogether, or write directly to the known_hosts file (implies no hash)

I have the server's fingerprint and/or public key. I would like a shell command to add it to the client's known_hosts file. This has to use whatever is configured (hash/no-hash)

Also, the script will do a git pull from this server so I don't know if providing port information at this point is relevant or not. Please tell me if it is.

Thanks. And feel free to point me in the right direction if this has already been answered and I missed it somehow.

PS - Extra info: One of the reasons I ask about ports is because I've done the following to no success (I'm aware this is getting the key from the host which I would rather not do):

ssh-keygen -R my.awesome.host # hostname
ssh-keygen -R 1.2.3.4         # IP
ssh-keygen -R my.awesome.host,1.2.3.4
ssh-keyscan -H my.awesome.host,1.2.3.4 >> ~/.ssh/known_hosts
ssh-keyscan -H 1.2.3.4 >> ~/.ssh/known_hosts
ssh-keyscan -H my.awesome.host >> ~/.ssh/known_hosts

But when I git clone (via ssh) I'm met with a resounding:

The authenticity of host '[my.awesome.host]:7999 ([1.2.3.4]:7999)' can't be established.
RSA key fingerprint is fi:ger:pr:in:ti:nf:or:ma:ti:on
Are you sure you want to continue connecting (yes/no)?

Yet ssh [email omitted] does not prompt me about the fingerprint.

已采纳解答

原帖链接 · Jakuje · CC BY-SA 3.0

Having a public key, you can simply write the key into the known_hosts file and possibly re-hash, if you need to:

HOSTNAME=my.awesome.host
PORT=7999
PUBKEY="ssh-rsa AAAAB3NzaC1yc2EAAAAD...E"
KNOWN_HOSTS="~/.ssh/known_hosts"
echo "[$HOSTNAME]:$PORT $PUBKEY" >> $KNOWN_HOSTS
# re-hash, if needed:
ssh -G -p $PORT $HOSTNAME | grep "hashknownhosts yes" && \
  ssh-keygen -H -f $KNOWN_HOSTS

The -G switch for ssh is fairly new. If it does not work, you will have to determine whether to hash the known hosts or not in different way (or do it regardless the conditions).

Hashing file with already hashed hosts does not touche these lines.

I didn't try the above script, but you should be able to get the point from that (and fix typos, if there are some).

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2016-08-18)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。