How To Change The Key Pair For EC2 Instance
Last Updated :
30 Jan, 2024
Amazon EC2 provides customizable virtual servers. To securely access them we use key pair. In this article, I will make sure you understand Amazon EC2 and how to secure them using key pairs, and also I will guide you through the step-by-step process to change your key pair of EC2 instances to ensure the safety of your EC2 environment.
What Is Amazon EC2?
Amazon EC2 is a web service that enables users to rent virtual servers in the cloud called EC2 instances. These EC2 instances can be scaled up or down based on the load on the instances. EC2 instances run on a variety of operating systems like Ubuntu, CentOS, etc and users who use these instances have full control over the configurations and management.
Amazon EC2 Key Pair Management
A key pair is a security credential associated with an EC2 instance when you launch an EC2 instance. These key pair consists of a public key and a private key. When you launch an instance public key is placed on the instance while the private key is stored in the local machine. This is crucial for communication with EC2 instances as it facilitates SSH access.
Sometimes you have to change the key pair of EC2 instances. Some reasons are:
- If the private key (key used to log in the EC2 instance) associated with the EC2 instance is lost or compromised then it is necessary to change the key pair to maintain the security of the EC2 instance.
- Changing the key pair periodically is a good practice as it enhances the overall security of the EC2 instance.
Steps To Change Key Pair For EC2 Instance
Step 1: For Creating an EC2 instance. Firstly provide the EC2 instance a name, then select Ubuntu as the AMI image (you can choose another image also) . Make sure that you are using t2.micro to avoid any costs. Then create a new key pair for the EC2 instance and download it. Maintain default settings for other options.
Step 2: You have now the .pem file in the downloads . Go to your downloads folder and open a terminal in it .

Step 3: Add execute permission to your .pem file(key). Then using the key in download folder login to your EC2 instance .
chmod +x old-key.pem
ssh -i "old-key.pem" [email protected]

Step 4 : Now you open another terminal in the downloads and here write the following command to generate a new key. This command will generate two keys a private key(key with no extension) and public key( key with .pub extension) .
ssh-keygen -b 4096 -f new-key

Step 5: To see the content of new public key write the following command .
cat new-key.pub

Step 6: Then copy the content of the new public key.

Step 7 : Now go to the terminal where you login to EC2 instance . Then change directory to .ssh directory . This .ssh directory contains the authorized keys.
cd .ssh

Step 8 : Here inside the directory you will find authorized_keys .You have to remove all the content and paste the new key content .
vi authorized_keys

Step 9 : Then logout from your EC2 instance . Use 'exit' command to logout from the EC2 instance .
exit

Step 10 : Rename your new private key.
mv new-key new-key.pem

Step 11 : Now login to your EC2 instance using the new private key.

And On trying to login the EC2 Instance with your the previous old key. If you try to access then you will get permission denied Error.

In this Article You first learned about what are EC2 instances and how they work in AWS cloud. Then you learned about key pairs , why they are important for security and when they need to be changed . Then finally We walked you through the step by step process of changing key pair of an EC2 instance to make sure that your AWS cloud server remains safe and Secure.
Similar Reads
How To Create EC2 Instances Using SDK For JAVA ?
The AWS SDK for Java provides various functionalities to use AWS services using APIs. It provides support for building Java applications easily with the help of the SDK. Using the SDK makes it easier to procure AWS services directly from Java code. Creating and provisioning EC2 instances from Java i
5 min read
How To Create Redhat EC2 Instance in AWS
provisioning the Red Hat Enterprise Linux (RHEL) instances on Amazon Web Services (AWS) offers a powerful and versatile solution for hosting and running applications, overseeing the jobs, and utilizing the abilities of both platforms. Red Hat Enterprise Linux is a main Linux distribution eminent for
5 min read
Securing EC2 Instances With Security Groups And Key Pairs
Amazon provides a service for website hosting and maintenance that is EC2, Elastic Cloud instance. They are the backbone of cloud-based applications, providing scalability and flexibility in computing resources. However, there are many challenges in protecting these instances from threats and attack
6 min read
How to Install Python3 on AWS EC2?
AWS or Amazon web services is a cloud service platform that provides on-demand computational services, databases, storage space, and many more services. EC2 or Elastic Compute Cloud is a scalable computing service launched on the AWS cloud platform. In simpler words, EC2 is nothing but a virtual com
3 min read
How to Install PHP on AWS EC2?
AWS or Amazon web services is a cloud service platform that provides on-demand computational services, databases, storage space, and many more services. EC2 or Elastic Compute Cloud is a scalable computing service launched on the AWS cloud platform. In simpler words, EC2 is nothing but a virtual com
2 min read
How To Increase The Storage Of EC2 Instance ?
EC2 or Elastic Compute Cloud is a core service provided by Amazon web services (AWS) which is used to create virtual servers on the AWS cloud platform the one that we create using the EC2 service is called an EC2 instance. we can use this ec2 instance to deploy and run a wide variety of applications
3 min read
How to Install R on AWS EC2?
R is widely used as a different programming language. There are several programming languages are present for different purposes. The C programming language is used to get basic knowledge in the programming field. Java programming language is used to get some deep knowledge of programming & its
7 min read
How To Create Key Pair In AWS Using Terraform ?
In cloud infrastructure management, secure admittance to instances is central. While working with Amazon Web Services (AWS) utilizing Terraform, creating key pairs is fundamental for secure access to EC2 instances. Key pairs comprise of a public key and a private key, where the public key is utilize
6 min read
How to Install Go on AWS EC2?
EC2 or Elastic Compute Cloud is a scalable computing service launched on the AWS cloud platform. In simpler words, EC2 is nothing but a virtual computer on which we can perform all our tasks and we have the authority to configure, launch or even dissipate this virtual computer.Go is an open-source,
2 min read
How to Install Python 2.8 on AWS EC2?
AWS or Amazon Web Services is one of the biggest cloud services providers with a variety of services such as on-demand computational services, databases, storage space, etc. EC2 or Elastic Compute Cloud is one of its services which acts as an on-demand computing service on the cloud platform. From a
4 min read