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

I'm using CloudInit to automate the startup of my instances on AWS. I followed the (scarce) documentation available at http://bazaar.launchpad.net/~cloud-init-dev/cloud-init/trunk/annotate/head%3A/doc/examples/cloud-config.txt and examples on /usr/share/doc/cloud-init, but still haven't figured out how to change the default username (ubuntu, id:1000).

I know I can create a script to manually delete the default ubuntu and add my user, but seems counter intuitive given that CloudInit exist exactly to automate the initial setup.

Any ideas?


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


You can put this in userdata:

#cloud-config
system_info:
  default_user:
    name: otherusername