Regex pattern to edit /etc/sudoers file

You shouldn't edit the /etc/sudoers file with any sort of script. There's a reason for the visudo command. Edits to the sudoers file should be rare and well-controlled.

That being said, if your editor for the visudo command is vi, you can run something like :%s/^# %wheel/%wheel/ to uncomment all of the lines what start with %wheel.

Or, if you reeeeeeally think it's necessary:

sudo sed --in-place 's/^#\s*\(%wheel\s\+ALL=(ALL)\s\+NOPASSWD:\s\+ALL\)/\1/' /etc/sudoers

Run it without the --in-place first to check the output. Use it at your own risk.


The following should work:

sed -i 's/^#\s*\(%wheel\s*ALL=(ALL)\s*NOPASSWD:\s*ALL\)/\1/' /etc/sudoers

No need to use sed. On centos you can simply create file at /etc/sudoers.d/

echo 'myuser ALL=(ALL) NOPASSWD: ALL' >> ./myuser
sudo chown root:root ./myuser
sudo mv ./myuser /etc/sudoers.d/

This will enable sudo without password for myuser. Check this for more information: https://unix.stackexchange.com/questions/375433/etc-sudoers-vs-etc-sudoers-d-file-for-enabling-sudo-for-a-user