Skip to content
Advertisement

correct way to rename user and remove password with ec2 user-data

I have a centos7 base ami and have successfully changed the user name using the ec2-launch user data modified from an amazon-linux script

#!/bin/bash
groupadd ec2-user
usermod -d /home/ec2-user -m -g ec2-user -l ec2-user centos
echo ""  | sudo tee -a  /etc/sudoers
echo "Defaults:root !requiretty"  | sudo tee -a  /etc/sudoers
echo "ec2-user   ALL=(ALL) NOPASSWD: ALL"  | sudo tee -a  /etc/sudoers
echo "Defaults:ec2-user !requiretty"  | sudo tee -a  /etc/sudoers

log in works as expected and home directory has been changed, however when I use sudo it still asks for a password. As I cannot get into the file to check format I wonder if I am using the correct syntax?

How do change the user and remove the sudo password requirement in a single script?

Advertisement

Answer

I believe your Cloudinit userdata script is failing because it’s attempting to use sudo without a tty (and the !requiretty hasn’t been added yet). Since that script runs as root anyways, this should work:

#!/bin/bash
groupadd ec2-user
usermod -d /home/ec2-user -m -g ec2-user -l ec2-user centos
echo ""  | tee -a  /etc/sudoers
echo "Defaults:root !requiretty"  | tee -a  /etc/sudoers
echo "ec2-user   ALL=(ALL) NOPASSWD: ALL"  | tee -a  /etc/sudoers
echo "Defaults:ec2-user !requiretty"  | tee -a  /etc/sudoers
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement