AWS CLI:密钥不是有效的OpenSSH公钥格式

Ina*_*mus 5 amazon-ec2 amazon-web-services aws-cli

如何解决呢?

# I used this command to create the key with a password
$ ssh-keygen -b 2048 -t rsa -C "awsfrankfurt" -f ~/.ssh/awsfrankfurt

# Then when I try to import it into AWS EC2, the error appears:
$ aws --region eu-central-1 ec2 import-key-pair \
    --key-name "awsfrankfurt" \
    --public-key-material ~/.ssh/awsfrankfurt

An error occurred (InvalidKey.Format) when the ImportKeyPair operation: 
Key is not in valid OpenSSH public key format
Run Code Online (Sandbox Code Playgroud)

hta*_*ess 8

AWS 支持RSA密钥对,它支持DSAECDSAEd25519密钥对。如果您尝试上传非RSA公开密钥,则会收到此错误。

这是记录在这里

Amazon EC2不接受DSA密钥。确保将密钥生成器设置为创建RSA密钥。

错误消息具有误导性,因为您可以上传有效的非RSA密钥并获取错误:

Error import KeyPair: InvalidKey.Format: Key is not in valid OpenSSH public key format
Run Code Online (Sandbox Code Playgroud)

对于在搜索此错误消息后找到此页面的人,此答案应该很有用。


Ina*_*mus 5

创建密钥,然后在调用aws的--public-key-material参数时,file://在密钥路径前面使用进行调用。

例:

$ aws --region eu-central-1 ec2 import-key-pair \
    --key-name "awsfrankfurt" \
    --public-key-material file://~/.ssh/awsfrankfurt  # <-- this
Run Code Online (Sandbox Code Playgroud)

这是一个很奇怪的问题,因为file://前缀通常用于Windows,但是在aws中,它也适用于基于unix的终端。