By default you can only log in to an Amazon EC2 Linux instance with an SSH key, as the default user (ec2-user, centos, rocky and so on), and that is extremely secure. In some instances it may be necessary to log in as root, or with a traditional username and password, as a “back door” to your web server. This guide shows how, what each setting does, and how to keep the risk down.
Prerequisites
- An EC2 instance running CentOS (the original steps) or a current RHEL, AlmaLinux, Rocky Linux or Amazon Linux image.
- An open SSH session as the default user with
sudorights. Keep this session open until you have tested the new login in a second window, so a typo can’t lock you out. - A security group that only allows port 22 from addresses you trust.
1. Edit your SSH config
Become root (sudo -i) and edit the SSH daemon configuration:
vi /etc/ssh/sshd_config

2. Un-comment the PermitRootLogin line
In the original CentOS image the setting was on line 42. Un-comment it and make sure it reads:
PermitRootLogin yes

PermitRootLogin controls whether root may log in over SSH at all. The possible values are yes, no, prohibit-password (root with keys only; older releases call it without-password) and forced-commands-only. If you only need root with an SSH key, prohibit-password is the safer choice.
3. Allow passwords if you need them
EC2 images ship with password logins disabled for every user. If the back door should accept a password, also set:
PasswordAuthentication yes
Root on an EC2 image has no usable password, so set one:
passwd root
Pick a strong one; see Choosing a Smart Root Password. A server with root password logins on the internet gets brute-force attempts within minutes.
4. Restart sshd
On CentOS 6, issue a service sshd restart:
service sshd restart
On systemd releases (CentOS 7, RHEL/AlmaLinux/Rocky 8-10, Amazon Linux 2 and 2023), test the configuration first, then restart:
sshd -t
systemctl restart sshd
That’s it! Open a second terminal and try ssh root@your-instance before you close your original session.
Root key logins and the cloud-init message
If root logins with your key reply Please login as the user “ec2-user” rather than the user “root” and disconnect, that is cloud-init. Its disable_root setting (on by default) puts a forced command in front of the key in /root/.ssh/authorized_keys. Remove that prefix so the line starts with ssh-rsa or ssh-ed25519, and set disable_root: false in /etc/cloud/cloud.cfg so it isn’t written again.
Modern releases: use a drop-in file
RHEL 9 and its rebuilds (AlmaLinux, Rocky Linux 9 and 10) default to PermitRootLogin prohibit-password, and sshd_config starts by including /etc/ssh/sshd_config.d/*.conf. Cloud images also add a 50-cloud-init.conf there with PasswordAuthentication no. sshd uses the first value it reads for each keyword, so put your overrides in a file that sorts first instead of editing the main file:
cat > /etc/ssh/sshd_config.d/01-root-login.conf <<'EOF'
PermitRootLogin yes
PasswordAuthentication yes
EOF
sshd -t && systemctl restart sshd
Verify the effective settings
sshd -T prints the configuration sshd will actually use:
sshd -T | grep -Ei 'permitrootlogin|passwordauthentication'
Troubleshooting
- Permission denied (publickey) when using a password: password authentication is still off. Check
sshd -Tand the files in/etc/ssh/sshd_config.d/. - sshd won’t start after the change: run
sshd -tto find the bad line, and readjournalctl -u sshd. - Locked out completely: use the EC2 Serial Console or Session Manager, or stop the instance and attach its root volume to another instance to fix the file.
Safer alternatives
On current AWS you rarely need a root back door: AWS Systems Manager Session Manager and EC2 Instance Connect give you emergency shell access without opening root logins. If you do enable root, consider turning it off again afterwards and going back to keys only, as in Configure SSH Key Authentication Only.
Related guides: Configure SSH Key Authentication Only | Choosing a Smart Root Password

Hey would you mind letting me know which hosting company you’re working with?
I’ve loaded your blog in 3 completely different
browsers and I must say this blog loads a lot faster than most.
Can you suggest a good web hosting provider at a reasonable price?
Thanks a lot, I apprecdiate it!
Absolutely! We host it.
https://zwiegnet.com/services/linux-hosting