The default when you launch any ec2 instances from AWS restricts the password login for security purposes by default. We can only log in to our instances using the .pem generated during the instance creation so in order to enable password authentication for our instances kindly follow the below steps.
Here I am using an ubuntu instance if you are running other instances like an Amazon Linux the default user will be ec2-user so you can modify the commands accordingly. In the blog, we are going to learn how to Enable password authentication for EC2 Instance
You can follow this official documentation from AWS in order to launch an ec2 instance.
https://docs.aws.amazon.com/efs/latest/ug/gs-step-one-create-ec2-resources.html
Step 1: log in to your instance
The first step is to log into your AWS ec2 instance using the .pem key created during the creation of the instance. You need to enable port 22 inbound traffic in the security group of your instance inorder to ssh into it.
ssh -i your-key.pem username@ip_address
Step 2: Switch to the user
Switch to the user you want to enable the password here I am using the user ubuntu but you can create your own user and enable password authentication for it.
sudo passwd <username>
Create a password for the user using the above command.
Step 3: Open the sshd config file.
Next, we need to open the sshd conf file in order to allow password authentication by default it will be disabled in ec2 instances.
sudo vi /etc/ssh/sshd_config
Find the Line containing “PasswordAuthentication” parameter and change its value from “no” to “yes”
PasswordAuthentication yes
Step 4: Restart the service
sudo systemctl restart sshd
Here as I am using ubuntu I used the systemctl restart command if you are running another type like amazon Linux you must use the service sshd restart command to restart the service
Step 5: Test Password Authentication on Your EC2 Instance
Testing the authentication of your instance for that try login in with the below command.
ssh ubuntu@<IP>
As you can see it asks for the password and we have successfully logged into using the password created before
Now we have enabled password authentication for the user ubuntu.
Also, the AWS ec2 instance blocks the root login default so if you want to enable the password authentication for the root user find “PermitRootLogin” parameter in the same sshd conf file and change its value from “prohibit-password” to “yes” and create a password for the root user by switching into root user and using the passwd command same as above.
Summary: In this blog, we have learned how to enable password authentication for any EC2 Instance