How to Setup Passwordless SSH Login on EC2 Ubuntu 22.04

Secure Shell (SSH) is a protocol for secure remote login from one computer to another. It provides several options for strong authentication, and it protects the connection by encrypting the data that passes through it. In this tutorial on Linuxbeast blog, we’ll go through the process of setting up SSH key-based authentication for an Amazon EC2 instance running Ubuntu 22.04, allowing for passwordless access.

Prerequisites

  • An Amazon EC2 instance running Ubuntu 22.04.
  • Local machine with SSH client installed.
  • Access to the local machine’s terminal or command line.
  • The private key file (.pem) for your EC2 instance.
  • Your EC2 instance’s Public DNS (Domain Name System) or IP address.

Step 1: Generate an SSH Key Pair

To start, we need to create a new SSH key pair on your local machine which will be used to authenticate with the EC2 instance. Open your terminal and execute the ssh-keygen command:

ssh-keygen -t rsa -m PEM

You will see output similar to:

Generating public/private rsa key pair.
Enter file in which to save the key (/home/ubuntu/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/ubuntu/.ssh/id_rsa.
Your public key has been saved in /home/ubuntu/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:Oeynyx4ToTHJ1a8SqgLwjqcqbcoM117yn5e4u1Jf+jg user@MyDesktop-PC

After the keys are generated, you can check them using this command:

ls -lah ~/.ssh

You should see id_rsa (private key) and id_rsa.pub (public key).

View your public key with:

cat ~/.ssh/id_rsa.pub

Copy the output as you’ll need this for the next step.

Step 2: Copy the Public Key to Your EC2 Instance

Now, transfer the public key to your EC2 instance using the following command, replacing your details where appropriate (your key location and EC2 public DNS or IP). Keep in mind that the EC2 key PEM file is different from the key pair file generated by ssh-keygen:

cat ~/.ssh/id_rsa.pub | ssh -i ~/path/to/your/key.pem ubuntu@your-ec2-public-dns "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"

Verify that the public key is correctly copied to the server:

ssh -i ~/path/to/your/key.pem ubuntu@your-ec2-public-dns "cat ~/.ssh/authorized_keys"

If the pasted key matches the one on your local machine, you’re good to go!

Step 3: Login to Your EC2 Instance Without a Password

Finally, you can now SSH into your EC2 instance without entering a password every time:

ssh ubuntu@your-ec2-public-dns

And there it is! You should be logged into your EC2 Ubuntu 22.04 instance without being prompted for a password.

Conclusion

Remember to replace placeholders like your-ec2-public-dns with the actual public DNS or IP address of your EC2 instance and the appropriate paths to your private key file (key.pem) that you use for connecting to your instance.

Setting up passwordless SSH login on EC2 Ubuntu 22.04 improves security and streamlines access. Follow our guide for a smoother workflow. For further assistance, please leave a comment below.

2 thoughts on “How to Setup Passwordless SSH Login on EC2 Ubuntu 22.04”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.