In this article, we will explain you how to create SSH passwordless login authentication to AWS EC2.
When accessing an EC2 instance, it's often required to authenticate using a password or SSH key. However, using a passwordless SSH login provides an added layer of security and convenience.
Using a passwordless SSH login to access your AWS EC2 instances provides several benefits. First, it eliminates the need to remember and type in a password every time you need to access your instance. This saves time and reduces the risk of typos or mistakes.
Second, it reduces the risk of password-based attacks, such as brute-force attacks or phishing scams. Finally, it allows you to easily automate processes and scripts that require access to your instance, without the need for manual intervention.
Setting Up an EC2 Instance:
To create a passwordless SSH login, we first need to set up an EC2 instance. You must have any existing AWS EC2 or You can create New AWS EC2 Instance.
This can be done by logging into your AWS account and navigating to the EC2 dashboard. From there, click the "Launch Instance" button and select the desired instance type, Amazon Machine Image (AMI), and configuration options. When configuring your instance, make sure to select a secure password, and limit access to only those who need it.
Generating SSH Keys
SSH keys are a pair of cryptographic keys that can be used to authenticate with an SSH server. In order to create a passwordless SSH login to AWS EC2, we need to generate a key pair. To do this, open a terminal window on your local machine and run the following command.
ssh-keygen -t rsa -b 4096
This command will generate a public and private key pair in the ~/.ssh directory. The -t flag specifies the type of key to generate (in this case, RSA), and the -b flag specifies the number of bits in the key.
Setting Up Passwordless SSH Login
Now that we have our EC2 instance and SSH keys, we can set up passwordless SSH login. To do this, we need to copy the public key to the EC2 instance.
Use ssh-copy-id command to copy the SSH key directly to target EC2 instance.
ssh-copy-id -i ~/.ssh/id_rsa.pub user@ec2-instance-ip
Replace user with the username of the instance, and ec2-instance-ip with the public IP address of the instance.
This command will copy the public key to the instance and add it to the authorized keys file, allowing passwordless SSH authentication.
Test the connection:
Try login to target EC2 Instance from Source EC2 Instance to confirm the copied SSH public key is working or not.