My colleague showed me a neat trick to run scripts on Azure Linux VMs as root. Use the Custom Script for Linux Extension. The Extension isn’t just for deployment - it can be run on an already-deployed Linux VM. By leveraging it to run shell scripts as root, you can make repairs to the system if you lock yourself out or need to add a user or ssh key for access. Store your script in a file and execute the extension via Portal, Azure CLI, or PowerShell.
The steps are covered in this MSDN blog article.