shou2017.com
JP

SSH connect to EC2 without key pairs

Tue Jul 26, 2022
Sat Aug 10, 2024
AWS

With AWS Systems Manager, you can easily connect to EC2 without key pairs, but there are some issues with this method.

For example, if you have multiple developers working on the same EC2, each person needs their own connection environment. It’s a hassle to distribute key pairs to everyone. You can use AWS Systems Manager or EC2 Instance Connect, but some people may not have those environments set up. The best is to create an environment where you can just SSH in directly.

So, let’s actually do it!

This is for EC2 with Amazon Linux.

  1. Use a key pair to log in as root.
  2. Set up users and passwords.
useradd username
passwd username
  # Change the password for 'username'.
  # New password:

Once that’s done, next:

  1. Edit /etc/ssh/sshd_config

Most likely, your sshd_config looks like this, so uncomment and enable PasswordAuthentication yes and PermitRootLogin yes.

# To disable tunneled clear text passwords, change to no here!
#PasswordAuthentication yes   # Uncomment this
#PermitEmptyPasswords no
PasswordAuthentication no     # Comment this out

# Authentication:

#LoginGraceTime 2m
#PermitRootLogin yes   # Uncomment this
#StrictModes yes
#MaxAuthTries 6
#MaxSessions 10

After that, just restart the sshd service.

systemctl restart sshd.service

Try to connect to make sure it works.

ssh username@public_IP

And that’s it. Just give each person the initial password, and once they connect, they can change it to something only they know.

Tags
EC2
See Also