Change default password of root user on aws ubuntu ec2 instance

First ssh into your server using the ssh key.

ssh -i "Mysecretkey.pem" ubuntu@ec2-ip.compute-1.amazonaws.com

Once you are logged in, switch to root user with the following command

sudo su

If successful, you will be switched to root.

Optional:

If you want to enable the root level login, then run this one liner command to enable root login

sed -i -e 's/.*exit 142" \(.*$\)/\1/' /root/.ssh/authorized_keys

Then run passwd root

It will ask for the new password

New password: // Enter new password here
Retype new password: // Re-Enter new password here

Once everything is correct, you will get the success message

passwd: password updated successfully

Congratulations, you have successfully changed your root password.

Also, to change the password of the specific user, you can repeat the same procedure, just provide the different username with the switch user (su) command.

That's it !