linux - Add a sudoer non-interactively from command line

07
2014-07
  • Roy Truelove

    (On Centos through Docker)

    I know that I can add a sudoer using visudo. Is there a way to add a user to the sudoer list straight from the command line, so I don't have to do it interactively?

    I'm asking because I'm trying to provision my Docker centos container which doesn't play with interactivity.

  • Answers
  • mhucka

    You could use cat to append text to the end of /etc/sudoers. First, make a backup copy of your /etc/sudoers file. Then:

    cat >> /etc/sudoers
    ...type one or more lines here...
    [control-D]
    

    Make absolutely sure to use two greater-than characters (>>) and not just one, or else you will overwrite the entire contents of your file.

  • BenjiWiebe

    To be able to do that, you should make sure you have the following line in your sudoers file:

    %sudo   ALL=(ALL:ALL) ALL
    

    You can customize the above line to change the permissions just as though %sudo was a user. That line will allow any users in the sudo group to use sudo.

    Now to allow <username> to use sudo, you can just do usermod -a -G sudo <username> as root, which adds <username> to the sudo group.

  • datasmid

    You could start by enabling the 'wheel' group in sudoers, and after you only need to add users to that group. Avoids convoluting the sudoers file.


  • Related Question

    linux - Command-line access to /dev/mem in Ubuntu
  • Shane

    So I'm trying to search through all my memory for a specific string, but I don't know how to get access to the contents of my memory.

    I try to run this but get an error message:

    ~$ sudo cat /dev/mem | strings | grep findme
    [sudo] password for user:
    cat: /dev/mem: Permission denied
    

    I'm running Ubuntu 9.04 64-bit with the 2.6.28-15-generic kernel. From my understanding it's some sort of built-in protection. How can I get around this if sudo isn't sufficient?


  • Related Answers
  • Kim

    Access to "real" memory via /dev/mem has been disabled by this kernel patch. So your options are to either recompile your kernel with the NONPROMISC_DEVMEM option disabled or you try a different route, such as writing your own kernel module. Happy kernel hacking!