Fixing a corrupt /etc/sudoers file in Linux VM in Azure

Tushar jadhav
2 min readJul 30, 2022

--

I was editing the /etc/sudoers file with vim on a linux VM (RHEL 7.9) in Azure trying to remove or disable being prompted for a password every time I used sudo.

we added the following to the file

vim /etc/sudoers
root ALL=(ALL:ALL) ALL
myadminuser ALL=(ALL:ALL) ALL NOPASSWD: ALL

Apparently it doesn’t follow the correct syntax so immediately afterwards I couldn’t do sudo. Below is the error message

[myadminuser@MYSERVER ~]$ sudo -i
>>> /etc/sudoers: syntax error near line 23 <<<
sudo: parse error in /etc/sudoers near line 23
sudo: no valid sudoers sources found, quitting
sudo: unable to initialize policy plugin

Since on the Azure VMs you don’t have the root password, then you’re stuck as the regular user do not have permissions to edit the sudoers file and you can’t sudo to root.

You could mount the VM disk to another VM and then edit the file that way, but that is cumbersome.

Solution is below.

From the Azure portal start Cloud CLI, choose Powershell

Run the following command to make /etc/sudoers editable by master

az vm run-command invoke — resource-group YOUR_RESOURCE_GROUP — name YOURVM — command-id RunShellScript — scripts “chmod 446 /etc/sudoers”

This gives the regular user permission to edit the file

with nano or VI undo the changes (i just deleted the NOPASSWD: ALL):

login to the server and edit sudoers file
nano /etc/sudoers (no sudo since you have access)

after edit, run the below command to configure default access to file.

az vm run-command invoke — resource-group YOUR_RESOURCE_GROUP — name YOURVM — command-id RunShellScript — scripts “chmod 440 /etc/sudoers”

I got fixed my problem after run above commands

If you found this article helpful then do click on 👏 the button and also feel free to drop a comment.

Follow for more stories like this 😊

--

--

Tushar jadhav

13 years of experience in IT with recent experience in DevOps and Cloud Engineering, As a blogger where I share topics and solutions related to DevOps &Linux