How does the ubuntu
user on the AWS images for Ubuntu Server 12.04 have passwordless sudo
for all commands when there is no configuration for it in /etc/sudoers
?
I'm using Ubuntu server 12.04 on Amazon. I want to add a new user that has the same behavior as the default Ubuntu user. Specifically I want passwordless sudo
for this new user.
So I've added a new user and went to edit /etc/sudoers
(using visudo of course). From reading that file it seemed like the default ubuntu
user was getting it's passwordless sudo
from being a member of the admin
group. So I added my new user to that. Which didn't work. Then I tried adding the NOPASSWD
directive to sudoers
. Which also didn't work.
Anyway, now I'm just curious. How does the ubuntu
user get passwordless privileges if they aren't defined in /etc/sudoers
. What is the mechanism that allows this?
Okay, I have discovered the answer so may as well put it here for completeness. At the end of
/etc/sudoers
there is what I thought was just a comment:However this actually includes the contents of that directory. Inside of which is the file
/etc/sudoers.d/90-cloudimg-ubuntu
. Which has the expected contentsSo that is where the sudo configuration for the default ubuntu user lives.
You should edit this file using visudo. The following command will let you edit the correct file with visudo.
And add a line like:
At the end.
I found that the most straight forward thing to do, in order to easily replicate this behavior across multiple servers, was the following:
Change this line:
to this line:
And move it under this line:
you should now have this:
then for every user that needs sudo access WITH a password:
and for every user that needs sudo access WITH NO password:
(on older versions of ubuntu, you may need to):
And that's it!
Edit: You may have to add the admin group as I don't think it exists by default.
You can also add the default AWS
ubuntu
user to theadmin
group via this command:Note: As @hata mentioned, you may need to use
adm
as your admin group name, depending on which version of Ubuntu is being used.I would create my own file under /etc/sudoers.d/ directory - the file created by Amazon Cloud might be overwritten in case of any update. After creating your file in /etc/sudoers.d, add this entry,
Reboot the system and this will work.
Short answer without using any editor (tested on bash, very risky to execute on remote hosts).
Configure sudo to work without a password for the current user:
Check the edit with:
Verify if you can use sudo without a password:
...or simply try it with:
As I was researching this, I realized that there's a line in the
/etc/sudoers
file that is not a comment, but a directive that makes any file or folder under the directory/etc/sudoers/*
override the contents of/etc/sudoers
.This is a sneaky little directive, as it appears to be a commented line upon first glance. It looks like this:
This is how I've implemented the non-root, passwordless user in an ephemeral Docker Image for use in a CICD pipeline with the base image of
ubuntu:18.04
:What happens with the above code:
foo
is created.foo
is added to the both thefoo
andsudo
group./home/foo
./bin/bash
.foo
androot
are deleted.sed
command does inline updates to the/etc/sudoers
file to allowfoo
androot
users passwordless access to thesudo
command.sed
command disables the#includedir
directive that would allow any files in subdirectories to override these inline updates.