How to change default user (ubuntu) via CloudInit on AWS

Solution 1:

According to this, CloudInit should support custom directives to create new users including overriding the default "ubuntu" user. I've tried it following examples, but haven't been able to get it to work.

However, since CloudInit supports user-data scripts and you can do just about anything in a script, I prefer to use standard commands rather than try to learn some new custom directives.

Here's how I change the default username from "ubuntu" in a user-data script. This example uses the new username "newuser" which you should change to your preference:

#!/bin/bash -ex
user=newuser
usermod  -l $user ubuntu
groupmod -n $user ubuntu
usermod  -d /home/$user -m $user
mv /etc/sudoers.d/90-cloudimg-ubuntu /etc/sudoers.d/90-cloudimg-$user
perl -pi -e "s/ubuntu/$user/g;" /etc/sudoers.d/90-cloudimg-$user

You can add on to this user-data script to do any other initialization and configuration needed on your instances.

Update: I've written an expanded article describing the steps for using both a user-data script and how to do it with CloudInit on recent versions of Ubuntu: http://alestic.com/2014/01/ec2-change-username

Solution 2:

You can put this in userdata:

#cloud-config
system_info:
  default_user:
    name: otherusername