What's the easiest way or method to automate the adding of a user to a fresh sudoers file?

StackOverflow https://stackoverflow.com/questions/5347489

  •  27-10-2019
  •  | 
  •  

Pergunta

so, I've adduser'ed micronxd. I've passwd'd micronxd. now I wanna make micronxd a sudo user.

I'm trying to pack as much of this process into a shell script as possible. If possible, could the solution involve sed'ing the sudoers file? I'd like to get a really strong grasp on that beastly little nugget (sed).

Foi útil?

Solução

The sudoers file should really only be edited with visudo(8).

That said, perhaps you should just add micronxd to the wheel group when adding the micronxd user, (see adduser(8) --add_extra_groups option) and then add this line to sudoers (via visudo(8) :) :

%wheel ALL=(ALL) ALL

Now, every user you create with the primary group or a supplementary group of wheel gets sudo access automatically. You may never need to edit the sudoers file again.

(Note that you can easily add anyone you want to the wheel group; vigr(8), find the wheel group, and append their login name to the line. Use commas to separate user names.)

Outras dicas

OK OK OK! I totally got it. This may be a bit overkill, but I'm not sure if sudoers files are alike across diff distros.

I found documentation on the holding buffer for sed here: http://anaturb.net/sed.htm

and on the x command for sed here: http://www.kingcomputerservices.com/unix_101/hands_off_editing_with_sed_part_2.htm

and I came up with this: sed -e '/^root/{h;}' -e 'x' -e 's/root/micronxd/' -e 'x' -e '/^root/G' -e 'wsudoers' sudoers

Here's the breakdown of the 6 steps:

1. '/^root/{h;}'

finds uncommented lines starting with root, and replaces the holding buffer with it.

  • regular sed buffer: [the whole sudoers file]
  • holding buffer: root ALL=(ALL) ALL

2. 'x'

swaps the holding buffer with the regular "to be sed'd" buffer.

  • regular sed buffer: root ALL=(ALL) ALL
  • holding buffer: [the whole sudoers file]

3. 's/root/micronxd/'

replaces root with micronxd

  • regular sed buffer: micronxd ALL=(ALL) ALL
  • holding buffer: [the whole sudoers file]

4. 'x'

swaps the holding buffer with the regular "to be sed'd" buffer, again.

  • regular sed buffer: [the whole sudoers file]
  • holding buffer: micronxd ALL=(ALL) ALL

5. '/^root/G'

the G appends the holding buffer after the line matching ^root.

  • regular sed buffer (just the relevant bits):

## Allow root ALL=(ALL) ALL to run any commands anywhere
root ALL=(ALL) ALL
micronxd ALL=(ALL) ALL

  • holding buffer: micronxd ALL=(ALL) ALL

6. 'wsudoers'

writes the regular sed buffer back to sudoers

I'm sure there's a better way, but I'm trying to get the hang of sed. plus, I wasn't sure if user MACHINE=COMMANDS ???? is the syntax for all linux systems, or just fedora (which is what I'm dealing with).

Anyways, I'm happy to see other ways (especially better ways).

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top