DEV Community

sriram ravi
sriram ravi

Posted on

How to Set Up Key-Based and Password-Based SSH for a Newly Created User on an EC2 Instance

How to Set Up Key-Based SSH for a Newly Created User on an EC2 Instance

  • Create a New EC2 Instance

First, create an EC2 instance using the AWS Management Console. Once the instance is running, you can log in via SSH.

  • Login to EC2 Instance

Use an SSH client (e.g., PuTTY, Terminal) to log in to your EC2 instance using the appropriate .pem key file.

  • Gain Root Privileges

Once logged in, enter the following command to switch to the root user:

sudo su
Enter fullscreen mode Exit fullscreen mode
  • Create a New User

Create a new user by running:

adduser username
Enter fullscreen mode Exit fullscreen mode
  • Set a Password for the New User

Set a password for the new user:

passwd username
Enter fullscreen mode Exit fullscreen mode
  • Change to the New Userโ€™s Home Directory

Navigate to the new user's home directory:

cd /home/username
Enter fullscreen mode Exit fullscreen mode
  • Create the .ssh Directory

Create the .ssh folder for the new user:

mkdir .ssh
Enter fullscreen mode Exit fullscreen mode
  • Generate a New SSH Key Pair

Go to the EC2 console and create a new PEM key.

  • Convert PEM to PPK (for PuTTY)

Open PuTTYgen and click on Load.

Select the PEM key you created and click Open.

Save the key as a .ppk file for use with PuTTY.

Image description

  • Copy the Public Key to the Authorized Keys File

In PuTTYgen, copy the public key to your clipboard.

Back on the EC2 instance, navigate to the .ssh folder for the new user:

cd /home/username/.ssh
Enter fullscreen mode Exit fullscreen mode

Open the authorized_keys file using a text editor:

vi authorized_keys
Enter fullscreen mode Exit fullscreen mode

Paste the public key into the authorized_keys file, then save and exit.

  • Log in Using SSH

Open a new session in your SSH client (e.g., PuTTY).

Enter the public IP address of your EC2 instance, select your private key file (.ppk), and use the new username you created to log in.

you should be able to log in to your EC2 instance as the new user using key-based authentication.

How to Set Up Password-Based SSH for a Newly Created User on an EC2 Instance

  • Create a New EC2 Instance

First, create an EC2 instance using the AWS Management Console. Once the instance is running, log in via SSH using your .pem key.

  • Login to EC2 Instance

Use an SSH client (e.g., PuTTY, Terminal) to log in to your EC2 instance.

  • Gain Root Privileges

Once logged in, switch to the root user:

sudo su
Enter fullscreen mode Exit fullscreen mode
  • Enable Password Authentication

Open the SSH configuration file:

vi /etc/ssh/sshd_config

Enter fullscreen mode Exit fullscreen mode

Find the line containing PasswordAuthentication and change it to yes:

PasswordAuthentication yes
Enter fullscreen mode Exit fullscreen mode

Save and exit the file.

Image description

  • Restart SSH Service

To apply the changes, restart the SSH service:

systemctl restart ssh
Enter fullscreen mode Exit fullscreen mode
  • Create a New User

Create a new user:

useradd username
Enter fullscreen mode Exit fullscreen mode
  • Set a Password for the New User

Set a password for the new user:

passwd username
Enter fullscreen mode Exit fullscreen mode

Image description

  • Login Using Password Authentication

Open a new SSH session, and enter the public IP address of your EC2 instance.

Enter the username and the password you set for the new user.

Image description

Image description

You should now be able to log in to the EC2 instance as the new user using password-based authentication.

Top comments (0)