Friday 28 January 2022

Reset your Azure Ubuntu VM Password

I created an Ubuntu VM in Azure some months ago and when this week I decided to connect to it again I realised that I could not find my password (I mean the password for my user in that Ubuntu VM, not my Azure account password). Oddly enough, there's not an option in the Azure portal or an Azure CLI command to directly reset the VM password, you'll have to do it through several steps.

First point is enable login to the VM using a ssh key. So you need a ssh key pair (generated with ssh-keygen -f ./myKey -t rsa) and then you have to run an Azure CLI command to upload the public key to the VM:
az vm user update -u myUserName --ssh-key-value "myPublicKeyContentHere" -g resourceGroup -n VMName

The command will upload the public key to your $HOME/.ssh/authorized_keys folder in the VM, so now you can login to the VM using:
ssh -i myKey.priv myUser@myVM

That's good, and probably using the ssh key to login is more convenient than using a regular password, but anyway I wanted to be able to use a password also. Once you log to the machine, if you try to change your forgotten password with passwd, you'll be asked for the old password, so what?

Well, that's simple, the salted hash of the different user's passwords is stored in the /etc/shadow file and you can edit it as super user with "su". You'll see one line like this for your user:

myUser:$6$gYiiiiiiiiiiiiiiiiiiiy0$uJ6Eu9IwZ/gggggggggggggggggggggggggggg.vgBol0j0HT1Z0Kxmg46AbVTIn0G4cKazzhhhhhhhhhhhh0yWF1:19019:0:99999:7:::

The second item in that line is your hashed password. Delete it (I mean, the password, not the whole line), and now your user has an empty password. If you run again the passwd command you won't be asked for the old (empty) password, and you can set your new password.

No comments:

Post a Comment