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
- Create a New User
Create a new user by running:
adduser username
- Set a Password for the New User
Set a password for the new user:
passwd username
- Change to the New Userโs Home Directory
Navigate to the new user's home directory:
cd /home/username
- Create the .ssh Directory
Create the .ssh folder for the new user:
mkdir .ssh
- 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.
- 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
Open the authorized_keys file using a text editor:
vi authorized_keys
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
- Enable Password Authentication
Open the SSH configuration file:
vi /etc/ssh/sshd_config
Find the line containing PasswordAuthentication and change it to yes:
PasswordAuthentication yes
Save and exit the file.
- Restart SSH Service
To apply the changes, restart the SSH service:
systemctl restart ssh
- Create a New User
Create a new user:
useradd username
- Set a Password for the New User
Set a password for the new user:
passwd username
- 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.
You should now be able to log in to the EC2 instance as the new user using password-based authentication.
Top comments (0)