Docker on Amazon Web Services
上QQ阅读APP看书,第一时间看更新

Creating an EC2 Key Pair

A key setup task that is required if you are going to running any EC2 instances in your AWS account is to establish one or more EC2 key pairs, which for Linux EC2 instances, can be used to define an SSH key pair that grants SSH access to your EC2 instances.

When you create an EC2 key pair, an SSH public/private key pair will be automatically generated, with the SSH public key being stored as a named EC2 key pair in AWS, and the corresponding SSH private key downloaded to your local client.  If you subsequently create any EC2 instances and reference a named EC2 key pair at instance creation, you will be able to automatically use the associated SSH private key to access your EC2 instances.

SSH access to Linux EC2 instances requires you to use the SSH private key associated with the configured EC2 key pair for the instance, and also requires appropriate network configuration and security groups to permit access to the EC2 instance SSH port from wherever your SSH client is located.

To create an EC2 Key Pair, first navigate to Services | EC2 in the AWS console, select Key Pairs from the Network & Security section within the left hand menu, and then click on the Create Key Pair button:

Here you have configured an EC2 key pair name admin, and after clicking on the Create button, a new EC2 key pair will be created, with the SSH private key downloaded to your computer:

At this point you need to move the SSH private key to an appropriate location on your computer, and modify the default permissions on the private key file as demonstrated below:

> mv ~/Downloads/admin.pem ~/.ssh/admin.pem
> chmod 600 ~/.ssh/admin.pem

Note that if you don't modify the permissions using the chmod command, when you attempt to use the SSH key you will be presented with the following error:

> ssh -i ~/.ssh/admin.pem 192.0.2.1
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: UNPROTECTED PRIVATE KEY FILE! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Permissions 0644 for '/Users/jmenga/.ssh/admin.pem' are too open.
It is required that your private key files are NOT accessible by others.
This private key will be ignored.
Load key "/Users/jmenga/.ssh/admin.pem": bad permissions