Amazon Linux:创建AMI映像时更改默认的ec2-user

use*_*689 1 linux amazon amazon-ec2

我正在尝试使用具有自定义用户/密码的amazon linux distro创建ami图像。我所做的(手动):

  • 使用Amazon Linux AMI在Amazon实例中午餐
  • 用用户登录 ec2-user
  • 例如创建一个新用户 admin
  • 修改用户密码 admin
  • 将用户添加admin到车轮组:usermod -aG wheel admin
  • 将用户添加admin到sudoers文件:sudo echo "admin ALL=NOPASSWD:ALL " > /etc/sudoers.d/admin
  • 允许ssh用户/通过登录 /etc/sshd/sshd_config (PermitRootLogin yes )
  • 重新启动ssh服务: /etc/init.d/ssh restart
  • 当我尝试使用用户登录时admin-成功。但是,当创建新的ami图像并从新创建的ami午餐新实例时,通过用户adminec2-user- Permission denied (publickey). 我错过了什么!

Eri*_*ães 5

The issue is that the EC2 service is not aware of your changes when you are launching the instance with the AMI you created. I found a solution for that problem using Cloud-Init which is built-in on Amazon Linux and other official AMIs as well, the complete tutorial can be found here: https://emagalha.es/blog/2018/01/21/customizing-the-default-user-of-an-ubuntu-ami/

In short, here is what you need to do:

  1. Launch an instance with the Amazon Linux AMI placing the following lines in the user data field:
#cloud-config
system_info:
  default_user:
    name: admin
Run Code Online (Sandbox Code Playgroud)
  1. Connect to the instance using the admin user and create a file at /etc/cloud/cloud.cfg.d/defaults.cfg with the same contents of the user data file you used to launch the instance.

  2. Shut down the instance and create the AMI

  3. Enjoy!

You can also update the /etc/cloud/cloud.cfg.d/00_defaults.cfg inside the instance with the new username instead of creating a new file. I just like creating a new file to make the change more transparent, in any case, the choice is yours.