简体   繁体   English

如何创建 aws ec2 公私密钥对

[英]How to create aws ec2 private-public key pair

I'm following this guide of creating aws environment.我正在遵循这个创建 aws 环境的指南。 Now after I created my environment I want to ssh to the ec2.现在,在创建环境后,我想通过 ssh 连接到 ec2。

What I need is to create private-public key pair, which I don't know how.我需要的是创建私钥 - 公钥对,我不知道如何。

at the beginning of the guide, it tells:在指南的开头,它说:

Generate public key from private key
ssh-keygen -y -f ~/.ssh/pemfile/mumbai.pem

But how I create a mumbai.pem file on my host?但是我如何在我的主机上创建一个 mumbai.pem 文件呢? Is there a command to download create this pem, or I need to download it from aws?是否有下载创建此 pem 的命令,或者我需要从 aws 下载它? I'm really new with aws, I hope this is not too obvious.我对 aws 真的很陌生,我希望这不是太明显。

Just run ssh-keygen and it should prompt you for details on where to create the key.只需运行ssh-keygen ,它就会提示您提供有关在哪里创建密钥的详细信息。 Just note: If you run this command on your local machine, it will generate both the public key and the private key.请注意:如果您在本地机器上运行此命令,它将同时生成公钥和私钥。 In this case, you will need to Import Your Own Public Key to Amazon EC2 .在这种情况下,您需要将您自己的公钥导入 Amazon EC2 This method works better for terraform as you can put the text value output of your public key into the aws_key_pair resource easily.此方法更适用于 terraform,因为您可以轻松地将公钥的文本值输出放入aws_key_pair资源中。

If you create the key via the ec2 console, AWS will keep the public key in the system automatically and your browser will download the private key.如果您通过 ec2 控制台创建密钥,AWS 将自动将公钥保存在系统中,您的浏览器将下载私钥。 See Creating a Key Pair Using Amazon EC2 .请参阅使用 Amazon EC2 创建密钥对 (this second approach will save you having to upload it to ec2 keypairs). (第二种方法将使您不必将其上传到 ec2 密钥对)。 This method also works with the aws_key_pair resource, however you'll have to import the existing resource into terraform.此方法也适用于aws_key_pair资源,但您必须将现有资源导入 terraform。 It's simpler to use the first approach.使用第一种方法更简单。

If you're doing it all via terraform, check out aws_key_pair如果您通过 terraform 完成所有操作,请查看aws_key_pair

First of all, it may be too much if you're new to AWS The tutorial you're using equiped servers with Terraform, which is a 3rd party tool out of AWS首先,如果您是 AWS 的新手,那可能太多了 您正在使用配备 Terraform 的服务器的教程,这是 AWS 的第 3 方工具

You may consider a much more intuitive turtorial to create your first instance from AWS console, and AWS will help to generate a key-pair, and you will have the full control您可以考虑使用更直观的教程从 AWS 控制台创建您的第一个实例,AWS 将帮助生成密钥对,您将拥有完全控制权

In the other hand, this article is an advaced one, it's trying to automate all infra work including instance creation, network and etc. It's useful but may be too complicated to follow另一方面,这篇文章是一篇高级文章,它试图自动化所有基础工作,包括实例创建、网络等。它很有用,但可能太复杂而无法遵循

So back to your question, TF will inject the public key generated based on mumbai.pem, into the new server created in this code snippet:所以回到你的问题,TF 会将基于 mumbai.pem 生成的公钥注入到在此代码片段中创建的新服务器中:

# Define SSH key pair for our instances
resource "aws_key_pair" "default" {
  key_name = "mumbai"
  public_key = "${file("${var.key_path}")}"
}

It's not too obivious as the author is so familiar with TF and he skips the basic part不是太明显,因为作者对TF太熟悉了,他跳过了基本部分

To generate the private key run:要生成私钥,请运行:

ssh-keygen -m PEM -f key.pem

The public key公钥

ssh-keygen -y -f key.pem > key.pem.pub

If you want to import it manually via aws cli to a Key Pair called AwsKeyName type:如果您想通过 aws cli 手动将其导入名为AwsKeyName类型的密钥对:

aws ec2 import-key-pair --key-name AwsKeyName \ 
     --public-key-material $(openssl enc -base64 -A -in key.pem.pub)

To create the Pair on the console go to EC2 and in the Key Pairs click Import.要在控制台上创建对,请转到 EC2,然后在密钥对中单击导入。 Then paste the contents in the public key file (key.pem.pub)然后将内容粘贴到公钥文件(key.pem.pub)中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM