[英]Add public key to known_hosts file
I am trying to copy a public key from Server A over to the known_hosts file in Server B. They are both linux servers.我正在尝试将公钥从服务器 A 复制到服务器 B 中的 known_hosts 文件。它们都是 linux 服务器。 Initially I thought about opening the public key file and copying its contents over to the known_hosts file but I suspect that is not the correct method.最初我想打开公钥文件并将其内容复制到 known_hosts 文件,但我怀疑这不是正确的方法。 Does anyone know what the right way to do so is?有谁知道这样做的正确方法是什么?
My public key is in the format ssh-rsa AADGD...
我的公钥格式为ssh-rsa AADGD...
Can someone help?有人可以帮忙吗?
Thanks!谢谢!
I answered almost similar answer on SuperUser few days ago. 我几天前在SuperUser上回答了几乎相似的答案。 The important parts: 重要的部分:
ssh-keyscan
which can create the format for you 有ssh-keyscan
可以为您创建格式 Otherwise just prefix your key with server IP address (you can add also hostname, after comma), remove the comment from end of the line and you are fine. 否则只需在密钥前加上服务器IP地址(您可以在逗号后添加主机名),从行尾删除注释即可。 Format then look like this: 格式然后看起来像这样:
11.22.33.44 ssh-rsa AADGD...
And one more note, if you use HashKnownHosts yes
(Debian and Ubuntu does), you need to re-hash your known_hosts
such as: 还有一点需要注意,如果你使用HashKnownHosts yes
(Debian和Ubuntu),你需要重新哈希你的known_hosts
例如:
ssh-keygen -Hf ~/.ssh/known_hosts
Assuming that you have a file called publickey.pub
then please do this:假设您有一个名为publickey.pub
的文件,请执行以下操作:
scp
the public key.pub
to your desired servers将public key.pub
scp
到你想要的服务器sudo /bin/bash -c "cat /$USER_PATH/public_key.pub >> $USER_PATH/.ssh/authorized_keys"
known_hosts
= run对于known_hosts
= 运行sudo /bin/bash -c "cat /$USER_PATH/public_key.pub >> $USER_PATH/.ssh/known_hosts"
ssh
测试与ssh
的连接Note: make sure you check your public's key format.注意:确保检查您的公钥格式。 The ones I have seen up to this point begin with the encryption algorithm eg ssh-RSA
到目前为止,我所看到的都是从加密算法开始的,例如ssh-RSA
This is how I did it. 这就是我做到的。
*ssh-keyscan -t rsa full-server-name * ssh-keyscan -t rsa full-server-name
Having just bumped into this problem, here's how I approached it: 刚刚碰到这个问题,这就是我接近它的方式:
Over time, copying the files mechanically via 随着时间的推移,通过机械方式复制文件
ssh-keyscan server-name >> ~/.ssh/known_hosts
gave me duplicate entries in .ssh/known_hosts. 在.ssh / known_hosts中给了我重复的条目。
Other manual methods required me to create the .ssh
directory didn't already exist, etc. 其他手动方法要求我创建.ssh
目录尚不存在等。
I decided to just let ssh
handle it: 我决定让ssh
处理它:
ssh -o StrictHostKeyChecking=no server-name ls
The -o StrictHostKeyChecking=no
option automatically answers 'yes' to the -o StrictHostKeyChecking=no
选项自动回答“是”
The authenticity of host 'server-name (12.345.678.900)' can't be established.
RSA key fingerprint is XXXXXXX.
Are you sure you want to continue connecting (yes/no)?
message (insert here all the security caveats about connecting randomly to machines you don't know). 消息(在此插入关于随机连接到您不知道的机器的所有安全警告)。
The ls
command is just a fluff command that will execute and force SSH to disconnect when done. ls
命令只是一个fluff命令,它将执行并强制SSH在完成时断开连接。 You can change it to whatever fluff command you like. 您可以将其更改为您喜欢的任何绒毛命令。
ssh will take care of creating the .ssh
dir (if necessary), adding only one copy of the key, etc. ssh将负责创建.ssh
目录(如果需要),只添加一个密钥副本等。
Platform: macOS 10.14 平台:macOS 10.14
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.