1. Log in to Your EC2 Instance
Use your private key to SSH into the EC2 instance: ssh -i /path/to/key.pem ec2-user@<EC2_IP>
Replace /path/to/key.pem with your key file and <EC2_IP> with the instance’s public IP.
2. Set or Reset the Password for the sudo User
Once logged in, switch to sudo mode: sudo su
Set a new password for the sudo user (e.g., ec2-user or ubuntu): passwd ec2-user
Replace ec2-user with your user name if it’s different.
Enter and confirm the new password when prompted.
After setting the password, you can use it when prompted for sudo actions or in other workflows requiring the sudouser’s password.
Important Notes
Avoid Password-Based Authentication for SSH: It’s less secure than key-based authentication. Use passwords sparingly and only where necessary.
Check AWS Instance Defaults: Some AMIs (e.g., Ubuntu, Amazon Linux) disable password authentication entirely in sshd_config. You may need to enable it temporarily if required:
- Edit the SSH configuration file:
sudo nano /etc/ssh/sshd_config - Look for
PasswordAuthenticationand set it to yes:PasswordAuthentication yes - Restart the SSH service:
sudo systemctl restart sshd
Revert Changes for Security: If you enable password authentication, disable it after completing your task to maintain security.