Method 1 : Use the wheel group
On CentOS, by default members of the wheel groups are sudoers. Thus the simplest method is to add the user to the wheel group.
Add user in wheel group
usermod -aG wheel myuser
Note: this method requires the user to log out and log back in for changes to take effect as it relies on group memberships
Make sure wheel group can run commands
Make sure the following line is present
## Allows people in group wheel to run all commands %wheel ALL=(ALL) ALL
(Optional) make the sudo passwordless
Comment the first line and uncomment the second
## Allows people in group wheel to run all commands #%wheel ALL=(ALL) ALL ## Same thing without a password %wheel ALL=(ALL) NOPASSWD: ALL
Method 2 : Explicitely declare user as sudoer
Add the end of the global configuration file (
/etc/sudoers) the following line allows to include bits of specific configuration.
## Read drop-in files from /etc/sudoers.d (the # here does not mean a comment) #includedir /etc/sudoers.d
So in order to declare myuser as a sudoer :
create the file
/etc/sudoers.d/myuser as :
myuser ALL=(ALL) NOPASSWD: ALL
This will allow myuser to become a password-less sudoer
Add a user as sudoer in a Dockerfile
The following excerpt creates a user (ansible, password ansible) and makes it a passwordless sudoer.
RUN \ useradd ansible &&\ echo 'ansible:ansible' | chpasswd &&\ echo 'ansible ALL=(ALL) NOPASSWD: ALL' >> /etc/sudoers &&\ mkdir /home/ansible/.ssh