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

Photo by Ilya Pavlov on Unsplash

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

Guide to Setting Up SSH Keys and Passwords for Fresh EC2 User Accounts

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

1. 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.

2. 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.

3. Gain Root Privileges

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

      sudo su
    

4. Create a New User

  • Create a new user by running:

      adduser username
    

5. Set a Password for the New User

  • Set a password for the new user:

      passwd username
    

6. Change to the New User’s Home Directory

  • Navigate to the new user's home directory:

      cd /home/username
    

7. Create the .ssh Directory

  • Create the .ssh folder for the new user:

      mkdir .ssh
    

8. Generate a New SSH Key Pair

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

9. 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.

10. 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.

12. 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.

Now, 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

In this tutorial, we'll guide you through the steps to set up PasswordAuthentication for SSH access to a newly created user on your EC2 instance.

1. 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.

2. Login to EC2 Instance

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

3. Gain Root Privileges

  • Once logged in, switch to the root user:

      sudo su
    

4. 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.

5. Restart SSH Service

  • To apply the changes, restart the SSH service:

      systemctl restart ssh
    

6. Create a New User

  • Create a new user:

      useradd username
    

7. Set a Password for the New User

  • Set a password for the new user:

      passwd username
    

8. 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.