Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex pattern to edit /etc/sudoers file

I want to remove (uncommnet #) wheel group in /etc/sudoers file so what would be the Regex pattern i should use?

#cat /etc/sudoers
....
....
## Allows members of the 'sys' group to run networking, software,
## service management apps and more.
# %sys ALL = NETWORKING, SOFTWARE, SERVICES, STORAGE, DELEGATING, PROCESSES, LOCATE, DRIVERS

## Allows people in group wheel to run all commands
# %wheel        ALL=(ALL)       ALL

## Same thing without a password
# %wheel  ALL=(ALL)       NOPASSWD: ALL

....
....

I want to remove # from following line.

...
# %wheel  ALL=(ALL)       NOPASSWD: ALL
...
like image 702
Satish Avatar asked May 02 '12 19:05

Satish


People also ask

What command is used to edit the sudoers file?

As with the /etc/sudoers file itself, you should always edit files within the /etc/sudoers. d directory with visudo . The syntax for editing these files would be: sudo visudo -f /etc/sudoers.


2 Answers

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.

like image 80
Tim Pote Avatar answered Oct 18 '22 09:10

Tim Pote


The following should work:

sed -i 's/^#\s*\(%wheel\s*ALL=(ALL)\s*NOPASSWD:\s*ALL\)/\1/' /etc/sudoers
like image 44
Andrew Clark Avatar answered Oct 18 '22 09:10

Andrew Clark