DEV Community

Cover image for EC2 Userdata script: Changing EC2 instance default username + run commands
Eden Jose
Eden Jose

Posted on • Edited on

EC2 Userdata script: Changing EC2 instance default username + run commands

This is a short note on how to change the username that your EC2 instance uses upon startup. If you're using RHEL or CentOS, you probably see that the default username you see when you log in to your instance for the first time is ec2-user.

You can change this, plus run other commands during startup by using the script below and putting it on the User Data field during the creation of instance.

In this example, I'm setting the following:

  • default username: jeden
  • hostname: tst-rhel
  • format of the shell prompt
#cloud-config
system_info:
  default_user:
    name: eden

runcmd:
 - 'sudo hostnamectl set-hostname tstrhel'
 - 'sudo sed -i "s/localhost/localhost tstrhel/" /etc/hosts'
 - 'echo "PS1=\"[\u@\H: \W] $ \"" >> .bashrc'
 - 'sudo yum update -y'
 - 'sudo yum install -y vim nano wget bash-completion firewalld'
 - 'sudo yum install -y epel-release'
 - 'sudo yum install -y python3'
 - 'sudo yum install -y python3-pip python-devel'
 - 'sudo yum groupinstall -y "development tools"'
 - 'sudo pip install --upgrade pip'
 - 'sudo yum install -y policycoreutils-python-utils'
Enter fullscreen mode Exit fullscreen mode

You may add more commands to be ran during startup by appending them in the runcmd section.

Note:

Some commands may not be ran during startup especially if the command requires that bootup process is done before it is executed.

Top comments (0)