简体   繁体   English

将公钥添加到 known_hosts 文件

[英]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: 重要的部分:

  • The format differs 格式不同
  • There are different host keys (types) on each server (make sure you paste the one that is actually used) 每台服务器上都有不同的主机密钥(类型)(确保粘贴实际使用的主机密钥)
  • There is 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的文件,请执行以下操作:

  1. scp the public key.pub to your desired serverspublic key.pub scp到你想要的服务器
  2. Run for AWS EC2 ubuntu instancesAWS EC2 ubuntu 个实例运行
sudo /bin/bash -c "cat /$USER_PATH/public_key.pub  >> $USER_PATH/.ssh/authorized_keys"
  1. for known_hosts = run对于known_hosts = 运行
sudo /bin/bash -c "cat /$USER_PATH/public_key.pub  >> $USER_PATH/.ssh/known_hosts"
  1. Test a connection with 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. 这就是我做到的。

  1. Generate a key on host server. 在主机服务器上生成密钥。 Using the below command. 使用以下命令。

*ssh-keyscan -t rsa full-server-name * ssh-keyscan -t rsa full-server-name

命令行和输出

  1. Now copy the highlighted section(in the picture) and append this key to the 'known_host' file on source server. 现在复制突出显示的部分(在图片中)并将此键附加到源服务器上的'known_host'文件。 Of course, the location for this file could be different for different environments. 当然,对于不同的环境,此文件的位置可能不同。

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.

相关问题 如何将主机列表安全地添加到known_hosts文件 - How to Securely Add a List of Hosts to known_hosts File Linux 上的 SSH:禁用本地子网上主机的主机密钥检查 (known_hosts) - SSH on Linux: Disabling host key checking for hosts on local subnet (known_hosts) 当我想将密钥传播到其他环境时,SSHknown_hosts文件添加了不同的ssh-rsa代码-原因是无法连接密码? - SSH known_hosts file adds different ssh-rsa code when I want to propagate key to other environment - cause of being unable to connect w/o password? ssh远程命令,不带known_hosts条目 - ssh remote commands without known_hosts entry 使用 Elastic Beanstalk EB known_hosts 在 AWS 上使用 pysftp 和 paramiko 进行 SFTP - SFTP with pysftp and paramiko on AWS using Elastic Beanstalk EB known_hosts 命令 '/bin/sh -c ssh-keyscan -t rsa 172.168.85.74 >> /root/.ssh/known_hosts' 返回一个非零代码:1 - The command '/bin/sh -c ssh-keyscan -t rsa 172.168.85.74 >> /root/.ssh/known_hosts' returned a non-zero code: 1 IP 地址“XX.XX.XX.XX”的 RSA 主机密钥不在已知主机列表中 - RSA host key for IP address 'XX.XX.XX.XX' not in list of known hosts 如何从.key文件中提取公钥和私钥? - How to extract public key and private key from .key file? 如何使用bash为每个命名的hosts文件添加一个字符串 - how to use bash to add a string to each named hosts file 创建脚本以安全地将条目添加到 /etc/hosts 文件 - Create a script to safely add entries to the /etc/hosts file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM