在Ubuntu AWS的自定义用户下安装authorized_keys文件

时间:2018-02-27 16:41:43

标签: amazon-web-services ubuntu ami packer aws-cloud-config

我正在尝试设置一个ubuntu服务器并使用非默认用户登录。我已经使用cloud-config和用户数据来设置初始用户,并使用打包器来配置服务器:

Grid.SetColumn(myDockPanel, 1); //= <DockPanel ... Grid.Column = "1"
Grid.SetRow(myDockPanel, 1); //= <DockPanel ... Grid.Row = "1"
Grid.SetRowSpan(myDockPanel, 8); //= <DockPanel ... Grid.RowSpan = "8"

Packer登录并将服务器设置为my_user,但是当我从AMI启动实例时,AWS会在/home/ubuntu/.ssh /

下安装authorized_keys文件

Packer config:

system_info:
  default_user:
    name: my_user
    shell: /bin/bash
    home: /home/my_user
    sudo: ['ALL=(ALL) NOPASSWD:ALL']

服务器启动后,/ etc / passwd中都存在ubuntu和my_user用户:

{
    "variables": {
        "aws_profile": ""
    },
    "builders": [{
        "type": "amazon-ebs",
        "profile": "{{user `aws_profile`}}",
        "region": "eu-west-1",
        "instance_type": "c5.large",
        "source_ami_filter": {
            "most_recent": true,
            "owners": ["099720109477"],
            "filters": {
                "name": "*ubuntu-xenial-16.04-amd64-server-*",
                "virtualization-type": "hvm",
                "root-device-type": "ebs"
            }
        },
        "ami_name": "my_ami_{{timestamp}}",
        "ssh_username": "my_user",
        "user_data_file": "cloud-config"
    }],
    "provisioners": [{
        "type": "shell",
        "pause_before": "10s",
        "inline": [
            "echo 'run some commands'"
        ]}
    ]
}

ubuntu用户在什么时候创建,有没有办法在启动时在/home/my_user/.ssh下安装authorized_keys文件而不是ubuntu?

2 个答案:

答案 0 :(得分:0)

您可以在使用cloud-init创建用户时添加公钥。这是你如何做到的。

users:
  - name: <username>
    groups: [ wheel ]
    sudo: [ "ALL=(ALL) NOPASSWD:ALL" ]
    shell: /bin/bash
    ssh-authorized-keys: 
    - ssh-rsa AAAAB3Nz<your public key>...

Addding additional SSH user account with cloud-init

答案 1 :(得分:0)

要在使用AMI从中启动新的EC2实例时保留默认用户,您必须更改值/etc/cloud/cloud.cfg并更新此部分:

system_info:
   default_user:
     # Update this!
     name: ubuntu