By default, Amazon Web Services EC2 Linux instances are created with authentication based on a private key (.PEM), which you use to connect over SSH with PuTTY or any other SSH client. In this tutorial we will see how to enable EC2 password authentication, using a username and password for the root user on AWS EC2 instances.
I personally use NANO, but you can use any editor you prefer, such as VIM. The first thing we will do is edit the sshd_config file located in the /etc/ssh/ folder:
sudo nano /etc/ssh/sshd_config
Once the file is open, find two specific lines: PasswordAuthentication and PermitRootLogin. If you are using NANO, you can use the search command (Ctrl + W) to find these values faster. Once you find them, change the value “no” or “prohibit-password” to “yes”, as in the example below:
PasswordAuthentication yes
PermitRootLogin yes
Important (security): enabling root login with a password increases your server’s attack surface. Use it only when necessary, set a long and unique password, restrict port 22 to trusted IP addresses with Security Groups and consider tools such as fail2ban. Whenever possible, prefer SSH keys.
Save the file. If you use NANO, press Ctrl + X to save the changes and exit. Next, set a password for the root user with the following commands. The passwd command will ask you for a new root password:
sudo su
passwd
Now restart the SSH service with the command below. You can also reboot the instance if you prefer.
sudo service sshd restart
Done! You can now connect to your EC2 instance using its public IP address or endpoint with the root user and the password you chose. If you still have questions about the process, here is a video walkthrough (in Spanish):
Need help managing or securing your AWS servers? Explore our managed cloud hosting service and our cybersecurity assessments.
