I'm in a bit of a conundrum. Put as simply as possible, I am trying to craft part of a script that will edit the /etc/sudoers file in linux utilizing sed. Due to the account I am using, I need to do this editing WITH sudo running the sed command. Due to this however, I can't end the command before I do all of the edits to the file otherwise I will lock myself out.
I'm specifically trying to modify the %wheel group within the /etc/sudoers file, and there are preexisting entries within the file on various servers. I want to add my own optimized entry, comment out the older entries, and finally make sure the entry I JUST added is not commented out.
I have crafted statements in sed to do this, and they run individually just fine, it's when I try to do anything to allow multiple statements in the same command that neither statement functions. (Even if I don't get any syntax errors)
For example my statements:
sudo sed 's/.*wheel/#&/' /etc/sudoers
---- works great by itself
sudo sed 's/^#\s*\(%wheel\s\+ALL=(ALL)\s\+NOPASSWD:\s\+LOG_INPUT:\s\+LOG_OUTPUT:\s\+ALL\)/\1/' /etc/sudoers
---- also works great by itself
However, since I must combine them into a single command, then things stop working..... such as:
sudo sed 's/(.*wheel/#&) (^#\s*\(%wheel\s\+ALL=(ALL)\s\+NOPASSWD:\s\+LOG_INPUT:\s\+LOG_OUTPUT:\s\+ALL\))/' /etc/sudoers -
--- Nope, no dice
Or
sudo sed -e 's/.*wheel/#&/' -e 's/^#\s*\(%wheel\s\+ALL=(ALL)\s\+NOPASSWD:\s\+LOG_INPUT:\s\+LOG_OUTPUT:\s\+ALL\)/\1/' /etc/sudoers
---- Doesn't work either
Etc, etc, nothing along those lines seems to solve the issue. The output after I run the 2 above commands shows that it is unchanged as if I just ran a cat on the original file. (yes I know I'm not permanently editing the file in place at the second)
Now I understand that yes... I could add a temporary entry at the bottom of the file for my user to begin with, and then delete it as the last step to circumvent this. Or I could run a script with a loop and likely also be successful, but there has to be a more elegant way to do it with a single command as I intended.
Anyone have any ideas? Also, if there is a better way to do what I intend to do in the first place, I'd be interested to know.
EDIT: Input/Output examples:
The lines that are in the file by default:
%wheel ALL=(ALL) ALL
%wheel ALL=(ALL) NOPASSWD: LOG_INPUT: LOG_OUTPUT: ALL ---added by me in a previous step
#%wheel ALL=(ALL) NOPASSWD: ALL
I want the final product after sed is run to look like this:
#%wheel ALL=(ALL) ALL
%wheel ALL=(ALL) NOPASSWD: LOG_INPUT: LOG_OUTPUT: ALL
#%wheel ALL=(ALL) NOPASSWD: ALL
I don't want it to look like this:
#%wheel ALL=(ALL) ALL
#%wheel ALL=(ALL) NOPASSWD: LOG_INPUT: LOG_OUTPUT: ALL
#%wheel ALL=(ALL) NOPASSWD: ALL
AND I don't want it to look like this:
%wheel ALL=(ALL) ALL
%wheel ALL=(ALL) NOPASSWD: LOG_INPUT: LOG_OUTPUT: ALL
%wheel ALL=(ALL) NOPASSWD: ALL
Right as it is now, the multiple statement sed command doesn't seem to do anything, it's only when I execute them as separate commands that I get the desired output. (which I can't do in this circumstance)
Any help would be appreciated!
sudoers
file should be edited withvisudo
.