How to reset local Linux password on Azure VMs
This article introduces several methods to reset local Linux Virtual Machine (VM) passwords. If the user account is expired or you just want to create a new account, you can use the following methods to create a new local admin account and re-gain access to the VM.
You can't log in to the VM, and you receive a message that indicates that the password that you used is incorrect. Additionally, you can't use VMAgent to reset your password on the Azure portal.
Manual password reset procedure
The following steps does not apply to the VM with unmanaged disk.
Take a snapshot for the OS disk of the affected VM, create a disk from the snapshot, and then attach the disk to a troubleshoot VM. For more information, see Troubleshoot a Windows VM by attaching the OS disk to a recovery VM using the Azure portal.
Connect to the troubleshooting VM using Remote Desktop.
Run the following SSH command on the troubleshooting VM to become a super-user.
Run fdisk -l or look at system logs to find the newly attached disk. Locate the drive name to mount. Then on the temporal VM, look in the relevant log file.
grep SCSI /var/log/kern.log (ubuntu) grep SCSI /var/log/messages (centos, suse, oracle)
The following is example output of the grep command:
kernel: [ 9707.100572] sd 3:0:0:0: [sdc] Attached SCSI disk
Create a mount point called tempmount.
Mount the OS disk on the mount point. You usually need to mount sdc1 or sdc2. This will depend on the hosting partition in /etc directory from the broken machine disk.
mount /dev/sdc1 /tempmount
Create copies of the core credential files before making any changes:
cp /etc/passwd /etc/passwd_orig cp /etc/shadow /etc/shadow_orig cp /tempmount/etc/passwd /etc/passwd cp /tempmount/etc/shadow /etc/shadow cp /tempmount/etc/passwd /tempmount/etc/passwd_orig cp /tempmount/etc/shadow /tempmount/etc/shadow_orig
Reset the user’s password that you need:
Move the modified files to the correct location on the broken machine's disk.
cp /etc/passwd /tempmount/etc/passwd cp /etc/shadow /tempmount/etc/shadow cp /etc/passwd_orig /etc/passwd cp /etc/shadow_orig /etc/shadow
Go back to the root and unmount the disk.
cd / umount /tempmount
In Azure portal, detach the disk from the troubleshooting VM.