Cloudbooklet
  • News
  • Artificial Intelligence
  • Applications
  • Linux
No Result
View All Result
Cloudbooklet
  • News
  • Artificial Intelligence
  • Applications
  • Linux
No Result
View All Result
Cloudbooklet
No Result
View All Result
Home AWS

How to Add New User with Key Pair in AWS EC2

by Cloudbooklet
3 years ago
in AWS
How To Add New User With Key Pair In Aws Ec2
ShareTweetSendShare
Readers like you help support Cloudbooklet. When you make a purchase using links on our site, we may earn an affiliate commission.

How to Add New User with Key Pair in AWS EC2. Learn how to create new user with new key pair and provide separate access to developers. Instead of using the default private key for all operations you can create additional users and share the corresponding private key to access the instance. In this guide […]

ADVERTISEMENT

How to Add New User with Key Pair in AWS EC2. Learn how to create new user with new key pair and provide separate access to developers. Instead of using the default private key for all operations you can create additional users and share the corresponding private key to access the instance.

In this guide you are going to learn how to create new SSH key pair with public key and private key and add it to your EC2 instance.

Prerequisites

Pem key file to access the instance SSH that you downloaded while creating the instance.

ADVERTISEMENT

Create New Key Pair

Login to your AWS console and navigate to EC2 >> NETWORK & SECURITY >> Key Pairs.

You might also like

How To Install Memcached With Apache-Php

How to Install and Configure Memcached with Apache and PHP

2 years ago
Setup Geoip Block Using Apache

How to Setup GeoIP Block using Apache

2 years ago

Click Create Key pair.

For Name enter a name to identify your key.

ADVERTISEMENT

For File Format choose pem which is the default format used by EC2. You can also use ppk format if you wish to use PUTTY to access the instance.

Click Create key pair.

ADVERTISEMENT

Now the private key will be downloaded to your browser. Save the key safely.

Note: This is the only time you get the key pair. You cannot download the key pair if you lost it.

ADVERTISEMENT

To use the .pem key you need to setup correct permissions, otherwise you cannot use it to connect to the instance.

chmod 400 key_pair_name.pem

Retrieve the Public Key from the Private Key

Once you get the private key, you can retrieve the public key easily using the following command.

ADVERTISEMENT
ssh-keygen -y -f key-pair-name.pem

This command returns the public key similar to the one below.

ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCo4D7e4OZnk2RbXHJjJIZNtyH6lpOW6FYPbD2Z9coosHqDTur0d482hzf58cO/0KDEGVRiSdUIj5wlUVlE+a//HCW8FQZBdZxVcfaua5uhAgUjnjUJwCJD+a/dHgyUFxXfQQSRCgv9rtREcbGn5NuXx/ZceWhQvgrnBVV5PI8dwXcc2qOx0w+1xp3gE2l1HLa7r0sI6ikiGrjNPkbFcbOawGW1F+K49hKfFlRqgCfmDnYkIiv9aEv5crUeQhpU5ChwYVJBV6VY8jeKee+17Ozcop4S4pHYbOrTBDt0Cz9jo50lS4JuoF2WWFCwk3yTcN+8fnykcEHEB02T1IXDtd4b

Copy the public key.

Create New User

SSH to your EC2 instance and perform the below steps to add user.

Use the adduser command to create new user.

For Ubuntu users use the following command.

sudo adduser username --disabled-password

This command will create a user with the specified username without password.

For Amazon Linux or Amazon Linux 2 users use the following command.

sudo adduser username

Add the Public Key to New User

Once the user is created you need to add the retrieved public key to the new user account.

Switch to the new user.

sudo su - username

This command switches from the default ubuntu or ec2-user to the new user you created in the previous step.

Navigate to the home directory of the new user.

cd ~/

Create a new .ssh directory.

mkdir .ssh

Setup appropriate permissions.

chmod 700 .ssh

Create a file named authorized_keys in the .ssh directory and change its file permissions to 600 (only the owner can read or write to the file).

touch .ssh/authorized_keys

Setup appropriate permissions for the file.

chmod 600 .ssh/authorized_keys

Edit the authorized_keys file.

nano authorized_keys

Add the retrieved public key to this file.

Note: The public key should be pasted in a single line. It should not be split over multiple lines.

Hit CTRL + X followed by Y and ENTER to save and exit the file.

Now you can login to your EC2 instance SSH using the corresponding private key.

Remove User

If you wish to remove the user, you can easily remove it using the userdel command.

Specify the -r option to remove the home directory. You can also skip this option if you wish to keep the home directory.

sudo userdel -r username

Conclusion

Now you have learned how to create new user with public key and private key on your AWS EC2 instance.

Tags: AWSEC2
Share3Tweet2SendShare
Cloudbooklet

Cloudbooklet

Comments 2

  1. Avatar Of Raúl Raúl says:
    2 years ago

    It worked perfectly. Thank you very much!

    Reply
    • Avatar Of Cloudbooklet Cloudbooklet says:
      2 years ago

      Very welcome

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Related Posts

Setup Ci/Cd Workflow With Github Actions

Setup CI/CD Workflow with GitHub Actions

2 years ago
How To Install And Setup Csf

How to Secure your Ubuntu server with CSF Firewall

2 years ago
Setup A Load Balanced Wordpress Website On Aws Ec2

Setup a Load Balanced WordPress Website on AWS EC2 – Part 2

3 years ago
Setup A Load Balanced Wordpress Website On Aws Ec2

Setup a Load Balanced WordPress Website on AWS EC2 – Part 1

3 years ago

Follow Us

Trending Articles

Delete Netflix Account

How to Delete Netflix Account Permanently

September 21, 2023

How to Create and Customize Stunning Contact Poster on iPhone

5 Best Laptop for Minecraft in 2023: Top Picks for All Budgets

Microsoft Surface Event: The Most Exciting and Innovative Launches and Updates

5 Free AI Soulmate Maker: Create Your Perfect Match

10 Best AI Song Generator in 2023 (Free and Paid)

Popular Articles

Ai Photo Editor

7 Best AI Photo Editor You Need to Try Online Free

September 7, 2023

Amazon Prime Big Deal Days 2023: Best Deals

How to Create Your Own VPN

Top 10 Advantages of a Cloud VPS Server

10 Best Minecraft Server Hosting Providers in 2023

Validator AI: The AI Powered Business Idea Validator

Subscribe Now

loader

Subscribe to our mailing list to receives daily updates!

Email Address*

Name

Cloudbooklet Logo

Welcome to our technology blog, where we explore the latest advancements in the field of artificial intelligence (AI) and how they are revolutionizing cloud computing. In this blog, we dive into the powerful capabilities of cloud platforms like Google Cloud Platform (GCP), Amazon Web Services (AWS), and Microsoft Azure, and how they are accelerating the adoption and deployment of AI solutions across various industries. Join us on this exciting journey as we explore the endless possibilities of AI and cloud computing.

  • About
  • Contact
  • Disclaimer
  • Privacy Policy

Cloudbooklet © 2023 All rights reserved.

No Result
View All Result
  • News
  • Artificial Intelligence
  • Applications
  • Linux

Cloudbooklet © 2023 All rights reserved.