For AWS:
Generate new key pair with your favorite tools.
Ex:- using openssh
samshaik@shaikprod:~$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/samshaik/.ssh/id_rsa): cloud-key
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in cloud-key.
Your public key has been saved in cloud-key.pub.
The key fingerprint is:
SHA256:GJk8yaVsFSNxquyVLwmR6uGMxzslniA0TMQ1Ib9AO8g samshaik@shaikprod
The key's randomart image is:
+---[RSA 2048]----+
|o+.+. o.*. |
|+o+ .= @ . |
|+E .o & |
| +oo.+ = |
|. +.+ + S |
|o*.o.o o |
|oo*+. o . |
| .+. . |
| .. |
+----[SHA256]-----+
Now this will create two keys (public/private) in the specified path.
samshaik@shaikprod:~$ ls -lrt ~/.ssh/cloud-key*
-rw------- 1 samshaik samshaik 1766 Jun 9 14:44 /home/samshaik/.ssh/cloud-key
-rw-r--r-- 1 samshaik samshaik 400 Jun 9 14:44 /home/samshaik/.ssh/cloud-key.pub
1) Now stop the VM
2) Include the below code snippet in the user data dialog box as follows:
Select instance --> Actions --> Instance Settings then choose View/Change User Data
Content-Type: multipart/mixed; boundary="//"
MIME-Version: 1.0
--//
Content-Type: text/cloud-config; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="cloud-config.txt"
#cloud-config
cloud_final_modules:
- [users-groups, once]
users:
- name: username (name of the user you want to change the key for ex:-ec2-user)
ssh-authorized-keys:
- PublicKeypair (No quotes required and make sure you have dash at the begining)
For AWS:

3) Start the instance
Now try ssh into the instance using the updated key.
Azure:
In Azure select the VM from the left menu --> Support + Troubleshooting --> Reset Password
Here you can either create a new user/update existing user password or ssh keys
In Oracle Cloud:

