Amazon web services 在现有Linux AMI上用密钥对替换用户名/密码身份验证

Amazon web services 在现有Linux AMI上用密钥对替换用户名/密码身份验证,amazon-web-services,amazon-ec2,amazon-ami,Amazon Web Services,Amazon Ec2,Amazon Ami,我有一个ami,需要用户名/密码才能通过ssh登录。我想从中创建新的AMI,在其中我可以从任何新创建的密钥对登录 有什么建议吗?我不确定什么AMI允许用户名/密码登录,但当您从AMI创建实例时,需要指定密钥对 该密钥将被添加到Amazon Linux的默认用户ec2用户、ubuntu AMI的ubuntu等的授权密钥中。为什么不将用户/密码添加到实例中,然后从中构建AMI?然后,您可以更改/etc/ssh/sshd_配置,并允许用户名密码为:PasswordAuthentication yes。

我有一个ami,需要用户名/密码才能通过ssh登录。我想从中创建新的AMI,在其中我可以从任何新创建的密钥对登录


有什么建议吗?

我不确定什么AMI允许用户名/密码登录,但当您从AMI创建实例时,需要指定密钥对


该密钥将被添加到Amazon Linux的默认用户ec2用户、ubuntu AMI的ubuntu等的授权密钥中。

为什么不将用户/密码添加到实例中,然后从中构建AMI?然后,您可以更改/etc/ssh/sshd_配置,并允许用户名密码为:PasswordAuthentication yes。顺便说一下,用户名/口令认证不适合在云端的服务器上,因为人在中间攻击。使用它的风险自负

不确定我是否完全理解了这个问题,但是如果您想在实例启动时更改其行为,我建议您使用cloudinit进行模糊处理。实例中的配置位于/etc/cloud/cloud.cfg下。例如,在Ubuntu上,默认值是这样的:

user: ubuntu
disable_root: 1
preserve_hostname: False
...
如果要更改默认用户,可以在此处进行更改

user: <myuser>
disable_root: 1
preserve_hostname: False
...

最简单的方法是将以下代码段添加到/etc/rc.local或其等效文件中

#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.

touch /var/lock/subsys/local
if [ ! -d /root/.ssh ] ; then
    mkdir -p /root/.ssh
    chmod 0700 /root/.ssh
fi

# Fetch public key using HTTP
curl -f http://169.254.169.254/latest/meta-data/public-keys/0/openssh-key > /tmp/aws-key 2>/dev/null
if [ $? -eq 0 ] ; then
    cat /tmp/aws-key >> /root/.ssh/authorized_keys
    chmod 0600 /root/.ssh/authorized_keys
fi
rm -f /tmp/aws-key

# or fetch public key using the file in the ephemeral store:
if [ -e /mnt/openssh_id.pub ] ; then
    cat /mnt/openssh_id.pub >> /root/.ssh/authorized_keys
    chmod 0600 /root/.ssh/authorized_keys
fi

我的问题是删除用户名/密码并添加密钥对身份验证。我也在尝试CloudInit来解决这个问题