Logo

dev-resources.site

for different kinds of informations.

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

Published at
1/9/2025
Categories
aws
devops
ssh
sshuser
Author
sriram_ravi
Categories
4 categories in total
aws
open
devops
open
ssh
open
sshuser
open
Author
11 person written this
sriram_ravi
open
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.

ssh Article's
30 articles in total
Favicon
How to Set Up Key-Based and Password-Based SSH for a Newly Created User on an EC2 Instance
Favicon
SSH Keys | Change the label of the public key
Favicon
让安卓手机不再吃灰:在安卓手机上搭建 Rust 开发环境
Favicon
SSH port forwarding from within code
Favicon
Mastering Ansible on macOS A Step by Step Guide
Favicon
kkTerminal —— A terminal for Web SSH connection
Favicon
Set Up SSH in 1 Minute Setup Like a Pro (With Some Fun Along the Way)
Favicon
How to Configure GitHub Authentication Using SSH Certificates
Favicon
Understanding SSH: Secure Shell Protocol
Favicon
Check gitlab ssh key auth
Favicon
How I Secured Port 22
Favicon
SSH port forwarding from within Raku code
Favicon
Changing an established SSH connection without disconnecting
Favicon
SSH port forwarding from within Rust code
Favicon
Configure SSH Passwordless Login from Windows to Linux
Favicon
Push to multiple GitHub accounts!
Favicon
Access to Google Cloud Virtual Machine through SSH
Favicon
Large file transfer from VPS to local machine
Favicon
Secure Your Ubuntu VPS: Restrict SSH Access to a Specific IP
Favicon
Accessing Remote Databases Without VPN Using SSH Tunnels
Favicon
Carla Simulator 1 : How to Set Up CARLA Simulator 🏎️🔥
Favicon
Getting Started with Oysape: Exploring Task and Pipeline
Favicon
Increase Debian based Linux VPS server’s security
Favicon
Splunk - SSH Dashboard Creation
Favicon
Debugging SSH connections: A Comprehensive Guide
Favicon
Understanding SSH Key Pairs: A Developer's Guide
Favicon
SSH Config File - Forgotten Gem
Favicon
SSH kalitini Github.com'ga qo'shish
Favicon
Using SSH to Connect Local Git to Remote Repositories
Favicon
Quickly and Easily Manage Multiple SSH and GPG Keys Across Git Repositories

Featured ones: